From 7c37aefde7dd297671dd555dfcaa799cf620a5e1 Mon Sep 17 00:00:00 2001 From: zhaoxiang <756958008@qq.com> Date: Mon, 12 Oct 2020 22:46:10 +0800 Subject: [PATCH] =?UTF-8?q?modified=20=E7=A7=BB=E6=A4=8D=E6=9D=83=E9=99=90?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/admin/Auth.php | 277 ++++++++++++++++++++++++++++++++++ app/controller/admin/User.php | 2 +- app/util/Tools.php | 10 +- 3 files changed, 283 insertions(+), 6 deletions(-) create mode 100644 app/controller/admin/Auth.php diff --git a/app/controller/admin/Auth.php b/app/controller/admin/Auth.php new file mode 100644 index 0000000..52dd221 --- /dev/null +++ b/app/controller/admin/Auth.php @@ -0,0 +1,277 @@ + + */ + +namespace app\controller\admin; + +use app\model\AdminAuthGroup; +use app\model\AdminAuthGroupAccess; +use app\model\AdminAuthRule; +use app\model\AdminMenu; +use app\util\ReturnCode; +use app\util\Tools; +use think\Response; + +class Auth extends Base { + + /** + * 获取权限组列表 + * @return Response + * @throws \think\db\exception\DbException + * @author zhaoxiang + */ + public function index(): Response { + $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT')); + $start = $this->request->get('page', 1); + $keywords = $this->request->get('keywords', ''); + $status = $this->request->get('status', ''); + + $obj = new AdminAuthGroup(); + if (strlen($status)) { + $obj = $obj->where('status', $status); + } + if ($keywords) { + $obj = $obj->whereLike('name', "%{$keywords}%"); + } + + $listObj = $obj->order('id', 'DESC')->paginate(['page' => $start, 'list_rows' => $limit])->toArray(); + + return $this->buildSuccess([ + 'list' => $listObj['data'], + 'count' => $listObj['total'] + ]); + } + + /** + * 获取全部已开放的可选组 + * @return Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author zhaoxiang + */ + public function getGroups(): Response { + $listInfo = (new AdminAuthGroup())->where(['status' => 1])->order('id', 'DESC')->select(); + $count = count($listInfo); + $listInfo = Tools::buildArrFromObj($listInfo); + + return $this->buildSuccess([ + 'list' => $listInfo, + 'count' => $count + ]); + } + + /** + * 获取组所在权限列表 + * @return Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author zhaoxiang + */ + public function getRuleList(): Response { + $groupId = $this->request->get('group_id', 0); + + $list = (new AdminMenu)->order('sort', 'ASC')->select(); + $list = Tools::buildArrFromObj($list); + $list = Tools::listToTree($list); + + $rules = []; + if ($groupId) { + $rules = (new AdminAuthRule())->where(['group_id' => $groupId])->select(); + $rules = Tools::buildArrFromObj($rules); + $rules = array_column($rules, 'url'); + } + $newList = $this->buildList($list, $rules); + + return $this->buildSuccess([ + 'list' => $newList + ]); + } + + /** + * 新增组 + * @return Response + * @author zhaoxiang + */ + public function add(): Response { + $res = AdminAuthGroup::create([ + 'name' => $this->request->post('name', ''), + 'description' => $this->request->post('description', '') + ]); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); + } + + return $this->buildSuccess(); + } + + /** + * 权限组状态编辑 + * @return Response + * @author zhaoxiang + */ + public function changeStatus(): Response { + $id = $this->request->get('id'); + $status = $this->request->get('status'); + $res = AdminAuthGroup::update([ + 'id' => $id, + 'status' => $status + ]); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); + } + + return $this->buildSuccess(); + } + + /** + * 编辑用户 + * @return Response + * @author zhaoxiang + */ + public function edit(): Response { + $res = AdminAuthGroup::update([ + 'id' => $this->request->post('id', 0), + 'name' => $this->request->post('name', ''), + 'description' => $this->request->post('description', '') + ]); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); + } + + return $this->buildSuccess(); + } + + /** + * 删除组 + * @return Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author zhaoxiang + */ + public function del(): Response { + $id = $this->request->get('id'); + if (!$id) { + return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); + } + + $listInfo = (new AdminAuthGroupAccess())->where('find_in_set("' . $id . '", `group_id`)')->select(); + if ($listInfo) { + foreach ($listInfo as $value) { + $oldGroupArr = explode(',', $value->group_id); + $key = array_search($id, $oldGroupArr); + unset($oldGroupArr[$key]); + $newData = implode(',', $oldGroupArr); + $value->group_id = $newData; + $value->save(); + } + } + + AdminAuthGroup::destroy($id); + AdminAuthRule::destroy(['group_id' => $id]); + + return $this->buildSuccess(); + } + + /** + * 从指定组中删除指定用户 + * @return Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author zhaoxiang + */ + public function delMember(): Response { + $gid = $this->request->get('gid', 0); + $uid = $this->request->get('uid', 0); + if (!$gid || !$uid) { + return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); + } + $oldInfo = (new AdminAuthGroupAccess())->where('uid', $uid)->find()->toArray(); + $oldGroupArr = explode(',', $oldInfo['group_id']); + $key = array_search($gid, $oldGroupArr); + unset($oldGroupArr[$key]); + $newData = implode(',', $oldGroupArr); + $res = AdminAuthGroupAccess::update([ + 'group_id' => $newData + ], [ + 'uid' => $uid + ]); + if ($res === false) { + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); + } + + return $this->buildSuccess(); + } + + /** + * 构建适用前端的权限数据 + * @param $list + * @param $rules + * @return array + * @author zhaoxiang + */ + private function buildList($list, $rules): array { + $newList = []; + foreach ($list as $key => $value) { + $newList[$key]['title'] = $value['title']; + $newList[$key]['key'] = $value['url']; + if (isset($value['children'])) { + $newList[$key]['expand'] = true; + $newList[$key]['children'] = $this->buildList($value['children'], $rules); + } else { + if (in_array($value['url'], $rules)) { + $newList[$key]['checked'] = true; + } + } + } + + return $newList; + } + + /** + * 编辑权限细节 + * @return Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author zhaoxiang + */ + public function editRule(): Response { + $id = $this->request->post('id', 0); + $rules = $this->request->post('rules', []); + if ($rules) { + $needAdd = []; + $has = (new AdminAuthRule())->where(['group_id' => $id])->select(); + $has = Tools::buildArrFromObj($has); + $hasRule = array_column($has, 'url'); + $needDel = array_flip($hasRule); + foreach ($rules as $key => $value) { + if (!empty($value)) { + if (!in_array($value, $hasRule)) { + $data['url'] = $value; + $data['group_id'] = $id; + $needAdd[] = $data; + } else { + unset($needDel[$value]); + } + } + } + if (count($needAdd)) { + (new AdminAuthRule())->saveAll($needAdd); + } + if (count($needDel)) { + $urlArr = array_keys($needDel); + (new AdminAuthRule())->whereIn('url', $urlArr)->where('group_id', $id)->delete(); + } + } + + return $this->buildSuccess(); + } +} diff --git a/app/controller/admin/User.php b/app/controller/admin/User.php index ed828a4..a2d6ec8 100644 --- a/app/controller/admin/User.php +++ b/app/controller/admin/User.php @@ -47,7 +47,7 @@ class User extends Base { } $listObj = $obj->order('create_time', 'DESC') - ->paginate(['page' => $start, 'list_rows' => $limit], false)->each(function($item, $key) { + ->paginate(['page' => $start, 'list_rows' => $limit])->each(function($item, $key) { $item->userData; })->toArray(); $listInfo = $listObj['data']; diff --git a/app/util/Tools.php b/app/util/Tools.php index 9e7e790..2bc15b0 100644 --- a/app/util/Tools.php +++ b/app/util/Tools.php @@ -106,13 +106,13 @@ class Tools { /** * 将二维数组变成指定key - * @param array $array + * @param $array * @param string $keyName * @return array * @author zhaoxiang */ - public static function buildArrByNewKey(array $array, string $keyName = 'id'): array { - $list = array(); + public static function buildArrByNewKey($array, string $keyName = 'id'): array { + $list = []; foreach ($array as $item) { $list[$item[$keyName]] = $item; } @@ -136,9 +136,9 @@ class Tools { string $child = 'children', string $root = '0' ): array { - $tree = array(); + $tree = []; if (is_array($list)) { - $refer = array(); + $refer = []; foreach ($list as $key => $data) { $refer[$data[$pk]] = &$list[$key]; }