DocsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 App\Tasks\PushTask;
  7. use Cache;
  8. use DB;
  9. use Hhxsv5\LaravelS\Swoole\Task\Task;
  10. use Request;
  11. /**
  12. * @apiDefine docs
  13. *
  14. * 知识库
  15. */
  16. class DocsController extends Controller
  17. {
  18. public function __invoke($method, $action = '')
  19. {
  20. $app = $method ? $method : 'main';
  21. if ($action) {
  22. $app .= "__" . $action;
  23. }
  24. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  25. }
  26. /**
  27. * 知识库列表
  28. *
  29. * @apiParam {Number} [page] 当前页,默认:1
  30. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  31. */
  32. public function book__lists()
  33. {
  34. $user = Users::authE();
  35. if (Base::isError($user)) {
  36. return $user;
  37. } else {
  38. $user = $user['data'];
  39. }
  40. //
  41. $lists = DB::table('docs_book')
  42. ->orderByDesc('id')
  43. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  44. $lists = Base::getPageList($lists);
  45. if ($lists['total'] == 0) {
  46. return Base::retError('暂无知识库', $lists);
  47. }
  48. return Base::retSuccess('success', $lists);
  49. }
  50. /**
  51. * 添加/修改知识库
  52. *
  53. * @apiParam {String} title 知识库名称
  54. */
  55. public function book__add()
  56. {
  57. $user = Users::authE();
  58. if (Base::isError($user)) {
  59. return $user;
  60. } else {
  61. $user = $user['data'];
  62. }
  63. //
  64. $id = intval(Request::input('id'));
  65. $title = trim(Request::input('title'));
  66. if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
  67. return Base::retError('标题限制2-100个字!');
  68. }
  69. if ($id > 0) {
  70. // 修改
  71. $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
  72. if (empty($row)) {
  73. return Base::retError('知识库不存在或已被删除!');
  74. }
  75. if ($row['username'] != $user['username']) {
  76. return Base::retError('此操作仅限知识库负责人!');
  77. }
  78. $data = [
  79. 'title' => $title,
  80. ];
  81. DB::table('docs_book')->where('id', $id)->update($data);
  82. return Base::retSuccess('修改成功!', $data);
  83. } else {
  84. // 添加
  85. $data = [
  86. 'username' => $user['username'],
  87. 'title' => $title,
  88. 'indate' => Base::time(),
  89. ];
  90. $id = DB::table('docs_book')->insertGetId($data);
  91. if (empty($id)) {
  92. return Base::retError('系统繁忙,请稍后再试!');
  93. }
  94. $data['id'] = $id;
  95. return Base::retSuccess('添加成功!', $data);
  96. }
  97. }
  98. /**
  99. * 删除知识库
  100. *
  101. * @apiParam {Number} id 知识库数据ID
  102. */
  103. public function book__delete()
  104. {
  105. $user = Users::authE();
  106. if (Base::isError($user)) {
  107. return $user;
  108. } else {
  109. $user = $user['data'];
  110. }
  111. //
  112. $id = intval(Request::input('id'));
  113. $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
  114. if (empty($row)) {
  115. return Base::retError('知识库不存在或已被删除!');
  116. }
  117. if ($row['username'] != $user['username']) {
  118. return Base::retError('此操作仅限知识库负责人!');
  119. }
  120. DB::table('docs_book')->where('id', $id)->delete();
  121. DB::table('docs_section')->where('bookid', $id)->delete();
  122. DB::table('docs_content')->where('bookid', $id)->delete();
  123. return Base::retSuccess('删除成功!');
  124. }
  125. /**
  126. * 章节列表
  127. *
  128. * @apiParam {Number} bookid 知识库数据ID
  129. */
  130. public function section__lists()
  131. {
  132. $user = Users::authE();
  133. if (Base::isError($user)) {
  134. return $user;
  135. } else {
  136. $user = $user['data'];
  137. }
  138. //
  139. $lists = Base::DBC2A(DB::table('docs_section')
  140. ->where('bookid', intval(Request::input('bookid')))
  141. ->orderByDesc('inorder')
  142. ->orderByDesc('id')
  143. ->take(500)
  144. ->get());
  145. if (empty($lists)) {
  146. return Base::retError('暂无章节');
  147. }
  148. foreach ($lists AS $key => $item) {
  149. $lists[$key]['icon'] = Base::fillUrl('images/files/' . $item['type'] . '.png');
  150. }
  151. return Base::retSuccess('success', Base::list2Tree($lists, 'id', 'parentid'));
  152. }
  153. /**
  154. * 添加/修改章节
  155. *
  156. * @apiParam {Number} bookid 知识库数据ID
  157. * @apiParam {String} title 章节名称
  158. * @apiParam {String} type 章节类型
  159. */
  160. public function section__add()
  161. {
  162. $user = Users::authE();
  163. if (Base::isError($user)) {
  164. return $user;
  165. } else {
  166. $user = $user['data'];
  167. }
  168. //
  169. $bookid = intval(Request::input('bookid'));
  170. $bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
  171. if (empty($bookRow)) {
  172. return Base::retError('知识库不存在或已被删除!');
  173. }
  174. $count = DB::table('docs_section')->where('bookid', $bookid)->count();
  175. if ($count >= 500) {
  176. return Base::retError(['知识库章节已经超过最大限制(%)!', 500]);
  177. }
  178. //
  179. $id = intval(Request::input('id'));
  180. $title = trim(Request::input('title'));
  181. $type = trim(Request::input('type'));
  182. if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
  183. return Base::retError('标题限制2-100个字!');
  184. }
  185. if ($id > 0) {
  186. // 修改
  187. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  188. if (empty($row)) {
  189. return Base::retError('知识库不存在或已被删除!');
  190. }
  191. $data = [
  192. 'title' => $title,
  193. ];
  194. DB::table('docs_section')->where('id', $id)->update($data);
  195. return Base::retSuccess('修改成功!', $data);
  196. } else {
  197. // 添加
  198. if (!in_array($type, ['document', 'mind', 'sheet', 'flow', 'folder'])) {
  199. return Base::retError('参数错误!');
  200. }
  201. $parentid = 0;
  202. if ($id < 0) {
  203. $count = Base::DBC2A(DB::table('docs_section')->where('id', abs($id))->where('bookid', $bookid)->count());
  204. if ($count > 0) {
  205. $parentid = abs($id);
  206. }
  207. }
  208. $data = [
  209. 'bookid' => $bookid,
  210. 'parentid' => $parentid,
  211. 'username' => $user['username'],
  212. 'title' => $title,
  213. 'type' => $type,
  214. 'inorder' => intval(DB::table('docs_section')->select(['inorder'])->where('bookid', $bookid)->orderByDesc('inorder')->value('inorder')) + 1,
  215. 'indate' => Base::time(),
  216. ];
  217. $id = DB::table('docs_section')->insertGetId($data);
  218. if (empty($id)) {
  219. return Base::retError('系统繁忙,请稍后再试!');
  220. }
  221. $data['id'] = $id;
  222. return Base::retSuccess('添加成功!', $data);
  223. }
  224. }
  225. /**
  226. * 排序任务
  227. *
  228. * @apiParam {Number} bookid 知识库数据ID
  229. * @apiParam {String} oldsort 旧排序数据
  230. * @apiParam {String} newsort 新排序数据
  231. */
  232. public function section__sort()
  233. {
  234. $user = Users::authE();
  235. if (Base::isError($user)) {
  236. return $user;
  237. } else {
  238. $user = $user['data'];
  239. }
  240. //
  241. $bookid = intval(Request::input('bookid'));
  242. $bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
  243. if (empty($bookRow)) {
  244. return Base::retError('知识库不存在或已被删除!');
  245. }
  246. //
  247. $newSort = explode(";", Request::input('newsort'));
  248. if (count($newSort) == 0) {
  249. return Base::retError('参数错误!');
  250. }
  251. //
  252. $count = count($newSort);
  253. foreach ($newSort AS $sort => $item) {
  254. list($newId, $newParentid) = explode(':', $item);
  255. DB::table('docs_section')->where([
  256. 'id' => $newId,
  257. 'bookid' => $bookid
  258. ])->update([
  259. 'inorder' => $count - intval($sort),
  260. 'parentid' => $newParentid
  261. ]);
  262. }
  263. return Base::retSuccess('保存成功!');
  264. }
  265. /**
  266. * 删除章节
  267. *
  268. * @apiParam {Number} id 章节数据ID
  269. */
  270. public function section__delete()
  271. {
  272. $user = Users::authE();
  273. if (Base::isError($user)) {
  274. return $user;
  275. } else {
  276. $user = $user['data'];
  277. }
  278. //
  279. $id = intval(Request::input('id'));
  280. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  281. if (empty($row)) {
  282. return Base::retError('文档不存在或已被删除!');
  283. }
  284. DB::table('docs_section')->where('parentid', $id)->update([ 'parentid' => $row['parentid'] ]);
  285. DB::table('docs_section')->where('id', $id)->delete();
  286. DB::table('docs_content')->where('sid', $id)->delete();
  287. return Base::retSuccess('删除成功!');
  288. }
  289. /**
  290. * 获取章节内容
  291. *
  292. * @apiParam {Number|String} id 章节数据ID(或:章节数据ID-历史数据ID)
  293. */
  294. public function section__content()
  295. {
  296. $id = Request::input('id');
  297. $hid = 0;
  298. if (Base::strExists($id, '-')) {
  299. list($id, $hid) = explode("-", $id);
  300. }
  301. $id = intval($id);
  302. $hid = intval($hid);
  303. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  304. if (empty($row)) {
  305. return Base::retError('文档不存在或已被删除!');
  306. }
  307. $whereArray = [];
  308. if ($hid > 0) {
  309. $whereArray[] = ['id', '=', $hid];
  310. }
  311. $whereArray[] = ['sid', '=', $id];
  312. $cRow = Base::DBC2A(DB::table('docs_content')->select(['id AS hid', 'content'])->where($whereArray)->orderByDesc('id')->first());
  313. if (empty($cRow)) {
  314. $cRow = [ 'hid' => 0, 'content' => '' ];
  315. }
  316. return Base::retSuccess('success', array_merge($row, $cRow));
  317. }
  318. /**
  319. * 获取章节历史内容
  320. *
  321. * @apiParam {Number} id 章节数据ID
  322. */
  323. public function section__history()
  324. {
  325. $user = Users::authE();
  326. if (Base::isError($user)) {
  327. return $user;
  328. } else {
  329. $user = $user['data'];
  330. }
  331. //
  332. $id = intval(Request::input('id'));
  333. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  334. if (empty($row)) {
  335. return Base::retError('文档不存在或已被删除!');
  336. }
  337. //
  338. $lists = Base::DBC2A(DB::table('docs_content')
  339. ->where('sid', $id)
  340. ->orderByDesc('id')
  341. ->take(50)
  342. ->get());
  343. if (count($lists) <= 1) {
  344. return Base::retError('暂无历史数据');
  345. }
  346. return Base::retSuccess('success', $lists);
  347. }
  348. /**
  349. * 保存章节内容
  350. *
  351. * @apiParam {Number} id 章节数据ID
  352. * @apiParam {Object} [D] Request Payload 提交
  353. * - content: 内容
  354. */
  355. public function section__save()
  356. {
  357. $user = Users::authE();
  358. if (Base::isError($user)) {
  359. return $user;
  360. } else {
  361. $user = $user['data'];
  362. }
  363. //
  364. $id = intval(Request::input('id'));
  365. $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
  366. if (empty($row)) {
  367. return Base::retError('文档不存在或已被删除!');
  368. }
  369. $D = Base::getContentsParse('D');
  370. DB::table('docs_content')->insert([
  371. 'bookid' => $row['bookid'],
  372. 'sid' => $id,
  373. 'content' => $D['content'],
  374. 'username' => $user['username'],
  375. 'indate' => Base::time()
  376. ]);
  377. //通知正在编辑的成员
  378. $sid = $id;
  379. $array = Base::json2array(Cache::get("docs::" . $sid));
  380. if ($array) {
  381. foreach ($array as $uname => $vbody) {
  382. if (intval($vbody['indate']) + 20 < time()) {
  383. unset($array[$uname]);
  384. }
  385. }
  386. }
  387. $pushLists = [];
  388. foreach ($array AS $tuser) {
  389. $uLists = Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where('username', $tuser['username'])->get());
  390. foreach ($uLists AS $item) {
  391. if ($item['username'] == $user['username']) {
  392. continue;
  393. }
  394. $pushLists[] = [
  395. 'fd' => $item['fd'],
  396. 'msg' => [
  397. 'messageType' => 'docs',
  398. 'body' => [
  399. 'type' => 'update',
  400. 'sid' => $sid,
  401. 'nickname' => $user['nickname'] ?: $user['username'],
  402. 'time' => time(),
  403. ]
  404. ]
  405. ];
  406. }
  407. }
  408. $pushTask = new PushTask($pushLists);
  409. Task::deliver($pushTask);
  410. //
  411. return Base::retSuccess('保存成功!');
  412. }
  413. }