From 5a4070deb7fe3f35282760b8affbd58d7d89c704 Mon Sep 17 00:00:00 2001 From: zhaoxiang Date: Fri, 9 Feb 2018 16:49:02 +0800 Subject: [PATCH] =?UTF-8?q?modified=20=E5=AE=8C=E6=88=90=E7=BB=84=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=EF=BC=8C=E7=BB=84=E5=88=A0=E9=99=A4=EF=BC=8C=E7=BB=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Auth.php | 84 +++++++++++++++++++++------ application/admin/controller/Base.php | 2 +- application/admin/controller/Menu.php | 2 +- 3 files changed, 67 insertions(+), 21 deletions(-) diff --git a/application/admin/controller/Auth.php b/application/admin/controller/Auth.php index 9b8e17d..71ffb38 100644 --- a/application/admin/controller/Auth.php +++ b/application/admin/controller/Auth.php @@ -1,6 +1,6 @@ */ @@ -31,7 +31,7 @@ class Auth extends Base { $where = []; - $listModel = (new ApiAuthGroup())->where($where); + $listModel = (new ApiAuthGroup())->where($where)->order('id', 'DESC'); $listInfo = $listModel->limit($start, $limit)->select(); $count = $listModel->count(); $listInfo = $this->buildArrFromObj($listInfo); @@ -42,6 +42,14 @@ class Auth extends Base { ]); } + /** + * 获取组所在权限列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + * @author zhaoxiang + */ public function getRuleList() { $groupId = $this->request->get('groupId', 0); @@ -49,45 +57,62 @@ class Auth extends Base { $list = $this->buildArrFromObj($list); $list = listToTree($list); + $rules = []; if ($groupId) { $rules = (new ApiAuthRule())->where(['groupId' => $groupId])->select(); $rules = array_column($rules, 'url'); } - - $newList = []; - foreach ($list as $key => $value) { - - } + $newList = $this->buildList($list, $rules); return $this->buildSuccess([ 'list' => $newList - ], '登录成功'); + ]); } /** - * 新增用户 等待组权限 + * 新增组 * @return array + * @throws \Exception * @author zhaoxiang */ public function add() { + $rules = []; $postData = $this->request->post(); - $res = ApiMenu::create($postData); + if ($postData['rules']) { + $rules = $postData['rules']; + $rules = array_filter($rules); + } + unset($postData['rules']); + $res = ApiAuthGroup::create($postData); if ($res === false) { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); } else { + if ($rules) { + $insertData = []; + foreach ($rules as $value) { + if ($value) { + $insertData[] = [ + 'groupId' => $res->id, + 'url' => $value + ]; + } + } + (new ApiAuthRule())->saveAll($insertData); + } + return $this->buildSuccess([]); } } /** - * 用户状态编辑 + * 权限组状态编辑 * @return array * @author zhaoxiang */ public function changeStatus() { $id = $this->request->get('id'); $status = $this->request->get('status'); - $res = ApiUser::update([ + $res = ApiAuthGroup::update([ 'id' => $id, 'status' => $status ]); @@ -114,7 +139,7 @@ class Auth extends Base { } /** - * 删除用户 + * 删除组 * @return array * @author zhaoxiang */ @@ -123,14 +148,35 @@ class Auth extends Base { if (!$id) { return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); } - $childNum = ApiMenu::where(['fid' => $id])->count(); - if ($childNum) { - return $this->buildFailed(ReturnCode::INVALID, '当前菜单存在子菜单,不可以被删除!'); - } else { - ApiMenu::destroy($id); + ApiAuthGroup::destroy($id); + ApiAuthRule::destroy(['groupId' => $id]); - return $this->buildSuccess([]); + return $this->buildSuccess([]); + } + + /** + * 构建适用前端的权限数据 + * @param $list + * @param $rules + * @return array + * @author zhaoxiang + */ + private function buildList($list, $rules) { + $newList = []; + foreach ($list as $key => $value) { + $newList[$key]['title'] = $value['name']; + $newList[$key]['key'] = $value['url']; + if (isset($value['_child'])) { + $newList[$key]['expand'] = true; + $newList[$key]['children'] = $this->buildList($value['_child'], $rules); + } else { + if (in_array($value['url'], $rules)) { + $newList[$key]['checked'] = true; + } + } } + + return $newList; } } diff --git a/application/admin/controller/Base.php b/application/admin/controller/Base.php index fb2692e..f3c3baf 100644 --- a/application/admin/controller/Base.php +++ b/application/admin/controller/Base.php @@ -60,7 +60,7 @@ class Base extends Controller { /** * 将查询的二维对象转换成二维数组 - * @param Object $res + * @param array $res * @param string $key * @return array * @author zhaoxiang diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index 5a261ed..cc9fa1d 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -20,7 +20,7 @@ class Menu extends Base { * @author zhaoxiang */ public function index() { - $list = ApiMenu::all(); + $list = (new ApiMenu)->where([])->order('sort', 'ASC')->select(); $list = $this->buildArrFromObj($list); $list = formatTree(listToTree($list));