From e3e65775f48cf27b88a38e4d1fcce56ac444fdb4 Mon Sep 17 00:00:00 2001 From: chenlong Date: Wed, 11 Oct 2023 15:22:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/Admin/NotebookController.php | 4 +- Controller/Admin/NotebookUserController.php | 17 +++ Controller/Admin/SharePreviewController.php | 34 ++++++ Enums/NotebookUserEnumStatus.php | 48 --------- Model/Notebook.php | 8 +- Model/NotebookUser.php | 8 +- Service/Admin/NotebookService.php | 82 +++++++++++++- Service/Admin/NotebookUserService.php | 41 ++++++- View/Admin/Notebook/lists.blade.php | 114 +++++++++++++++++--- View/Admin/Notebook/share.sc.php | 40 +++++++ 10 files changed, 326 insertions(+), 70 deletions(-) create mode 100644 Controller/Admin/SharePreviewController.php delete mode 100644 Enums/NotebookUserEnumStatus.php create mode 100644 View/Admin/Notebook/share.sc.php diff --git a/Controller/Admin/NotebookController.php b/Controller/Admin/NotebookController.php index ad9ee9d..6d69813 100644 --- a/Controller/Admin/NotebookController.php +++ b/Controller/Admin/NotebookController.php @@ -64,10 +64,12 @@ class NotebookController extends AbstractController public function store(NotebookRequest $notebookRequest): ResponseInterface { $model = NotebookService::aop()->store($notebookRequest->post()); - $data = $model->setVisible(['id', "title", "content", "create_time", 'update_time'])->toArray(); + $data = $model->setVisible(['id', "title", "share_key", "content", 'mode', "create_time", 'update_time'])->toArray(); $data['create_name'] = $model->createAdmin->name; $data['latest_name'] = $model->latestAdmin->name; + $data['user_mode'] = 'super'; + $data['user'] = []; return Response::json()->success($data); } diff --git a/Controller/Admin/NotebookUserController.php b/Controller/Admin/NotebookUserController.php index e935d1f..ba998f5 100644 --- a/Controller/Admin/NotebookUserController.php +++ b/Controller/Admin/NotebookUserController.php @@ -4,6 +4,7 @@ namespace Plugins\Notebook\Controller\Admin; use App\Exception\ApiMessageException; use App\Controller\AbstractController; +use Hyperf\Validation\Middleware\ValidationMiddleware; use Plugins\Notebook\Service\Admin\NotebookUserService; use App\Middleware\Admin\AuthenticateMiddleware; use App\Middleware\Admin\PermissionsMiddleware; @@ -49,4 +50,20 @@ class NotebookUserController extends AbstractController return Response::json()->success(); } + /** + * 删除数据 + * + * @return ResponseInterface + * @throws ApiMessageException + */ + #[Scene('destroy')] + #[PostMapping(path: 'destroy')] + public function destroy(): ResponseInterface + { + NotebookUserService::aop()->destroy($this->request->post('ids')); + + return Response::json()->success(); + } + + } diff --git a/Controller/Admin/SharePreviewController.php b/Controller/Admin/SharePreviewController.php new file mode 100644 index 0000000..a875ac6 --- /dev/null +++ b/Controller/Admin/SharePreviewController.php @@ -0,0 +1,34 @@ +query('key'))->first(); + + return $render->render('plugins.Notebook.Admin.Notebook.share', [ + 'notebook' => $notebook + ]); + } +} \ No newline at end of file diff --git a/Enums/NotebookUserEnumStatus.php b/Enums/NotebookUserEnumStatus.php deleted file mode 100644 index de4a4ee..0000000 --- a/Enums/NotebookUserEnumStatus.php +++ /dev/null @@ -1,48 +0,0 @@ - '正常', - self::Eliminate => '剔除', - }; - } - - /** - * 转标签 - */ - public function toTag():AbstractHtmlElement - { - return match ($this){ - self::Normal => $this->getTag('success', 'light'), - self::Eliminate => $this->getTag('warning', 'light'), - }; - } -} diff --git a/Model/Notebook.php b/Model/Notebook.php index 9d4ba67..c8b9b29 100644 --- a/Model/Notebook.php +++ b/Model/Notebook.php @@ -25,7 +25,7 @@ CREATE TABLE `sd_notebook` ( `title` varchar(127) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '标题', `content` text COLLATE utf8mb4_general_ci COMMENT '内容', `administrators_id` int NOT NULL COMMENT '创建人', - `mode` tinyint(1) NOT NULL DEFAULT '1' COMMENT '模式:1=公开,2=私有', + `mode` tinyint(1) NOT NULL DEFAULT '2' COMMENT '模式:1=公开,2=私有', `share_key` varchar(10) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '分享密钥', `latest_administrators_id` int NOT NULL COMMENT '最后修改人', `create_time` datetime NOT NULL COMMENT '创建时间', @@ -68,7 +68,11 @@ class Notebook extends Model public function user() { - return $this->hasMany(NotebookUser::class); + return $this->hasMany(NotebookUser::class) + ->join('administrators', 'administrators.id', '=', "notebook_user.administrators_id") + ->select([ + "notebook_user.id","notebook_user.notebook_id","notebook_user.administrators_id","notebook_user.mode","notebook_user.status","notebook_user.create_time","administrators.name as user" + ]); } } diff --git a/Model/NotebookUser.php b/Model/NotebookUser.php index c596513..5acd378 100644 --- a/Model/NotebookUser.php +++ b/Model/NotebookUser.php @@ -11,10 +11,10 @@ use App\Model\Model; * @property int $notebook_id 笔记 * @property int $administrators_id 管理员 * @property int $mode 权限 - * @property int $status 状态 * @property string $create_time * @property string $update_time * @property int $delete_time + * @property Notebook $notebook */ #[CreateSql(<<attributes['administrators_id'] = (int)$value; } + + public function notebook() + { + return $this->belongsTo(Notebook::class); + } } diff --git a/Service/Admin/NotebookService.php b/Service/Admin/NotebookService.php index 0076398..f6f790b 100644 --- a/Service/Admin/NotebookService.php +++ b/Service/Admin/NotebookService.php @@ -10,6 +10,10 @@ use App\Service\Admin\Traits\StoreServiceTrait; use App\Service\Admin\Traits\UpdateServiceTrait; use App\Service\Admin\Traits\SwitchServiceTrait; use App\Util\AdminAuth; +use App\Util\Hy; +use Hyperf\Database\Query\JoinClause; +use Plugins\Notebook\Enums\NotebookEnumMode; +use Plugins\Notebook\Enums\NotebookUserEnumMode; use Plugins\Notebook\Model\Notebook; use App\Util\EasySearch; use Plugins\Notebook\Model\NotebookUser; @@ -34,27 +38,95 @@ class NotebookService extends AbstractAdminService $query = $this->model ->leftJoin('administrators as a1', 'a1.id', '=', 'notebook.administrators_id') ->leftJoin('administrators as a2', 'a2.id', '=', 'notebook.latest_administrators_id') + ->leftJoin('notebook_user', function (JoinClause $clause){ + $clause->on('notebook_user.notebook_id', '=', 'notebook.id') + ->where('notebook_user.administrators_id', AdminAuth::getInfo()->id) + ->whereNull('notebook_user.delete_time'); + }) + ->where(function ($builder){ + $builder->orWhere('notebook.administrators_id', AdminAuth::getInfo()->id) + ->orWhere('notebook_user.administrators_id', AdminAuth::getInfo()->id) + ->orWhere('notebook.mode', NotebookEnumMode::Public->value); + }) + ->orderBy('notebook.id', 'desc') ->with(['user']) - ->select(["notebook.id","notebook.title","notebook.content","notebook.mode","notebook.share_key","a1.name as create_name","a2.name as latest_name","notebook.create_time","notebook.update_time"]); + ->select(["notebook.id","notebook.administrators_id","notebook.title","notebook_user.mode as user_mode","notebook.content","notebook.mode","notebook.share_key","a1.name as create_name","a2.name as latest_name","notebook.create_time","notebook.update_time"]); - return EasySearch::create($query)->getData(); + $data = EasySearch::create($query)->getData(); + + $data['data']->each(function ($datum) { + if ($datum->administrators_id === AdminAuth::getInfo()->id) { + $datum->user_mode = "super"; + } elseif ($datum->user_mode === NotebookUserEnumMode::ReadingAndWriting->value) { + $datum->user_mode = "write"; + } else { + $datum->user_mode = "read"; + } + }); + + return $data; } + /** + * @param $data + * + * @return void + */ public function beforeStore(&$data): void { $data['administrators_id'] = AdminAuth::getInfo()->id; + $data['mode'] = NotebookEnumMode::Private->value; + $data['share_key'] = substr(md5(time()), 0, 10); } + /** + * @param $data + * @param $id + * + * @return void + */ public function beforeWrite(&$data, $id = null): void { $data['latest_administrators_id'] = AdminAuth::getInfo()->id; } + /** + * @param array $ids + * + * @return void + */ + public function beforeDestroy(array $ids) + { + $this->model->where('administrators_id', AdminAuth::getInfo()->id); + } + + /** + * @param $data + * @param $id + * + * @return void + * @throws ApiMessageException + */ public function beforeUpdate(&$data, $id): void { if (strtotime($data['update_time']) < strtotime($this->model->update_time)){ throw new ApiMessageException("文档内容不是最新版,请更新后编辑"); } + + if ($this->model->administrators_id === AdminAuth::getInfo()->id) { + return; + } + if ($data['mode'] != $this->model->mode){ + throw new ApiMessageException("对不起,你无修改权限"); + } + + $userMode = NotebookUser::where('notebook_id', $id) + ->where('administrators_id', AdminAuth::getInfo()->id) + ->value('mode'); + + if ($userMode !== NotebookUserEnumMode::ReadingAndWriting->value) { + throw new ApiMessageException("对不起,你暂无修改权限"); + } } /** @@ -85,6 +157,12 @@ class NotebookService extends AbstractAdminService */ public function addUser(int $notebook_id, array $userIds): void { + $notebook = $this->model->find($notebook_id); + + if ($notebook?->administrators_id != AdminAuth::getInfo()->id) { + throw new ApiMessageException("对不起,未找到该操作"); + } + $data = []; foreach ($userIds as $user_id) { $data[] = [ diff --git a/Service/Admin/NotebookUserService.php b/Service/Admin/NotebookUserService.php index 0c22e91..32ab769 100644 --- a/Service/Admin/NotebookUserService.php +++ b/Service/Admin/NotebookUserService.php @@ -3,11 +3,15 @@ namespace Plugins\Notebook\Service\Admin; +use App\Exception\ApiMessageException; use App\Service\Admin\AbstractAdminService; use App\Service\Admin\Traits\DestroyServiceTrait; use App\Service\Admin\Traits\StoreServiceTrait; use App\Service\Admin\Traits\UpdateServiceTrait; use App\Service\Admin\Traits\SwitchServiceTrait; +use App\Util\AdminAuth; +use App\Util\Hy; +use Plugins\Notebook\Model\Notebook; use Plugins\Notebook\Model\NotebookUser; use App\Util\EasySearch; @@ -30,7 +34,42 @@ class NotebookUserService extends AbstractAdminService { $query = $this->model->select(["notebook_user.id","notebook_user.notebook_id","notebook_user.administrators_id","notebook_user.mode","notebook_user.status","notebook_user.create_time"]); - return EasySearch::create($query)->getData(); + if (Hy::request()->query('notebook_id')) { + $query->where("notebook_user.notebook_id", Hy::request()->query('notebook_id')); + } + + return EasySearch::create($query)->totalCancellations()->getData()['data']->toArray(); + } + + /** + * @param $data + * @param $id + * + * @return void + * @throws ApiMessageException + */ + public function beforeUpdate(&$data, $id): void + { + if ($this->model->notebook->administrators_id != AdminAuth::getInfo()->id) { + throw new ApiMessageException('对不起,未找到该操作'); + } } + /** + * @param array $ids + * + * @return void + * @throws ApiMessageException + */ + public function beforeDestroy(array $ids): void + { + $data = NotebookUser::whereIn('id', $ids)->pluck('notebook_id'); + $notAuth = Notebook::whereIn('id', $data->toArray()) + ->where('administrators_id', '<>', AdminAuth::getInfo()->id) + ->value('id'); + + if ($notAuth) { + throw new ApiMessageException('对不起,未找到该操作'); + } + } } diff --git a/View/Admin/Notebook/lists.blade.php b/View/Admin/Notebook/lists.blade.php index 588613c..4021936 100644 --- a/View/Admin/Notebook/lists.blade.php +++ b/View/Admin/Notebook/lists.blade.php @@ -136,6 +136,25 @@ width: 100%; user-select: none; } + .delete-user{ + color: red; + } + .delete-user:hover{ + cursor: pointer; + } + .readonly{ + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 110px; + z-index: 99; + background-color: #f8e3c535; + color: #f89898; + font-size: 30px; + line-height: 110px; + text-align: center; + } @endsection @section('main') @@ -160,10 +179,10 @@
创建时间:@{{item.create_time}}
创建人:@{{ item.create_name }}
最后更新: @{{ item.latest_name }}
-
+
-
-
+
+
@@ -180,12 +199,14 @@
+
只读模式,任何修改都无效
{{-- 分享 --}} - @{{ shareUrl }} + @{{ shareUrl }} + {{-- 模式 --}} @@ -202,8 +223,8 @@ - {{-- 允许查看 --}} - + {{-- 协作用户 --}} +
+ + +
@@ -256,7 +282,7 @@ VueInit.data.allowVisible = false; VueInit.data.modeData = {!! json_encode(\Plugins\Notebook\Enums\NotebookEnumMode::mapping()) !!}; VueInit.data.adminData = {!! json_encode(\App\Model\Base\Administrators::getSelectData('name')) !!}; - VueInit.data.adminData.push({value:"*",label:'全部用户'}) + VueInit.data.adminData.unshift({value:"*",label:'全部用户'}) VueInit.data.shareUrl = ""; VueInit.data.updateNotebook = ""; VueInit.data.allowUserData = []; @@ -282,17 +308,69 @@ this.updateNotebook = item; }; + VueInit.methods.deleteUser = function (row, index) { + axios({ + url: "{{ \App\Util\Hy::route()->to([\Plugins\Notebook\Controller\Admin\NotebookUserController::class, 'destroy']) }}", + method: 'post', + data: { + ids: [row.id] + }, + }).then(({ data }) => { + if (data.code !== 200) { + this.$message.error(data.msg); + }else{ + this.$message.success('更新成功'); + this.getNotebookUsers(this.updateNotebook.id, (data) => { + this.updateNotebook.user = data; + }); + } + }); + }; + + VueInit.methods.modeChange = function (row) { + axios({ + url: "{{ \App\Util\Hy::route()->to([\Plugins\Notebook\Controller\Admin\NotebookUserController::class, 'update']) }}", + method: 'post', + data: row, + }).then(({ data }) => { + if (data.code !== 200) { + row.mode = row.mode === 1 ? 2 : 1; + this.$message.error(data.msg); + }else{ + this.$message.success('更新成功'); + } + }); + }; + + VueInit.methods.getNotebookUsers = function (notebook_id, success){ + axios({ + url: "{{ \App\Util\Hy::route()->to([\Plugins\Notebook\Controller\Admin\NotebookUserController::class, 'listsData']) }}", + method: 'get', + params: { + notebook_id:notebook_id + }, + }).then(({ data }) => { + if (data.code === 200) { + success && success(data.data) + } + }); + } + VueInit.methods.addAllowUser = function () { if (this.waitAddAllowUser.length > 0) { let waitAddAllowUser = []; + if (this.waitAddAllowUser.includes('*')) { + this.waitAddAllowUser = this.adminData.map(v => v.value); + } + this.waitAddAllowUser.map(v =>{ - if (!this.updateNotebook.map(v1 => v1.administrators_id).includes(v)) { + if (!this.updateNotebook.user.map(v1 => v1.administrators_id).includes(v) && v !== '*') { waitAddAllowUser.push(v); } - }) + }); this.waitAddAllowUser = []; - if (waitAddAllowUser) { + if (waitAddAllowUser.length > 0) { this.addAllowUserRequest(waitAddAllowUser); } } @@ -309,7 +387,9 @@ }).then(({data}) =>{ if (data.code === 200) { this.$message.success("设置成功"); - // todo getNewData + this.getNotebookUsers(this.updateNotebook.id, (data) => { + this.updateNotebook.user = data; + }) }else{ this.$message.error("设置失败"); } @@ -368,7 +448,9 @@ } VueInit.methods.deleteaa = function (item, index){ - this.$confirm("确认删除笔记吗?").then(() =>{ + this.$confirm("确认删除笔记吗?", { + confirm:"确认" + }).then(() =>{ this.notebook.splice(index, 1); if (index === this.active) { this.currentNotebook = this.notebook[index] ? this.notebook[index] : {title: ''}; @@ -420,6 +502,10 @@ } data = data ? data : this.currentNotebook; + if (data.user_mode === 'read') { + return; + } + data.update_time = this.toTime(); axios({ url: "update", @@ -506,7 +592,7 @@ VueInit.methods.share = function (item,index){ this.shareVisible = true; - this.shareUrl = `http://www.baidu.com/${item.id}`; + this.shareUrl = `{{ \App\Util\Hy::route()->to([\Plugins\Notebook\Controller\Admin\SharePreviewController::class, 'index']) }}?key=${item.share_key}`; } diff --git a/View/Admin/Notebook/share.sc.php b/View/Admin/Notebook/share.sc.php new file mode 100644 index 0000000..a630165 --- /dev/null +++ b/View/Admin/Notebook/share.sc.php @@ -0,0 +1,40 @@ +title ?: '不存在的页面'); +if (!$notebook) { + Html::loadThemeResource('ElementUI'); +} + +Html::css()->addCss(<<content ?: ' ';