From 6da68f09cd4c71e39a75463ae7382d101f65244f Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Tue, 14 Apr 2020 20:31:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9D=83=E9=99=90=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/Permission.php | 21 +++++++++++++++++++-- catch/permissions/model/Permissions.php | 8 +++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/catch/permissions/controller/Permission.php b/catch/permissions/controller/Permission.php index 68aa133..9c058bf 100644 --- a/catch/permissions/controller/Permission.php +++ b/catch/permissions/controller/Permission.php @@ -22,12 +22,29 @@ 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 { - return CatchResponse::success(Tree::done($this->permissions->getList())); + // 获取菜单类型 + $menuList = $this->permissions->getList(true); + + // 获取按钮类型并且重新排列 + $buttonList = []; + $this->permissions + ->whereIn('parent_id', array_unique($menuList->column('id'))) + ->where('type', Permissions::BTN_TYPE) + ->select()->each(function ($item) use (&$buttonList){ + $buttonList[$item['parent_id']][] = $item->toArray(); + }); + + // 返回树结构 + return CatchResponse::success(Tree::done($menuList->each(function (&$item) use ($buttonList){ + $item['actionList'] = $buttonList[$item['id']] ?? []; + })->toArray())); } /** diff --git a/catch/permissions/model/Permissions.php b/catch/permissions/model/Permissions.php index 6ec89be..8489a7d 100644 --- a/catch/permissions/model/Permissions.php +++ b/catch/permissions/model/Permissions.php @@ -39,13 +39,15 @@ class Permissions extends CatchModel public const PUT = 'put'; public const DELETE = 'delete'; - public function getList() + public function getList($isMenu = false) { return $this->catchSearch() ->order('sort', 'desc') ->order('id', 'desc') - ->select() - ->toArray(); + ->when($isMenu, function ($query){ + $query->where('type', self::MENU_TYPE); + }) + ->select(); } public function roles(): \think\model\relation\BelongsToMany