| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Module\Base;
- use App\Module\Users;
- use DB;
- use Request;
- /**
- * @apiDefine docs
- *
- * 知识库
- */
- class DocsController extends Controller
- {
- public function __invoke($method, $action = '')
- {
- $app = $method ? $method : 'main';
- if ($action) {
- $app .= "__" . $action;
- }
- return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
- }
- /**
- * 知识库列表
- *
- * @apiParam {Number} [page] 当前页,默认:1
- * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
- */
- public function book__lists()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $lists = DB::table('docs_book')
- ->orderByDesc('id')
- ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
- $lists = Base::getPageList($lists);
- if ($lists['total'] == 0) {
- return Base::retError('暂无知识库', $lists);
- }
- return Base::retSuccess('success', $lists);
- }
- /**
- * 添加/修改知识库
- *
- * @apiParam {String} title 知识库名称
- */
- public function book__add()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $id = intval(Request::input('id'));
- $title = trim(Request::input('title'));
- if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
- return Base::retError('标题限制2-100个字!');
- }
- if ($id > 0) {
- // 修改
- $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
- if (empty($row)) {
- return Base::retError('知识库不存在或已被删除!');
- }
- if ($row['username'] != $user['username']) {
- return Base::retError('此操作仅限知识库负责人!');
- }
- $data = [
- 'title' => $title,
- ];
- DB::table('docs_book')->where('id', $id)->update($data);
- return Base::retSuccess('修改成功!', $data);
- } else {
- // 添加
- $data = [
- 'username' => $user['username'],
- 'title' => $title,
- 'indate' => Base::time(),
- ];
- $id = DB::table('docs_book')->insertGetId($data);
- if (empty($id)) {
- return Base::retError('系统繁忙,请稍后再试!');
- }
- $data['id'] = $id;
- return Base::retSuccess('添加成功!', $data);
- }
- }
- /**
- * 删除知识库
- *
- * @apiParam {Number} id 知识库数据ID
- */
- public function book__delete()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $id = intval(Request::input('id'));
- $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
- if (empty($row)) {
- return Base::retError('知识库不存在或已被删除!');
- }
- if ($row['username'] != $user['username']) {
- return Base::retError('此操作仅限知识库负责人!');
- }
- DB::table('docs_book')->where('id', $id)->delete();
- DB::table('docs_section')->where('bookid', $id)->delete();
- //未完成,应该还要删除章节
- return Base::retSuccess('删除成功!');
- }
- /**
- * 章节列表
- *
- * @apiParam {Number} bookid 知识库数据ID
- */
- public function section__lists()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $lists = Base::DBC2A(DB::table('docs_section')
- ->where('bookid', intval(Request::input('bookid')))
- ->orderByDesc('id')
- ->take(200)
- ->get());
- if (empty($lists)) {
- return Base::retError('暂无章节');
- }
- return Base::retSuccess('success', Base::list2Tree($lists, 'id', 'parentid'));
- }
- /**
- * 添加/修改章节
- *
- * @apiParam {Number} bookid 知识库数据ID
- * @apiParam {String} title 章节名称
- * @apiParam {String} type 章节类型
- */
- public function section__add()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $bookid = intval(Request::input('bookid'));
- $bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
- if (empty($bookRow)) {
- return Base::retError('知识库不存在或已被删除!');
- }
- //
- $id = intval(Request::input('id'));
- $title = trim(Request::input('title'));
- $type = trim(Request::input('type'));
- if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
- return Base::retError('标题限制2-100个字!');
- }
- if ($id > 0) {
- // 修改
- $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
- if (empty($row)) {
- return Base::retError('知识库不存在或已被删除!');
- }
- $data = [
- 'title' => $title,
- ];
- DB::table('docs_section')->where('id', $id)->update($data);
- return Base::retSuccess('修改成功!', $data);
- } else {
- // 添加
- if (!in_array($type, ['text', 'mind', 'excel', 'chart'])) {
- return Base::retError('参数错误!');
- }
- $parentid = 0;
- if ($id < 0) {
- $count = Base::DBC2A(DB::table('docs_section')->where('id', abs($id))->where('bookid', $bookid)->count());
- if ($count > 0) {
- $parentid = abs($id);
- }
- }
- $data = [
- 'bookid' => $bookid,
- 'parentid' => $parentid,
- 'username' => $user['username'],
- 'title' => $title,
- 'type' => $type,
- 'indate' => Base::time(),
- ];
- $id = DB::table('docs_section')->insertGetId($data);
- if (empty($id)) {
- return Base::retError('系统繁忙,请稍后再试!');
- }
- $data['id'] = $id;
- return Base::retSuccess('添加成功!', $data);
- }
- }
- /**
- * 删除章节
- *
- * @apiParam {Number} id 章节数据ID
- */
- public function section__delete()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $id = intval(Request::input('id'));
- $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
- if (empty($row)) {
- return Base::retError('文档不存在或已被删除!');
- }
- DB::table('docs_section')->where('id', $id)->delete();
- //未完成,应该还要删除章节
- return Base::retSuccess('删除成功!');
- }
- }
|