DocsController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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('id')
  139. ->take(200)
  140. ->get());
  141. if (empty($lists)) {
  142. return Base::retError('暂无章节');
  143. }
  144. return Base::retSuccess('success', Base::list2Tree($lists, 'id', 'parentid'));
  145. }
  146. /**
  147. * 添加/修改章节
  148. *
  149. * @apiParam {Number} bookid 知识库数据ID
  150. * @apiParam {String} title 章节名称
  151. * @apiParam {String} type 章节类型
  152. */
  153. public function section__add()
  154. {
  155. $user = Users::authE();
  156. if (Base::isError($user)) {
  157. return $user;
  158. } else {
  159. $user = $user['data'];
  160. }
  161. //
  162. $bookid = intval(Request::input('bookid'));
  163. $bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
  164. if (empty($bookRow)) {
  165. return Base::retError('知识库不存在或已被删除!');
  166. }
  167. //
  168. $id = intval(Request::input('id'));
  169. $title = trim(Request::input('title'));
  170. $type = trim(Request::input('type'));
  171. if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
  172. return Base::retError('标题限制2-100个字!');
  173. }
  174. if ($id > 0) {
  175. // 修改
  176. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  177. if (empty($row)) {
  178. return Base::retError('知识库不存在或已被删除!');
  179. }
  180. $data = [
  181. 'title' => $title,
  182. ];
  183. DB::table('docs_section')->where('id', $id)->update($data);
  184. return Base::retSuccess('修改成功!', $data);
  185. } else {
  186. // 添加
  187. if (!in_array($type, ['text', 'mind', 'excel', 'chart'])) {
  188. return Base::retError('参数错误!');
  189. }
  190. $parentid = 0;
  191. if ($id < 0) {
  192. $count = Base::DBC2A(DB::table('docs_section')->where('id', abs($id))->where('bookid', $bookid)->count());
  193. if ($count > 0) {
  194. $parentid = abs($id);
  195. }
  196. }
  197. $data = [
  198. 'bookid' => $bookid,
  199. 'parentid' => $parentid,
  200. 'username' => $user['username'],
  201. 'title' => $title,
  202. 'type' => $type,
  203. 'indate' => Base::time(),
  204. ];
  205. $id = DB::table('docs_section')->insertGetId($data);
  206. if (empty($id)) {
  207. return Base::retError('系统繁忙,请稍后再试!');
  208. }
  209. $data['id'] = $id;
  210. return Base::retSuccess('添加成功!', $data);
  211. }
  212. }
  213. /**
  214. * 删除章节
  215. *
  216. * @apiParam {Number} id 章节数据ID
  217. */
  218. public function section__delete()
  219. {
  220. $user = Users::authE();
  221. if (Base::isError($user)) {
  222. return $user;
  223. } else {
  224. $user = $user['data'];
  225. }
  226. //
  227. $id = intval(Request::input('id'));
  228. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  229. if (empty($row)) {
  230. return Base::retError('文档不存在或已被删除!');
  231. }
  232. DB::table('docs_section')->where('id', $id)->delete();
  233. //未完成,应该还要删除章节
  234. return Base::retSuccess('删除成功!');
  235. }
  236. }