refactor: 重构权限服务类

This commit is contained in:
Ying
2023-07-03 10:08:34 +08:00
parent 8becf0ef36
commit 4e377def8d
42 changed files with 1679 additions and 1524 deletions

View File

@@ -12,7 +12,9 @@ declare (strict_types=1);
namespace app\admin\controller\system;
use app\admin\service\UserService;
use app\AdminController;
use app\common\exception\OperateException;
use app\common\library\Ip2Region;
use app\common\model\system\User as UserModel;
use app\common\model\system\UserGroup as UserGroupModel;
@@ -43,121 +45,52 @@ class User extends AdminController
* @throws DbException
* @throws ModelNotFoundException
*/
public function index(): \support\Response
public function index(): Response
{
$userGroup = UserGroupModel::select()->toArray();
if (request()->isAjax()) {
// 获取数据
$post = \request()->all();
$page = (int)input('page') ?? 1;
$limit = (int)input('limit') ?? 10;
$status = !empty($post['status']) ? (int)$post['status'] - 1 : 1;
// 生成查询条件
$where = array();
if (!empty($post['nickname'])) {
$where[] = ['nickname', 'like', '%' . $post['nickname'] . '%'];
}
if (!empty($post['group_id'])) {
$where[] = ['group_id', 'find in set', $post['group_id']];
}
// 生成查询数据
$where[] = ['status', '=', $status];
$count = $this->model->where($where)->count();
$page = ($count <= $limit) ? 1 : $page;
$list = $this->model->where($where)->order("id asc")->limit((int)$limit)->page((int)$page)->select();
// 循环处理数据
foreach ($list as $key => $value) {
$value->hidden(['pwd', 'salt']);
$region = Ip2Region::instance()->memorySearch($value['login_ip']);
$region = explode('|', $region['region']);
$list[$key]['region'] = $region;
$result = list_search($userGroup, ['id' => $value['group_id']]);
if (!empty($result)) {
$list[$key]['group'] = $result['title'];
}
}
// TODO..
return $this->success('查询成功', "", $list, $count);
$post = request()->all();
list($count, $list) = UserService::dataList($post);
return $this->success('查询成功', "/", $list, $count);
}
return view('/system/user/index', [
'UserGroup' => $userGroup,
'UserGroup' => UserGroupModel::select()->toArray()
]);
}
/**
* 添加会员
* @return Response
* @throws OperateException
*/
public function add()
public function add(): Response
{
if (request()->isPost()) {
$post = request()->post();
// 禁止重复注册
$whereName[] = ['nickname', '=', $post['nickname']];
$whereEmail[] = ['email', '=', $post['email']];
if ($this->model->whereOr([$whereName, $whereEmail])->findOrEmpty()->toArray()) {
return $this->error('该用户ID或邮箱已经存在');
}
// 生成密码
$salt = Random::alpha();
$post['salt'] = $salt;
$post['pwd'] = encryptPwd($post['pwd'], $post['salt']);
if ($this->model->create($post)) {
return $this->success('注册成功!');
}
return $this->error('注册失败!');
validate(\app\common\validate\system\User::class)->scene('add')->check($post);
UserService::add($post);
return $this->success('注册成功!');
}
return $this->error('注册失败!');
}
/**
* 编辑会员
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function edit()
public function edit(): Response
{
if (request()->isPost()) {
$post = \request()->post();
// 查询数据
$data = $this->model->find($post['id']);
if ($data['nickname'] != $post['nickname']) {
$whereName[] = ['nickname', '=', $post['nickname']];
if ($this->model->where($whereName)->find()) {
return $this->error('该用户ID已经存在');
}
}
if ($data['email'] != $post['email']) {
$whereEmail[] = ['email', '=', $post['email']];
if ($this->model->where($whereEmail)->find()) {
return $this->error('该用户邮箱已经存在!');
}
}
// 为空则去掉密码
if (empty($post['pwd'])) {
unset($post['pwd']);
} else {
$salt = Random::alpha();
$post['salt'] = $salt;
$post['pwd'] = encryptPwd($post['pwd'], $post['salt']);
}
if ($this->model->update($post)) {
return $this->success('更新成功!');
}
return $this->error('更新失败!');
$post = request()->post();
validate(\app\common\validate\system\User::class)->scene('edit')->check($post);
UserService::edit($post);
return $this->success('更新成功!');
}
return $this->error('更新失败!');
}
/**