modified 完善用户加组

This commit is contained in:
zhaoxiang 2018-02-10 23:33:16 +08:00
parent f898ffb23a
commit a989b55abc
3 changed files with 75 additions and 11 deletions

View File

@ -46,6 +46,25 @@ class Auth extends Base {
]); ]);
} }
/**
* 获取全部已开放的可选组
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getGroups() {
$listInfo = (new ApiAuthGroup())->where(['status' => 1])->order('id', 'DESC')->select();
$count = count($listInfo);
$listInfo = $this->buildArrFromObj($listInfo);
return $this->buildSuccess([
'list' => $listInfo,
'count' => $count
]);
}
/** /**
* 获取组所在权限列表 * 获取组所在权限列表
* @return array * @return array

View File

@ -8,9 +8,11 @@
namespace app\admin\controller; namespace app\admin\controller;
use app\model\ApiAuthGroupAccess;
use app\model\ApiUser; use app\model\ApiUser;
use app\model\ApiUserData; use app\model\ApiUserData;
use app\util\ReturnCode; use app\util\ReturnCode;
use app\util\Tools;
class User extends Base { class User extends Base {
@ -58,6 +60,12 @@ class User extends Base {
$userData = $this->buildArrFromObj($userData); $userData = $this->buildArrFromObj($userData);
$userData = $this->buildArrByNewKey($userData, 'uid'); $userData = $this->buildArrByNewKey($userData, 'uid');
$userGroup = ApiAuthGroupAccess::all(function($query) use ($idArr) {
$query->whereIn('uid', $idArr);
});
$userGroup = $this->buildArrFromObj($userGroup);
$userGroup = $this->buildArrByNewKey($userGroup, 'uid');
foreach ($listInfo as $key => $value) { foreach ($listInfo as $key => $value) {
if (isset($userData[$value['id']])) { if (isset($userData[$value['id']])) {
$listInfo[$key]['lastLoginIp'] = long2ip($userData[$value['id']]['lastLoginIp']); $listInfo[$key]['lastLoginIp'] = long2ip($userData[$value['id']]['lastLoginIp']);
@ -65,6 +73,11 @@ class User extends Base {
$listInfo[$key]['lastLoginTime'] = date('Y-m-d H:i:s', $userData[$value['id']]['lastLoginTime']); $listInfo[$key]['lastLoginTime'] = date('Y-m-d H:i:s', $userData[$value['id']]['lastLoginTime']);
} }
$listInfo[$key]['regIp'] = long2ip($listInfo[$key]['regIp']); $listInfo[$key]['regIp'] = long2ip($listInfo[$key]['regIp']);
if (isset($userGroup[$value['id']])) {
$listInfo[$key]['groupId'] = explode(',', $userGroup[$value['id']]['groupId']);
} else {
$listInfo[$key]['groupId'] = [];
}
} }
return $this->buildSuccess([ return $this->buildSuccess([
@ -74,16 +87,29 @@ class User extends Base {
} }
/** /**
* 新增用户 等待组权限 * 新增用户
* @return array * @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function add() { public function add() {
$groups = '';
$postData = $this->request->post(); $postData = $this->request->post();
$res = ApiMenu::create($postData); $postData['regIp'] = request()->ip(1);
$postData['regTime'] = time();
$postData['password'] = Tools::userMd5($postData['password']);
if ($postData['groupId']) {
$groups = implode(',', $postData['groupId']);
}
unset($postData['groupId']);
$res = ApiUser::create($postData);
if ($res === false) { if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else { } else {
ApiAuthGroupAccess::create([
'uid' => $res->id,
'groupId' => $groups
]);
return $this->buildSuccess([]); return $this->buildSuccess([]);
} }
} }
@ -109,15 +135,33 @@ class User extends Base {
/** /**
* 编辑用户 * 编辑用户
* @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
*/ */
public function edit() { public function edit() {
$groups = '';
$postData = $this->request->post(); $postData = $this->request->post();
$res = ApiMenu::update($postData); $postData['regIp'] = request()->ip(1);
$postData['regTime'] = time();
if ($postData['password'] === 'ApiAdmin') {
unset($postData['password']);
} else {
$postData['password'] = Tools::userMd5($postData['password']);
}
if ($postData['groupId']) {
$groups = implode(',', $postData['groupId']);
}
unset($postData['groupId']);
$res = ApiUser::update($postData);
if ($res === false) { if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else { } else {
ApiAuthGroupAccess::update([
'groupId' => $groups
], [
'uid' => $res->id,
]);
return $this->buildSuccess([]); return $this->buildSuccess([]);
} }
} }
@ -132,14 +176,11 @@ class User extends Base {
if (!$id) { if (!$id) {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
} }
$childNum = ApiMenu::where(['fid' => $id])->count(); ApiUser::destroy($id);
if ($childNum) { ApiAuthGroupAccess::destroy(['uid' => $id]);
return $this->buildFailed(ReturnCode::INVALID, '当前菜单存在子菜单,不可以被删除!');
} else { return $this->buildSuccess([]);
ApiMenu::destroy($id);
return $this->buildSuccess([]);
}
} }
} }

View File

@ -71,6 +71,10 @@ return [
'admin/Auth/del', 'admin/Auth/del',
['method' => 'get', 'after_behavior' => $afterBehavior] ['method' => 'get', 'after_behavior' => $afterBehavior]
], ],
'Auth/getGroups' => [
'admin/Auth/getGroups',
['method' => 'get', 'after_behavior' => $afterBehavior]
],
'Auth/getRuleList' => [ 'Auth/getRuleList' => [
'admin/Auth/getRuleList', 'admin/Auth/getRuleList',
['method' => 'get', 'after_behavior' => $afterBehavior] ['method' => 'get', 'after_behavior' => $afterBehavior]