From e7491555e85838fb394080c2aaaa0ed02e462d69 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 22 Apr 2020 20:17:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/Permission.php | 16 +++++++++---- catch/permissions/model/Permissions.php | 26 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/catch/permissions/controller/Permission.php b/catch/permissions/controller/Permission.php index 4e8249d..da1530e 100644 --- a/catch/permissions/controller/Permission.php +++ b/catch/permissions/controller/Permission.php @@ -22,12 +22,13 @@ class Permission extends CatchController /** * * @time 2019年12月11日 + * @param Request $request * @return Json * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DataNotFoundException */ - public function index(): Json + public function index(Request $request): Json { // 获取菜单类型 $menuList = $this->permissions->getList(true); @@ -41,9 +42,11 @@ class Permission extends CatchController $buttonList[$item['parent_id']][] = $item->toArray(); }); + // 子节点的 key + $children = $request->param('actionList') ?? 'children'; // 返回树结构 - return CatchResponse::success(Tree::done($menuList->each(function (&$item) use ($buttonList){ - $item['children'] = $buttonList[$item['id']] ?? []; + return CatchResponse::success(Tree::done($menuList->each(function (&$item) use ($buttonList, $children){ + $item[$children] = $buttonList[$item['id']] ?? []; })->toArray())); } @@ -81,6 +84,11 @@ class Permission extends CatchController { $permission = $this->permissions->findBy($id); + $params = array_merge($request->param(), [ + 'parent_id' => $permission->parent_id, + 'level' => $permission->level + ]); + // 如果是父分类需要更新所有子分类的模块 if (!$permission->parent_id) { $this->permissions->updateBy($permission->parent_id, [ @@ -88,7 +96,7 @@ class Permission extends CatchController ], 'parent_id'); } - return CatchResponse::success($this->permissions->updateBy($id, $request->param())); + return CatchResponse::success($this->permissions->updateBy($id, $params)); } /** diff --git a/catch/permissions/model/Permissions.php b/catch/permissions/model/Permissions.php index 0452999..bbd9d64 100644 --- a/catch/permissions/model/Permissions.php +++ b/catch/permissions/model/Permissions.php @@ -3,6 +3,7 @@ namespace catchAdmin\permissions\model; use catchAdmin\permissions\model\search\PermissionsSearch; use catcher\base\CatchModel; +use think\Model; class Permissions extends CatchModel { @@ -74,4 +75,29 @@ class Permissions extends CatchModel ]) ->select(); } + + /** + * 插入后回调 更新 level + * + * @time 2020年04月22日 + * @param Model $model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return array|bool|Model|void|null + */ + public static function onAfterInsert(Model $model) + { + $model = self::where('id', $model->id)->find(); + + if ($model && $model->parent_id) { + $parent = self::where('id', $model->parent_id)->find(); + $level = $parent->level ? $parent->level . '-' . $parent->id : $parent->id; + return $model->where('id', $model->id)->update([ + 'level' => $level + ]); + } + + return true; + } }