feat: 升级权限管理模式

This commit is contained in:
augushong
2025-03-20 16:57:05 +08:00
parent 1887733b32
commit d3e85fa552
50 changed files with 330 additions and 1125 deletions

View File

@@ -6,6 +6,7 @@ use app\admin\model\SystemAuth;
use app\admin\model\SystemAuthNode;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use app\admin\service\NodeService;
use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use think\App;
@@ -36,11 +37,30 @@ class AuthBase extends AdminController
{
$row = $this->model->find($id);
empty($row) && $this->error('数据不存在');
if ($this->request->isAjax()) {
$list = $this->model->getAuthorizeNodeListByAdminId($id);
$this->success('获取成功', $list);
$checkNodeList = (new SystemAuthNode())
->where('auth_id', $id)
->column('node');
$module_list = (new NodeService())->getNodeTree();
foreach ($module_list as $module_key => $module) {
foreach ($module['children'] as $controllerKey => $controller) {
foreach ($controller['children'] as $actionKey => $action) {
$checked = in_array($action['node'], $checkNodeList);
$checked_string = $checked? 'checked' : '';
$disabled_string = '';
if(!$action['auth']){
$checked_string = 'checked';
$disabled_string = 'disabled';
}
$module_list[$module_key]['children'][$controllerKey]['children'][$actionKey]['checked'] = $checked_string;
$module_list[$module_key]['children'][$controllerKey]['children'][$actionKey]['disabled'] = $disabled_string;
}
}
}
$this->assign('row', $row);
$this->assign('checked_node_list', $checkNodeList);
$this->assign('module_list', $module_list);
return $this->fetch();
}
@@ -52,8 +72,8 @@ class AuthBase extends AdminController
{
$this->checkPostRequest();
$id = $this->request->post('id');
$node = $this->request->post('node', '[]');
$node = json_decode($node, true);
$node = $this->request->post('node', []);
$row = $this->model->find($id);
empty($row) && $this->error('数据不存在');
try {
@@ -64,7 +84,7 @@ class AuthBase extends AdminController
foreach ($node as $vo) {
$saveAll[] = [
'auth_id' => $id,
'node_id' => $vo,
'node' => $vo,
];
}
$authNode->saveAll($saveAll);

View File

@@ -3,9 +3,9 @@
namespace base\admin\controller\system;
use app\admin\model\SystemMenu;
use app\admin\model\SystemNode;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use app\admin\service\NodeService;
use app\admin\service\TriggerService;
use app\common\constants\MenuConstant;
use app\common\controller\AdminController;
@@ -14,7 +14,7 @@ use think\facade\Db;
/**
* Class Menu.
* @ControllerAnnotation(title="菜单管理",auth=true)
* @ControllerAnnotation(title="菜单管理")
*/
class MenuBase extends AdminController
{
@@ -206,10 +206,8 @@ class MenuBase extends AdminController
public function getMenuTips()
{
$node = input('get.keywords');
$list = SystemNode::whereLike('node', "%{$node}%")
->field('node,title')
->limit(10)
->select();
$list = (new NodeService())->getNodeCollection();
$list = $list->whereLike('node', $node);
return json([
'code' => 0,

View File

@@ -2,7 +2,6 @@
namespace base\admin\controller\system;
use app\admin\model\SystemNode;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use app\admin\service\NodeService;
@@ -21,7 +20,6 @@ class NodeBase extends AdminController
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new SystemNode();
}
/**
@@ -29,82 +27,8 @@ class NodeBase extends AdminController
*/
public function index()
{
if ($this->request->isAjax()) {
if (input('selectFields')) {
return $this->selectList();
}
$count = $this->model
->count();
$list = $this->model
->getNodeTreeList();
$data = [
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list,
];
return json($data);
}
$module_list = (new NodeService())->getNodeTree();
$this->assign('module_list', $module_list);
return $this->fetch();
}
/**
* @NodeAnotation(title="系统节点更新")
*/
public function refreshNode($force = 0)
{
$this->checkPostRequest();
$nodeList = (new NodeService())->getNodelist();
empty($nodeList) && $this->error('暂无需要更新的系统节点');
$model = new SystemNode();
try {
if ($force == 1) {
$updateNodeList = $model->whereIn('node', array_column($nodeList, 'node'))->select();
$formatNodeList = array_format_key($nodeList, 'node');
foreach ($updateNodeList as $vo) {
isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->update([
'title' => $formatNodeList[$vo['node']]['title'],
'is_auth' => $formatNodeList[$vo['node']]['is_auth'],
]);
}
}
$existNodeList = $model->field('node,title,type,is_auth')->select();
foreach ($nodeList as $key => $vo) {
foreach ($existNodeList as $v) {
if ($vo['node'] == $v->node) {
unset($nodeList[$key]);
break;
}
}
}
$model->saveAll($nodeList);
TriggerService::updateNode();
} catch (\Exception $e) {
$this->error('节点更新失败');
}
$this->success('节点更新成功');
}
/**
* @NodeAnotation(title="清除失效节点")
*/
public function clearNode()
{
$this->checkPostRequest();
$nodeList = (new NodeService())->getNodelist();
$model = new SystemNode();
try {
$existNodeList = $model->field('id,node,title,type,is_auth')->select()->toArray();
$formatNodeList = array_format_key($nodeList, 'node');
foreach ($existNodeList as $vo) {
!isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->delete();
}
TriggerService::updateNode();
} catch (\Exception $e) {
$this->error('节点更新失败');
}
$this->success('节点更新成功');
}
}