|
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use App\Module\Base;
|
|
|
+use App\Module\Docs;
|
|
|
use App\Module\Users;
|
|
|
use App\Tasks\PushTask;
|
|
|
use Cache;
|
|
@@ -43,6 +44,18 @@ class DocsController extends Controller
|
|
|
}
|
|
|
//
|
|
|
$lists = DB::table('docs_book')
|
|
|
+ ->where('role_edit', 'reg')
|
|
|
+ ->orWhere(function ($query) use ($user) {
|
|
|
+ $query->where('role_edit', 'private')->where('username', $user['username']);
|
|
|
+ })
|
|
|
+ ->orWhere(function ($query) use ($user) {
|
|
|
+ $query->where('role_edit', 'member')->whereIn('id', function ($query2) use ($user) {
|
|
|
+ $query2->select('bookid')
|
|
|
+ ->from('docs_users')
|
|
|
+ ->where('username', $user['username'])
|
|
|
+ ->whereRaw(env('DB_PREFIX') . 'docs_book.id = bookid');
|
|
|
+ });
|
|
|
+ })
|
|
|
->orderByDesc('id')
|
|
|
->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
|
|
|
$lists = Base::getPageList($lists);
|
|
@@ -55,6 +68,7 @@ class DocsController extends Controller
|
|
|
/**
|
|
|
* 添加/修改知识库
|
|
|
*
|
|
|
+ * @apiParam {Number} id 知识库数据ID
|
|
|
* @apiParam {String} title 知识库名称
|
|
|
*/
|
|
|
public function book__add()
|
|
@@ -68,6 +82,10 @@ class DocsController extends Controller
|
|
|
//
|
|
|
$id = intval(Request::input('id'));
|
|
|
$title = trim(Request::input('title'));
|
|
|
+ $role = Docs::checkRole($id, 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
if (mb_strlen($title) < 2 || mb_strlen($title) > 100) {
|
|
|
return Base::retError('标题限制2-100个字!');
|
|
|
}
|
|
@@ -77,9 +95,6 @@ class DocsController extends Controller
|
|
|
if (empty($row)) {
|
|
|
return Base::retError('知识库不存在或已被删除!');
|
|
|
}
|
|
|
- if ($row['username'] != $user['username']) {
|
|
|
- return Base::retError('此操作仅限知识库负责人!');
|
|
|
- }
|
|
|
$data = [
|
|
|
'title' => $title,
|
|
|
];
|
|
@@ -102,6 +117,48 @@ class DocsController extends Controller
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 设置知识库
|
|
|
+ *
|
|
|
+ * @apiParam {Number} id 知识库数据ID
|
|
|
+ * @apiParam {String} role_edit
|
|
|
+ * @apiParam {String} role_view
|
|
|
+ */
|
|
|
+ public function book__setting()
|
|
|
+ {
|
|
|
+ $user = Users::authE();
|
|
|
+ if (Base::isError($user)) {
|
|
|
+ return $user;
|
|
|
+ } else {
|
|
|
+ $user = $user['data'];
|
|
|
+ }
|
|
|
+ //
|
|
|
+ $id = intval(Request::input('id'));
|
|
|
+ $role = Docs::checkRole($id, 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
+ $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
|
|
|
+ if (empty($row)) {
|
|
|
+ return Base::retError('知识库不存在或已被删除!');
|
|
|
+ }
|
|
|
+ $setting = Base::string2array($row['setting']);
|
|
|
+ $type = trim(Request::input('type'));
|
|
|
+ if ($type == 'save') {
|
|
|
+ foreach (Request::input() AS $key => $value) {
|
|
|
+ if (in_array($key, ['role_edit', 'role_view'])) {
|
|
|
+ $setting[$key] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DB::table('docs_book')->where('id', $id)->update([
|
|
|
+ 'role_edit' => $setting['role_edit'],
|
|
|
+ 'role_view' => $setting['role_view'],
|
|
|
+ 'setting' => Base::array2string($setting),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ return Base::retSuccess($type == 'save' ? '修改成功!' : 'success', $setting ?: json_decode('{}'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 删除知识库
|
|
|
*
|
|
|
* @apiParam {Number} id 知识库数据ID
|
|
@@ -130,11 +187,13 @@ class DocsController extends Controller
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 章节列表
|
|
|
+ * 成员-列表
|
|
|
*
|
|
|
- * @apiParam {Number} bookid 知识库数据ID
|
|
|
+ * @apiParam {Number} id 知识库数据ID
|
|
|
+ * @apiParam {Number} [page] 当前页,默认:1
|
|
|
+ * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
|
|
|
*/
|
|
|
- public function section__lists()
|
|
|
+ public function users__lists()
|
|
|
{
|
|
|
$user = Users::authE();
|
|
|
if (Base::isError($user)) {
|
|
@@ -143,8 +202,120 @@ class DocsController extends Controller
|
|
|
$user = $user['data'];
|
|
|
}
|
|
|
//
|
|
|
+ $id = intval(Request::input('id'));
|
|
|
+ $role = Docs::checkRole($id, 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
+ $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
|
|
|
+ if (empty($row)) {
|
|
|
+ return Base::retError('知识库不存在或已被删除!');
|
|
|
+ }
|
|
|
+ //
|
|
|
+ $lists = DB::table('docs_book')
|
|
|
+ ->join('docs_users', 'docs_book.id', '=', 'docs_users.bookid')
|
|
|
+ ->select(['docs_book.title', 'docs_users.*'])
|
|
|
+ ->where([
|
|
|
+ ['docs_book.id', $id],
|
|
|
+ ])
|
|
|
+ ->orderByDesc('docs_users.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
|
|
|
+ $lists = Base::getPageList($lists);
|
|
|
+ if ($lists['total'] == 0) {
|
|
|
+ return Base::retError('未找到任何相关的成员');
|
|
|
+ }
|
|
|
+ foreach ($lists['lists'] AS $key => $item) {
|
|
|
+ $userInfo = Users::username2basic($item['username']);
|
|
|
+ $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
|
|
|
+ $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
|
|
|
+ $lists['lists'][$key]['profession'] = $userInfo['profession'];
|
|
|
+ }
|
|
|
+ return Base::retSuccess('success', $lists);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 成员-添加、删除
|
|
|
+ *
|
|
|
+ * @apiParam {String} act
|
|
|
+ * - delete: 删除成员
|
|
|
+ * - else: 添加成员
|
|
|
+ * @apiParam {Number} id 知识库数据ID
|
|
|
+ * @apiParam {Array|String} username 用户名(或用户名组)
|
|
|
+ */
|
|
|
+ public function users__join()
|
|
|
+ {
|
|
|
+ $user = Users::authE();
|
|
|
+ if (Base::isError($user)) {
|
|
|
+ return $user;
|
|
|
+ } else {
|
|
|
+ $user = $user['data'];
|
|
|
+ }
|
|
|
+ //
|
|
|
+ $id = intval(Request::input('id'));
|
|
|
+ $role = Docs::checkRole($id, 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
+ $row = Base::DBC2A(DB::table('docs_book')->where('id', $id)->first());
|
|
|
+ if (empty($row)) {
|
|
|
+ return Base::retError('知识库不存在或已被删除!');
|
|
|
+ }
|
|
|
+ //
|
|
|
+ $usernames = Request::input('username');
|
|
|
+ if (empty($usernames)) {
|
|
|
+ return Base::retError('参数错误!');
|
|
|
+ }
|
|
|
+ if (!is_array($usernames)) {
|
|
|
+ if (Base::strExists($usernames, ',')) {
|
|
|
+ $usernames = explode(',', $usernames);
|
|
|
+ } else {
|
|
|
+ $usernames = [$usernames];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //
|
|
|
+ foreach ($usernames AS $username) {
|
|
|
+ $inRow = Base::DBC2A(DB::table('docs_users')->where(['bookid' => $id, 'username' => $username])->first());
|
|
|
+ switch (Request::input('act')) {
|
|
|
+ case 'delete': {
|
|
|
+ if ($inRow) {
|
|
|
+ DB::table('docs_users')->where([
|
|
|
+ 'bookid' => $id,
|
|
|
+ 'username' => $username
|
|
|
+ ])->delete();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+ if (!$inRow && $username != $user['username']) {
|
|
|
+ DB::table('docs_users')->insert([
|
|
|
+ 'bookid' => $id,
|
|
|
+ 'username' => $username,
|
|
|
+ 'indate' => Base::time()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Base::retSuccess('操作完成!');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 章节列表
|
|
|
+ *
|
|
|
+ * @apiParam {String} act 请求方式,用于判断权限
|
|
|
+ * - edit: 管理页请求
|
|
|
+ * - view: 阅读页请求
|
|
|
+ * @apiParam {Number} bookid 知识库数据ID
|
|
|
+ */
|
|
|
+ public function section__lists()
|
|
|
+ {
|
|
|
+ $bookid = intval(Request::input('bookid'));
|
|
|
+ $role = Docs::checkRole($bookid, Request::input('act'));
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
$lists = Base::DBC2A(DB::table('docs_section')
|
|
|
- ->where('bookid', intval(Request::input('bookid')))
|
|
|
+ ->where('bookid', $bookid)
|
|
|
->orderByDesc('inorder')
|
|
|
->orderByDesc('id')
|
|
|
->take(500)
|
|
@@ -155,7 +326,11 @@ class DocsController extends Controller
|
|
|
foreach ($lists AS $key => $item) {
|
|
|
$lists[$key]['icon'] = Base::fillUrl('images/files/' . $item['type'] . '.png');
|
|
|
}
|
|
|
- return Base::retSuccess('success', Base::list2Tree($lists, 'id', 'parentid'));
|
|
|
+ $bookDetail = Base::DBC2A(DB::table('docs_book')->select(['title'])->where('id', $bookid)->first());
|
|
|
+ return Base::retSuccess('success', [
|
|
|
+ 'book' => $bookDetail ?: json_decode('{}'),
|
|
|
+ 'tree' => Base::list2Tree($lists, 'id', 'parentid')
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -175,6 +350,10 @@ class DocsController extends Controller
|
|
|
}
|
|
|
//
|
|
|
$bookid = intval(Request::input('bookid'));
|
|
|
+ $role = Docs::checkRole($bookid, 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
$bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
|
|
|
if (empty($bookRow)) {
|
|
|
return Base::retError('知识库不存在或已被删除!');
|
|
@@ -232,7 +411,7 @@ class DocsController extends Controller
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 排序任务
|
|
|
+ * 排序章节
|
|
|
*
|
|
|
* @apiParam {Number} bookid 知识库数据ID
|
|
|
* @apiParam {String} oldsort 旧排序数据
|
|
@@ -248,6 +427,10 @@ class DocsController extends Controller
|
|
|
}
|
|
|
//
|
|
|
$bookid = intval(Request::input('bookid'));
|
|
|
+ $role = Docs::checkRole($bookid, 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
$bookRow = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
|
|
|
if (empty($bookRow)) {
|
|
|
return Base::retError('知识库不存在或已被删除!');
|
|
@@ -291,6 +474,10 @@ class DocsController extends Controller
|
|
|
if (empty($row)) {
|
|
|
return Base::retError('文档不存在或已被删除!');
|
|
|
}
|
|
|
+ $role = Docs::checkRole($row['bookid'], 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
DB::table('docs_section')->where('parentid', $id)->update([ 'parentid' => $row['parentid'] ]);
|
|
|
DB::table('docs_section')->where('id', $id)->delete();
|
|
|
DB::table('docs_content')->where('sid', $id)->delete();
|
|
@@ -300,7 +487,10 @@ class DocsController extends Controller
|
|
|
/**
|
|
|
* 获取章节内容
|
|
|
*
|
|
|
- * @apiParam {Number|String} id 章节数据ID(或:章节数据ID-历史数据ID)
|
|
|
+ * @apiParam {String} act 请求方式,用于判断权限
|
|
|
+ * - edit: 管理页请求
|
|
|
+ * - view: 阅读页请求
|
|
|
+ * @apiParam {Number|String} id 章节数据ID(或:章节数据ID-历史数据ID)
|
|
|
*/
|
|
|
public function section__content()
|
|
|
{
|
|
@@ -315,6 +505,10 @@ class DocsController extends Controller
|
|
|
if (empty($row)) {
|
|
|
return Base::retError('文档不存在或已被删除!');
|
|
|
}
|
|
|
+ $role = Docs::checkRole($row['bookid'], Request::input('act'));
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
$whereArray = [];
|
|
|
if ($hid > 0) {
|
|
|
$whereArray[] = ['id', '=', $hid];
|
|
@@ -346,6 +540,10 @@ class DocsController extends Controller
|
|
|
if (empty($row)) {
|
|
|
return Base::retError('文档不存在或已被删除!');
|
|
|
}
|
|
|
+ $role = Docs::checkRole($row['bookid'], 'view');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
//
|
|
|
$lists = Base::DBC2A(DB::table('docs_content')
|
|
|
->where('sid', $id)
|
|
@@ -379,6 +577,13 @@ class DocsController extends Controller
|
|
|
if (empty($row)) {
|
|
|
return Base::retError('文档不存在或已被删除!');
|
|
|
}
|
|
|
+ $role = Docs::checkRole($row['bookid'], 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
+ if ($row['lockdate'] + 60 > Base::time() && $row['lockname'] != $user['username']) {
|
|
|
+ return Base::retError(['已被会员【%】锁定!', Users::nickname($row['lockname'])]);
|
|
|
+ }
|
|
|
$D = Base::getContentsParse('D');
|
|
|
DB::table('docs_content')->insert([
|
|
|
'bookid' => $row['bookid'],
|
|
@@ -387,40 +592,56 @@ class DocsController extends Controller
|
|
|
'username' => $user['username'],
|
|
|
'indate' => Base::time()
|
|
|
]);
|
|
|
- //通知正在编辑的成员
|
|
|
- $sid = $id;
|
|
|
- $array = Base::json2array(Cache::get("docs::" . $sid));
|
|
|
- if ($array) {
|
|
|
- foreach ($array as $uname => $vbody) {
|
|
|
- if (intval($vbody['indate']) + 20 < time()) {
|
|
|
- unset($array[$uname]);
|
|
|
- }
|
|
|
- }
|
|
|
+ Docs::notice($id, [ 'type' => 'update' ]);
|
|
|
+ //
|
|
|
+ return Base::retSuccess('保存成功!');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存章节内容
|
|
|
+ *
|
|
|
+ * @apiParam {String} act
|
|
|
+ * - lock: 锁定
|
|
|
+ * - unlock: 解锁
|
|
|
+ * @apiParam {Number} id 章节数据ID
|
|
|
+ */
|
|
|
+ public function section__lock()
|
|
|
+ {
|
|
|
+ $user = Users::authE();
|
|
|
+ if (Base::isError($user)) {
|
|
|
+ return $user;
|
|
|
+ } else {
|
|
|
+ $user = $user['data'];
|
|
|
}
|
|
|
- $pushLists = [];
|
|
|
- foreach ($array AS $tuser) {
|
|
|
- $uLists = Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where('username', $tuser['username'])->get());
|
|
|
- foreach ($uLists AS $item) {
|
|
|
- if ($item['username'] == $user['username']) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- $pushLists[] = [
|
|
|
- 'fd' => $item['fd'],
|
|
|
- 'msg' => [
|
|
|
- 'messageType' => 'docs',
|
|
|
- 'body' => [
|
|
|
- 'type' => 'update',
|
|
|
- 'sid' => $sid,
|
|
|
- 'nickname' => $user['nickname'] ?: $user['username'],
|
|
|
- 'time' => time(),
|
|
|
- ]
|
|
|
- ]
|
|
|
- ];
|
|
|
- }
|
|
|
+ //
|
|
|
+ $id = intval(Request::input('id'));
|
|
|
+ $act = trim(Request::input('act'));
|
|
|
+ $row = Base::DBC2A(DB::table('docs_section')->where('id', $id)->first());
|
|
|
+ if (empty($row)) {
|
|
|
+ return Base::retError('文档不存在或已被删除!');
|
|
|
}
|
|
|
- $pushTask = new PushTask($pushLists);
|
|
|
- Task::deliver($pushTask);
|
|
|
+ $role = Docs::checkRole($row['bookid'], 'edit');
|
|
|
+ if (Base::isError($role)) {
|
|
|
+ return $role;
|
|
|
+ }
|
|
|
+ if ($row['lockdate'] + 60 > Base::time() && $row['lockname'] != $user['username']) {
|
|
|
+ return Base::retError(['已被会员【%】锁定!', Users::nickname($row['lockname'])]);
|
|
|
+ }
|
|
|
+ if ($act == 'lock') {
|
|
|
+ $upArray = [
|
|
|
+ 'lockname' => $user['username'],
|
|
|
+ 'lockdate' => Base::time(),
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $upArray = [
|
|
|
+ 'lockname' => '',
|
|
|
+ 'lockdate' => 0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ DB::table('docs_section')->where('id', $id)->update($upArray);
|
|
|
+ $upArray['type'] = $act;
|
|
|
+ Docs::notice($id, $upArray);
|
|
|
//
|
|
|
- return Base::retSuccess('保存成功!');
|
|
|
+ return Base::retSuccess($act == 'lock' ? '锁定成功' : '已解除锁定', $upArray);
|
|
|
}
|
|
|
}
|