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

@@ -1,64 +0,0 @@
<?php
namespace base\common\command;
use app\admin\model\SystemNode;
use app\admin\service\NodeService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
class NodeBase extends Command
{
protected function configure()
{
$this->setName('node')
->addOption('force', null, Option::VALUE_REQUIRED, '是否强制刷新', 0)
->setDescription('系统节点刷新服务');
}
protected function execute(Input $input, Output $output)
{
$force = $input->getOption('force');
$output->writeln('========正在刷新节点服务:=====' . date('Y-m-d H:i:s'));
$check = $this->refresh($force);
$check !== true && $output->writeln('节点刷新失败:' . $check);
$output->writeln('刷新完成:' . date('Y-m-d H:i:s'));
}
protected function refresh($force)
{
$nodeList = (new NodeService())->getNodelist();
if (empty($nodeList)) {
return true;
}
$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->insertAll($nodeList);
} catch (\Exception $e) {
return $e->getMessage();
}
return true;
}
}

View File

@@ -17,7 +17,7 @@ class VersionBase extends Command
public const PRODUCT_VERSION = '';
public const LAYUI_VERSION = '2.9.18';
public const LAYUI_VERSION = 'v2.10.1';
public const COMMENT = [
'版本更新说明:',

View File

@@ -48,7 +48,6 @@ class AdminControllerBase extends BaseController
'sort',
'remark',
'is_delete',
'is_auth',
'title',
];

View File

@@ -2,7 +2,10 @@
namespace base\common\service;
use app\admin\model\SystemAuthNode;
use app\admin\service\NodeService;
use app\common\constants\AdminConstant;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
@@ -26,7 +29,6 @@ class AuthServiceBase
'auth_on' => true, // 权限开关
'system_admin' => 'system_admin', // 用户表
'system_auth' => 'system_auth', // 权限表
'system_node' => 'system_node', // 节点表
'system_auth_node' => 'system_auth_node', // 权限-节点表
];
@@ -48,6 +50,8 @@ class AuthServiceBase
*/
protected $adminNode;
protected $nodeService;
/***
* 构造方法
* AuthService constructor.
@@ -58,6 +62,8 @@ class AuthServiceBase
*/
public function __construct($adminId = null)
{
$this->nodeService = new NodeService();
$this->adminId = $adminId;
$this->adminInfo = $this->getAdminInfo();
$this->nodeList = $this->getNodeList();
@@ -66,7 +72,7 @@ class AuthServiceBase
return $this;
}
public function isSuperAdmin()
public function isSuperAdmin()
{
return $this->adminId == AdminConstant::SUPER_ADMIN_ID;
}
@@ -100,7 +106,7 @@ class AuthServiceBase
return Config::get('admin.default_auth_check');
}
$nodeInfo = $this->nodeList[$node];
if ($nodeInfo['is_auth'] == 0) {
if (!$nodeInfo['auth']) {
return true;
}
// 用户验证,优先获取缓存信息
@@ -143,20 +149,7 @@ class AuthServiceBase
}
if (!empty($adminInfo) && !empty($adminInfo['auth_ids'])) {
$buildAuthSql = Db::name($this->config['system_auth'])
->distinct(true)
->whereIn('id', $adminInfo['auth_ids'])
->field('id')
->buildSql(true);
$buildAuthNodeSql = Db::name($this->config['system_auth_node'])
->distinct(true)
->where("auth_id IN {$buildAuthSql}")
->field('node_id')
->buildSql(true);
$nodeList = Db::name($this->config['system_node'])
->distinct(true)
->where("id IN {$buildAuthNodeSql}")
->column('node');
$nodeList = SystemAuthNode::where('auth_id', 'in', $adminInfo['auth_ids'])->cache(60)->column('node');
}
return $nodeList;
@@ -170,9 +163,15 @@ class AuthServiceBase
*/
public function getNodeList()
{
return Db::name($this->config['system_node'])
->autoCache(null, null, 'table')
->column('id,node,title,type,is_auth', 'node');
$cache_key = 'node_paris';
$node_list = Cache::get($cache_key);
if (!$node_list) {
$node_list = $this->nodeService->getNodeParis();
Cache::set($cache_key, $node_list, 60);
}
return $node_list;
}
/**