DocsController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Users;
  6. use DB;
  7. use Request;
  8. /**
  9. * @apiDefine docs
  10. *
  11. * 知识库
  12. */
  13. class DocsController extends Controller
  14. {
  15. public function __invoke($method, $action = '')
  16. {
  17. $app = $method ? $method : 'main';
  18. if ($action) {
  19. $app .= "__" . $action;
  20. }
  21. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  22. }
  23. /**
  24. * 知识库列表
  25. *
  26. * @apiParam {Number} [page] 当前页,默认:1
  27. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  28. */
  29. public function book__lists()
  30. {
  31. $user = Users::authE();
  32. if (Base::isError($user)) {
  33. return $user;
  34. } else {
  35. $user = $user['data'];
  36. }
  37. //
  38. $lists = DB::table('docs_book')
  39. ->orderByDesc('id')
  40. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  41. $lists = Base::getPageList($lists);
  42. if ($lists['total'] == 0) {
  43. return Base::retError('暂无知识库', $lists);
  44. }
  45. return Base::retSuccess('success', $lists);
  46. }
  47. /**
  48. * 添加/修改知识库
  49. *
  50. * @apiParam {String} title 知识库名称
  51. */
  52. public function book__add()
  53. {
  54. $user = Users::authE();
  55. if (Base::isError($user)) {
  56. return $user;
  57. } else {
  58. $user = $user['data'];
  59. }
  60. //
  61. $id = intval(Request::input('id'));
  62. $title = trim(Request::input('title'));
  63. if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
  64. return Base::retError('标题限制2-100个字!');
  65. }
  66. if ($id > 0) {
  67. // 修改
  68. $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
  69. if (empty($row)) {
  70. return Base::retError('知识库不存在或已被删除!');
  71. }
  72. if ($row['username'] != $user['username']) {
  73. return Base::retError('此操作仅限知识库负责人!');
  74. }
  75. $data = [
  76. 'title' => $title,
  77. ];
  78. DB::table('docs_book')->where('id', $id)->update($data);
  79. return Base::retSuccess('修改成功!', $data);
  80. } else {
  81. // 添加
  82. $data = [
  83. 'username' => $user['username'],
  84. 'title' => $title,
  85. 'indate' => Base::time(),
  86. ];
  87. $id = DB::table('docs_book')->insertGetId($data);
  88. if (empty($id)) {
  89. return Base::retError('系统繁忙,请稍后再试!');
  90. }
  91. $data['id'] = $id;
  92. return Base::retSuccess('添加成功!', $data);
  93. }
  94. }
  95. /**
  96. * 删除知识库
  97. *
  98. * @apiParam {Number} id 知识库数据ID
  99. */
  100. public function book__delete()
  101. {
  102. $user = Users::authE();
  103. if (Base::isError($user)) {
  104. return $user;
  105. } else {
  106. $user = $user['data'];
  107. }
  108. //
  109. $id = intval(Request::input('id'));
  110. $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
  111. if (empty($row)) {
  112. return Base::retError('知识库不存在或已被删除!');
  113. }
  114. if ($row['username'] != $user['username']) {
  115. return Base::retError('此操作仅限知识库负责人!');
  116. }
  117. DB::table('docs_book')->where('id', $id)->delete();
  118. DB::table('docs_section')->where('bookid', $id)->delete();
  119. //未完成,应该还要删除章节
  120. return Base::retSuccess('删除成功!');
  121. }
  122. /**
  123. * 章节列表
  124. *
  125. * @apiParam {Number} bookid 知识库数据ID
  126. */
  127. public function section__lists()
  128. {
  129. $user = Users::authE();
  130. if (Base::isError($user)) {
  131. return $user;
  132. } else {
  133. $user = $user['data'];
  134. }
  135. //
  136. $lists = Base::DBC2A(DB::table('docs_section')
  137. ->where('bookid', intval(Request::input('bookid')))
  138. ->orderByDesc('inorder')
  139. ->orderByDesc('id')
  140. ->take(300)
  141. ->get());
  142. if (empty($lists)) {
  143. return Base::retError('暂无章节');
  144. }
  145. foreach ($lists AS $key => $item) {
  146. $lists[$key]['icon'] = Base::fillUrl('images/files/' . $item['type'] . '.png');
  147. }
  148. return Base::retSuccess('success', Base::list2Tree($lists, 'id', 'parentid'));
  149. }
  150. /**
  151. * 添加/修改章节
  152. *
  153. * @apiParam {Number} bookid 知识库数据ID
  154. * @apiParam {String} title 章节名称
  155. * @apiParam {String} type 章节类型
  156. */
  157. public function section__add()
  158. {
  159. $user = Users::authE();
  160. if (Base::isError($user)) {
  161. return $user;
  162. } else {
  163. $user = $user['data'];
  164. }
  165. //
  166. $bookid = intval(Request::input('bookid'));
  167. $bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
  168. if (empty($bookRow)) {
  169. return Base::retError('知识库不存在或已被删除!');
  170. }
  171. $count = DB::table('docs_section')->where('bookid', $bookid)->count();
  172. if ($count >= 300) {
  173. return Base::retError('知识库章节已经超过最大限制(300)!');
  174. }
  175. //
  176. $id = intval(Request::input('id'));
  177. $title = trim(Request::input('title'));
  178. $type = trim(Request::input('type'));
  179. if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
  180. return Base::retError('标题限制2-100个字!');
  181. }
  182. if ($id > 0) {
  183. // 修改
  184. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  185. if (empty($row)) {
  186. return Base::retError('知识库不存在或已被删除!');
  187. }
  188. $data = [
  189. 'title' => $title,
  190. ];
  191. DB::table('docs_section')->where('id', $id)->update($data);
  192. return Base::retSuccess('修改成功!', $data);
  193. } else {
  194. // 添加
  195. if (!in_array($type, ['document', 'mind', 'sheet', 'chart', 'folder'])) {
  196. return Base::retError('参数错误!');
  197. }
  198. $parentid = 0;
  199. if ($id < 0) {
  200. $count = Base::DBC2A(DB::table('docs_section')->where('id', abs($id))->where('bookid', $bookid)->count());
  201. if ($count > 0) {
  202. $parentid = abs($id);
  203. }
  204. }
  205. $data = [
  206. 'bookid' => $bookid,
  207. 'parentid' => $parentid,
  208. 'username' => $user['username'],
  209. 'title' => $title,
  210. 'type' => $type,
  211. 'inorder' => intval(DB::table('docs_section')->select(['inorder'])->where('bookid', $bookid)->orderByDesc('inorder')->value('inorder')) + 1,
  212. 'indate' => Base::time(),
  213. ];
  214. $id = DB::table('docs_section')->insertGetId($data);
  215. if (empty($id)) {
  216. return Base::retError('系统繁忙,请稍后再试!');
  217. }
  218. $data['id'] = $id;
  219. return Base::retSuccess('添加成功!', $data);
  220. }
  221. }
  222. /**
  223. * 排序任务
  224. *
  225. * @apiParam {Number} bookid 知识库数据ID
  226. * @apiParam {String} oldsort 旧排序数据
  227. * @apiParam {String} newsort 新排序数据
  228. */
  229. public function section__sort()
  230. {
  231. $user = Users::authE();
  232. if (Base::isError($user)) {
  233. return $user;
  234. } else {
  235. $user = $user['data'];
  236. }
  237. //
  238. $bookid = intval(Request::input('bookid'));
  239. $bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
  240. if (empty($bookRow)) {
  241. return Base::retError('知识库不存在或已被删除!');
  242. }
  243. //
  244. $newSort = explode(";", Request::input('newsort'));
  245. if (count($newSort) == 0) {
  246. return Base::retError('参数错误!');
  247. }
  248. //
  249. $count = count($newSort);
  250. foreach ($newSort AS $sort => $item) {
  251. list($newId, $newParentid) = explode(':', $item);
  252. DB::table('docs_section')->where([
  253. 'id' => $newId,
  254. 'bookid' => $bookid
  255. ])->update([
  256. 'inorder' => $count - intval($sort),
  257. 'parentid' => $newParentid
  258. ]);
  259. }
  260. return Base::retSuccess('保存成功!');
  261. }
  262. /**
  263. * 删除章节
  264. *
  265. * @apiParam {Number} id 章节数据ID
  266. */
  267. public function section__delete()
  268. {
  269. $user = Users::authE();
  270. if (Base::isError($user)) {
  271. return $user;
  272. } else {
  273. $user = $user['data'];
  274. }
  275. //
  276. $id = intval(Request::input('id'));
  277. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  278. if (empty($row)) {
  279. return Base::retError('文档不存在或已被删除!');
  280. }
  281. DB::table('docs_section')->where('parentid', $id)->update([ 'parentid' => $row['parentid'] ]);
  282. DB::table('docs_section')->where('id', $id)->delete();
  283. //未完成,应该还要删除章节
  284. return Base::retSuccess('删除成功!');
  285. }
  286. }