Skip to content

Commit

Permalink
Hotfix/后台权限菜单遍历问题 (#56)
Browse files Browse the repository at this point in the history
* 解决后台权限菜单遍历过慢的问题

* 修改没有赋值权限的账号获取菜单异常问题

Co-authored-by: zhongshaofa <shaofa.zhong@happy-seed.com>
  • Loading branch information
zhongshaofa and zhongshaofa committed Jan 7, 2021
1 parent a303531 commit 598b4ad
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
3 changes: 2 additions & 1 deletion app/admin/middleware/CheckAdmin.php
Expand Up @@ -30,7 +30,8 @@ public function handle(Request $request, \Closure $next)
$adminConfig = config('admin');
$adminId = session('admin.id');
$expireTime = session('admin.expire_time');
$authService = new AuthService($adminId);
/** @var AuthService $authService */
$authService = app(AuthService::class, ['adminId' => $adminId]);
$currentNode = $authService->getCurrentNode();
$currentController = parse_name($request->controller());

Expand Down
66 changes: 55 additions & 11 deletions app/common/service/AuthService.php
Expand Up @@ -42,14 +42,38 @@ class AuthService
'system_auth_node' => 'system_auth_node',// 权限-节点表
];

/**
* 管理员信息
* @var array|\think\Model|null
*/
protected $adminInfo;

/**
* 所有节点信息
* @var array
*/
protected $nodeList;

/**
* 管理员所有授权节点
* @var array
*/
protected $adminNode;

/***
* 构造方法
* AuthService constructor.
* @param null $adminId
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function __construct($adminId = null)
{
$this->adminId = $adminId;
$this->adminInfo = $this->getAdminInfo();
$this->nodeList = $this->getNodeList();
$this->adminNode = $this->getAdminNode();
return $this;
}

Expand Down Expand Up @@ -78,25 +102,19 @@ public function checkNode($node = null)
$node = $this->parseNodeStr($node);
}
// 判断是否加入节点控制,优先获取缓存信息
$nodeInfo = Db::name($this->config['system_node'])
->where(['node' => $node])
->find();
if (empty($nodeInfo)) {
if (!isset($this->nodeList[$node])) {
return false;
}
$nodeInfo = $this->nodeList[$node];
if ($nodeInfo['is_auth'] == 0) {
return true;
}
// 用户验证,优先获取缓存信息
$adminInfo = Db::name($this->config['system_admin'])
->where('id', $this->adminId)
->find();
if (empty($adminInfo) || $adminInfo['status'] != 1 || empty($adminInfo['auth_ids'])) {
if (empty($this->adminInfo) || $this->adminInfo['status'] != 1 || empty($this->adminInfo['auth_ids'])) {
return false;
}
// 判断该节点是否允许访问
$allNode = $this->getAdminNode();
if (in_array($node, $allNode)) {
if (in_array($node, $this->adminNode)) {
return true;
}
return false;
Expand Down Expand Up @@ -127,7 +145,7 @@ public function getAdminNode()
'id' => $this->adminId,
'status' => 1,
])->find();
if (!empty($adminInfo)) {
if (!empty($adminInfo) && !empty($adminInfo['auth_ids'])) {
$buildAuthSql = Db::name($this->config['system_auth'])
->distinct(true)
->whereIn('id', $adminInfo['auth_ids'])
Expand All @@ -146,6 +164,32 @@ public function getAdminNode()
return $nodeList;
}

/**
* 获取所有节点信息
* @time 2021-01-07
* @return array
* @author zhongshaofa <shaofa.zhong@happy-seed.com>
*/
public function getNodeList(){
return Db::name($this->config['system_node'])
->column('id,node,title,type,is_auth','node');
}

/**
* 获取管理员信息
* @time 2021-01-07
* @return array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author zhongshaofa <shaofa.zhong@happy-seed.com>
*/
public function getAdminInfo(){
return Db::name($this->config['system_admin'])
->where('id', $this->adminId)
->find();
}

/**
* 驼峰转下划线规则
* @param string $node
Expand Down
10 changes: 5 additions & 5 deletions app/common/service/MenuService.php
Expand Up @@ -57,20 +57,20 @@ public function getHomeInfo()
*/
public function getMenuTree()
{
$menuTreeList = $this->buildMenuChild(0, $this->getMenuData());
return $menuTreeList;
/** @var AuthService $authService */
$authServer = app(AuthService::class, ['adminId' => $this->adminId]);
return $this->buildMenuChild(0, $this->getMenuData(),$authServer);
}

private function buildMenuChild($pid, $menuList)
private function buildMenuChild($pid, $menuList, AuthService $authServer)
{
$treeList = [];
$authServer = (new AuthService($this->adminId));
foreach ($menuList as &$v) {
$check = empty($v['href']) ? true : $authServer->checkNode($v['href']);
!empty($v['href']) && $v['href'] = __url($v['href']);
if ($pid == $v['pid'] && $check) {
$node = $v;
$child = $this->buildMenuChild($v['id'], $menuList);
$child = $this->buildMenuChild($v['id'], $menuList, $authServer);
if (!empty($child)) {
$node['child'] = $child;
}
Expand Down

0 comments on commit 598b4ad

Please sign in to comment.