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

@@ -13,7 +13,10 @@ declare(strict_types=1);
namespace app\admin\controller\system;
use app\admin\enums\AdminEnum;
use app\admin\service\AdminService;
use app\AdminController;
use app\common\exception\OperateException;
use app\common\model\system\AdminNotice;
use app\common\model\system\Jobs;
use app\common\model\system\Department;
@@ -63,64 +66,20 @@ class Admin extends AdminController
/**
* 获取资源列表
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index()
public function index(): Response
{
$this->jobs = Jobs::select()->toArray();
$this->group = AdminGroupModel::select()->toArray();
$this->department = Department::getListTree();
// 判断isAjax
if (request()->isAjax()) {
// 获取数据
$post = \request()->all();
$page = (int)request()->input('page') ?? 1;
$limit = (int)request()->input('limit') ?? 10;
$status = !empty($post['status']) ? $post['status'] - 1 : 1;
// 生成查询条件
$where = array();
if (!empty($post['name'])) {
$where[] = ['name', 'like', '%' . $post['name'] . '%'];
}
if (!empty($post['dep'])) {
$where[] = ['department_id', 'find in set', $post['dep']];
}
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")->withoutField('pwd')->limit((int)$limit)->page((int)$page)->select()->toArray();
// 循环处理数据
foreach ($list as $key => $value) {
$groupIDs = explode(',', $value['group_id']);
foreach ($groupIDs as $field => $id) {
// 查找组
$result = list_search($this->group, ['id' => $id]);
if (!empty($result)) {
$list[$key]['group'][$field] = $result;
}
}
if (!empty($list[$key]['group'])) {
$list[$key]['group'] = list_sort_by($list[$key]['group'], 'id');
}
$authNodes = $this->auth->getRulesNode($value['id']);
$list[$key][AUTH_RULES] = $authNodes[$this->auth->authPrivate];
$authNodes = $this->auth->getRulesNode($value['id'], AUTH_CATE);
$list[$key][AUTH_CATE] = $authNodes[$this->auth->authPrivate];
}
$params = request()->all();
list('count' => $count, 'list' => $list) = AdminService::dataList($params);
return $this->success('查询成功', null, $list, $count);
}
@@ -134,39 +93,15 @@ class Admin extends AdminController
/**
* 添加管理员
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function add(): \support\Response
public function add(): Response
{
if (request()->isPost()) {
// 验证数据
$post = request()->post();
$post = request_validate_rules($post, get_class($this->model));
if (!is_array($post)) {
return $this->error($post);
}
$where[] = ['name', '=', $post['name']];
$where[] = ['email', '=', $post['email']];
if ($this->model->whereOr($where)->find()) {
return $this->error('该用户名或邮箱已被注册!');
}
// 管理员加密
$post['pwd'] = encryptPwd($post['pwd']);
$post['create_ip'] = request()->getRealIp();
$data = $this->model->create($post);
if (!is_empty($data['id'])) {
$access['admin_id'] = $data['id'];
$access['group_id'] = $data['group_id'];
AdminAccessModel::insert($access);
return $this->success('添加管理员成功!');
} else {
return $this->error('添加管理员失败!');
}
validate(\app\common\validate\system\Admin::class)->scene('add')->check($post);
AdminService::add($post);
return $this->success('添加管理员成功');
}
// 获取用户组
@@ -175,115 +110,65 @@ class Admin extends AdminController
/**
* 更新管理员
* @return Response
* @throws OperateException
*/
public function edit()
public function edit(): Response
{
if (request()->isPost()) {
$id = request()->input('id');
if (!empty($id) && is_numeric($id)) {
// 验证数据
$post = request()->all();
$retError = request_validate_rules($post, get_class($this->model), 'edit');
if (!is_array($retError)) {
return $this->error($retError);
}
if (isset($post['pwd']) && !empty($post['pwd'])) {
$post['pwd'] = encryptPwd($post['pwd']);
} else {
// 清空避免被覆盖
unset($post['pwd']);
}
if ($this->model->update($post)) {
$access['group_id'] = $post['group_id'];
AdminAccessModel::where('admin_id', $id)->update($access);
return $this->success('更新管理员成功!');
}
}
$post = request()->all();
validate(\app\common\validate\system\Admin::class)->scene('edit')->check($post);
AdminService::edit($post);
return $this->success('更新管理员成功');
}
return $this->error('更新管理员失败');
}
/**
* 获取用户权限树
* @access public
* getAdminRules
*/
public function getPermissions()
{
return $this->authService->getPermissionsMenu();
}
/**
* 获取节点数据
* @access public
*/
public function getRuleCateTree()
{
$type = input('type', AdminEnum::ADMIN_AUTH_RULES);
return $this->authService->getRuleCatesTree($type, $this->authService->authPrivate);
}
/**
* 编辑权限
* @return Response
* @access public
* @return Response
* @throws OperateException
*/
public function editRules(): Response
{
return $this->updateRuleCates();
$adminId = input('admin_id', 0);
AdminService::updateRulesNodes($adminId, AdminEnum::ADMIN_AUTH_RULES);
return $this->success('更新权限成功!');
}
/**
* 编辑栏目权限
* @return Response
* @access public
* @return Response
* @throws OperateException
*/
public function editCates(): Response
{
return $this->updateRuleCates(AUTH_CATE);
}
/**
* 更新权限函数
* @access protected
* @param string $type
* @return Response
*/
protected function updateRuleCates(string $type = AUTH_RULES): Response
{
$admin_id = input('admin_id');
$rules = request()->post($type) ?? [];
$access = $this->auth->getRulesNode($admin_id, $type);
$rules = array_diff($rules, $access[$this->auth->authGroup]);
// 权限验证
if (!$this->auth->checkRuleOrCateNodes($rules, $type, $this->auth->authPrivate)) {
return $this->error('没有权限!');
}
// 获取个人节点
$differ = array_diff($access[$this->auth->authPrivate], $access[$this->auth->authGroup]);
$current = [];
if (!$this->auth->superAdmin()) {
$current = $this->auth->getRulesNode();
$current = array_diff($differ, $current[$this->auth->authPrivate]);
}
$rules = array_unique(array_merge($rules, $current));
$AdminAccessModel = new AdminAccessModel();
$data = ["$type" => implode(',', $rules)];
if ($AdminAccessModel->update($data, ['admin_id' => $admin_id])) {
return $this->success('更新权限成功!');
}
return $this->error('更新权限失败!');
}
/**
* 获取用户权限树
* getAdminRules
* @return mixed
*/
public function getPermissions(): mixed
{
$list = [];
if (\request()->isAjax()) {
$type = input('type', 'menu');
$group = input('group', 0);
if ($type == 'menu') {
return $this->auth->getRulesMenu();
} else {
try {
$list = $this->auth->getRuleCatesTree($type, $group ? $this->auth->authGroup : $this->auth->authPrivate);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $list;
}
}
return $list;
$adminId = input('admin_id', 0);
AdminService::updateRulesNodes($adminId, AdminEnum::ADMIN_AUTH_CATES);
return $this->success('更新权限成功!');
}
/**
@@ -295,157 +180,6 @@ class Admin extends AdminController
return view('/system/admin/theme');
}
/**
* 消息模板
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function bells(): Response
{
$list = [];
$count = [];
$array = ['notice', 'message', 'todo'];
$type = input('type', 'notice');
if (\request()->isAjax()) {
$page = input('page', 1);
$limit = input('limit', 3);
// 计算最大页码
$data = AdminNotice::with(['admin'])->where(['type' => $type, 'admin_id' => get_admin_id()])
->order('id', 'desc')->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
return $this->success('获取成功', '', $data);
}
foreach ($array as $item) {
$where = [
['type', '=', $item],
['admin_id', '=', get_admin_id()]
];
$count[$item] = AdminNotice::where($where)->where('status', 0)->count();
$list[$item] = AdminNotice::with(['admin'])->withoutField('content')->where($where)->limit(3)->order('id desc')->select()->toArray();
}
return view('/system/admin/bells', [
'list' => $list,
'count' => $count
]);
}
/**
* 阅读消息
* @return response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function readNotice(): Response
{
$id = input('id', 0);
$type = input('type', 'notice');
if (!empty($id)) {
$detail = AdminNotice::with(['admin'])->where(['id' => $id, 'admin_id' => get_admin_id()])->find();
if (empty($detail)) {
return $this->error('404 Not Found');
}
// 默认已读
if ($type !== 'todo') {
$detail->status = 1;
$detail->save();
}
}
return $this->view('/system/admin/' . $type, [
'detail' => $detail ?? []
]);
}
/**
* 更新即时消息
* @return Response|void
*/
public function saveNotice()
{
if (\request()->post()) {
$post = request()->post();
$post['send_id'] = get_admin_id();
$post['type'] = 'message';
$post['send_ip'] = request()->getRealIp();
$post['create_time'] = time();
try {
AdminNotice::sendNotice($post, 'none');
} catch (\Exception $e) {
return $this->error('发送失败:' . $e->getMessage());
}
return $this->success('发送成功');
} else if (\request()->isAjax()) {
$id = input('id', 0);
$status = input('status', 1);
try {
if (empty($id)) {
throw new Exception('参数错误');
}
AdminNotice::where(['id' => $id, 'admin_id' => get_admin_id()])->update(['status' => $status]);
} catch (Exception $e) {
return $this->error('更新失败');
}
return $this->success('更新成功');
}
}
/**
* 清空消息
* @return Response|void
*/
public function clearNotice()
{
if (\request()->isAjax()) {
$type = input('type', 'notice');
$where = [
['type', '=', $type],
['status', '=', 1],
['admin_id', '=', get_admin_id()]
];
try {
AdminNotice::where($where)->delete();
} catch (Exception $e) {
return $this->error('清空失败');
}
return $this->success('清空成功');
}
}
/**
* 全部消息已读
* @return Response|void
*/
public function readAllNotice()
{
if (\request()->isAjax()) {
$type = input('type', 'notice');
$where = [
['type', '=', $type],
['admin_id', '=', get_admin_id()]
];
try {
AdminNotice::where($where)->update(['status' => 1]);
} catch (Exception $e) {
return $this->error('操作失败');
}
return $this->success('全部已读成功');
}
}
/**
* 个人中心
* @param Request $request
@@ -454,7 +188,7 @@ class Admin extends AdminController
* @throws DbException
* @throws ModelNotFoundException
*/
public function center(Request $request): \support\Response
public function center(Request $request): Response
{
if (request()->isPost()) {
$post = request()->post();
@@ -548,7 +282,7 @@ class Admin extends AdminController
* @throws DbException
* @throws ModelNotFoundException
*/
public function pwd(): \support\Response
public function pwd(): Response
{
if (request()->isPost()) {
@@ -576,10 +310,9 @@ class Admin extends AdminController
/**
* 语言配置
* @return mixed
* @throws \think\Exception
* @return Response
*/
public function language()
public function language(): Response
{
$language = input('l');
$env = base_path() . '/.env';
@@ -590,13 +323,14 @@ class Admin extends AdminController
if (write_file($env, $content)) {
return json(['success']);
}
return json(['error']);
}
/**
* 更改状态
* @return \support\Response
* @return Response
*/
public function status()
public function status(): Response
{
$id = input('id');
if ($id == 1) {
@@ -613,14 +347,14 @@ class Admin extends AdminController
/**
* 删除管理员
* @return mixed
* @throws \think\db\exception\DbException
* @return Response
* @throws DbException
*/
public function del()
public function del(): Response
{
$id = input('id');
!is_array($id) && ($id = array($id));
if (!empty($id) && is_array($id)) {
if (!empty($id)) {
// 过滤权限
if (in_array("1", $id)) {
@@ -641,21 +375,20 @@ class Admin extends AdminController
/**
* 清理系统缓存
* @return \support\Response
* @return Response
*/
public function clear(): \support\Response
public function clear(): Response
{
if (request()->isAjax()) {
$type = input('type');
try {
// 清理内容
if ($type == 'all' || $type == 'content') {
$session = session(AdminSession);
\support\Cache::clear();
request()->session()->set(AdminSession, $session);
$session = session(AdminEnum::ADMIN_SESSION);
Cache::clear();
request()->session()->set(AdminEnum::ADMIN_SESSION, $session);
}
// 清理模板