modified 全面优化完成权限组等功能

This commit is contained in:
zhaoxiang 2019-06-22 18:36:36 +08:00
parent de1160bdb7
commit 2e60c0db88
2 changed files with 31 additions and 29 deletions

View File

@ -30,12 +30,15 @@ class Auth extends Base {
$keywords = $this->request->get('keywords', ''); $keywords = $this->request->get('keywords', '');
$status = $this->request->get('status', ''); $status = $this->request->get('status', '');
$where['name'] = ['like', "%{$keywords}%"]; $obj = new AdminAuthGroup();
if ($status === '1' || $status === '0') { if (strlen($status)) {
$where['status'] = $status; $obj = $obj->where('status', $status);
} }
$listObj = (new AdminAuthGroup())->where($where)->order('id DESC') if ($keywords) {
->paginate($limit, false, ['page' => $start])->toArray(); $obj = $obj->where('name', 'like', $keywords);
}
$listObj = $obj->order('id DESC')->paginate($limit, false, ['page' => $start])->toArray();
return $this->buildSuccess([ return $this->buildSuccess([
'list' => $listObj['data'], 'list' => $listObj['data'],
@ -45,11 +48,11 @@ class Auth extends Base {
/** /**
* 获取全部已开放的可选组 * 获取全部已开放的可选组
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array * @return array
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException * @throws \think\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function getGroups() { public function getGroups() {
$listInfo = (new AdminAuthGroup())->where(['status' => 1])->order('id', 'DESC')->select(); $listInfo = (new AdminAuthGroup())->where(['status' => 1])->order('id', 'DESC')->select();
@ -71,15 +74,15 @@ class Auth extends Base {
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function getRuleList() { public function getRuleList() {
$groupId = $this->request->get('groupId', 0); $groupId = $this->request->get('group_id', 0);
$list = (new AdminMenu)->where([])->order('sort', 'ASC')->select(); $list = (new AdminMenu)->where([])->order('sort', 'ASC')->select();
$list = Tools::buildArrFromObj($list); $list = Tools::buildArrFromObj($list);
$list = listToTree($list); $list = Tools::listToTree($list);
$rules = []; $rules = [];
if ($groupId) { if ($groupId) {
$rules = (new AdminAuthRule())->where(['groupId' => $groupId])->select(); $rules = (new AdminAuthRule())->where(['group_id' => $groupId])->select();
$rules = Tools::buildArrFromObj($rules); $rules = Tools::buildArrFromObj($rules);
$rules = array_column($rules, 'url'); $rules = array_column($rules, 'url');
} }
@ -113,8 +116,8 @@ class Auth extends Base {
foreach ($rules as $value) { foreach ($rules as $value) {
if ($value) { if ($value) {
$insertData[] = [ $insertData[] = [
'groupId' => $res->id, 'group_id' => $res->id,
'url' => $value 'url' => $value
]; ];
} }
} }
@ -181,21 +184,20 @@ class Auth extends Base {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
} }
$listInfo = (new AdminAuthGroupAccess())->where(['groupId' => ['like', "%{$id}%"]])->select(); $listInfo = (new AdminAuthGroupAccess())->where('find_in_set("' . $id . '", `group_id`)')->select();
if ($listInfo) { if ($listInfo) {
foreach ($listInfo as $value) { foreach ($listInfo as $value) {
$valueArr = $value->toArray(); $oldGroupArr = explode(',', $value->group_id);
$oldGroupArr = explode(',', $valueArr['groupId']);
$key = array_search($id, $oldGroupArr); $key = array_search($id, $oldGroupArr);
unset($oldGroupArr[$key]); unset($oldGroupArr[$key]);
$newData = implode(',', $oldGroupArr); $newData = implode(',', $oldGroupArr);
$value->groupId = $newData; $value->group_id = $newData;
$value->save(); $value->save();
} }
} }
AdminAuthGroup::destroy($id); AdminAuthGroup::destroy($id);
AdminAuthRule::destroy(['groupId' => $id]); AdminAuthRule::destroy(['group_id' => $id]);
return $this->buildSuccess([]); return $this->buildSuccess([]);
} }
@ -214,12 +216,12 @@ class Auth extends Base {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
} }
$oldInfo = AdminAuthGroupAccess::get(['uid' => $uid])->toArray(); $oldInfo = AdminAuthGroupAccess::get(['uid' => $uid])->toArray();
$oldGroupArr = explode(',', $oldInfo['groupId']); $oldGroupArr = explode(',', $oldInfo['group_id']);
$key = array_search($gid, $oldGroupArr); $key = array_search($gid, $oldGroupArr);
unset($oldGroupArr[$key]); unset($oldGroupArr[$key]);
$newData = implode(',', $oldGroupArr); $newData = implode(',', $oldGroupArr);
$res = AdminAuthGroupAccess::update([ $res = AdminAuthGroupAccess::update([
'groupId' => $newData 'group_id' => $newData
], [ ], [
'uid' => $uid 'uid' => $uid
]); ]);
@ -266,7 +268,7 @@ class Auth extends Base {
private function editRule() { private function editRule() {
$postData = $this->request->post(); $postData = $this->request->post();
$needAdd = []; $needAdd = [];
$has = (new AdminAuthRule())->where(['groupId' => $postData['id']])->select(); $has = (new AdminAuthRule())->where(['group_id' => $postData['id']])->select();
$has = Tools::buildArrFromObj($has); $has = Tools::buildArrFromObj($has);
$hasRule = array_column($has, 'url'); $hasRule = array_column($has, 'url');
$needDel = array_flip($hasRule); $needDel = array_flip($hasRule);
@ -274,7 +276,7 @@ class Auth extends Base {
if (!empty($value)) { if (!empty($value)) {
if (!in_array($value, $hasRule)) { if (!in_array($value, $hasRule)) {
$data['url'] = $value; $data['url'] = $value;
$data['groupId'] = $postData['id']; $data['group_id'] = $postData['id'];
$needAdd[] = $data; $needAdd[] = $data;
} else { } else {
unset($needDel[$value]); unset($needDel[$value]);
@ -287,8 +289,8 @@ class Auth extends Base {
if (count($needDel)) { if (count($needDel)) {
$urlArr = array_keys($needDel); $urlArr = array_keys($needDel);
AdminAuthRule::destroy([ AdminAuthRule::destroy([
'groupId' => $postData['id'], 'group_id' => $postData['id'],
'url' => ['in', $urlArr] 'url' => ['in', $urlArr]
]); ]);
} }
} }

View File

@ -121,11 +121,11 @@ class User extends Base {
return $this->buildFailed(ReturnCode::PARAM_INVALID, '非法操作'); return $this->buildFailed(ReturnCode::PARAM_INVALID, '非法操作');
} }
$totalNum = (new AdminAuthGroupAccess())->where('find_in_set("' . $gid . '", `groupId`)')->count(); $totalNum = (new AdminAuthGroupAccess())->where('find_in_set("' . $gid . '", `group_id`)')->count();
$start = $limit * ($page - 1); $start = $limit * ($page - 1);
$sql = "SELECT au.* FROM admin_user as au LEFT JOIN admin_auth_group_access as aaga " . $sql = "SELECT au.* FROM admin_user as au LEFT JOIN admin_auth_group_access as aaga " .
" ON aaga.`uid` = au.`id` WHERE find_in_set('{$gid}', aaga.`groupId`) " . " ON aaga.`uid` = au.`id` WHERE find_in_set('{$gid}', aaga.`group_id`) " .
" ORDER BY au.regTime DESC LIMIT {$start}, {$limit}"; " ORDER BY au.create_time DESC LIMIT {$start}, {$limit}";
$userInfo = Db::query($sql); $userInfo = Db::query($sql);
$uidArr = array_column($userInfo, 'id'); $uidArr = array_column($userInfo, 'id');
@ -134,11 +134,11 @@ class User extends Base {
foreach ($userInfo as $key => $value) { foreach ($userInfo as $key => $value) {
if (isset($userData[$value['id']])) { if (isset($userData[$value['id']])) {
$userInfo[$key]['lastLoginIp'] = long2ip($userData[$value['id']]['lastLoginIp']); $userInfo[$key]['last_login_ip'] = long2ip($userData[$value['id']]['last_login_ip']);
$userInfo[$key]['loginTimes'] = $userData[$value['id']]['loginTimes']; $userInfo[$key]['login_times'] = $userData[$value['id']]['login_times'];
$userInfo[$key]['lastLoginTime'] = date('Y-m-d H:i:s', $userData[$value['id']]['lastLoginTime']); $userInfo[$key]['last_login_time'] = date('Y-m-d H:i:s', $userData[$value['id']]['last_login_time']);
} }
$userInfo[$key]['regIp'] = long2ip($userInfo[$key]['regIp']); $userInfo[$key]['create_ip'] = long2ip($userInfo[$key]['create_ip']);
} }
return $this->buildSuccess([ return $this->buildSuccess([