a6f2284f51
# Conflicts: # app/common/controller/AuthController.php # app/common/logic/AuthLogic.php # app/common/model/User.php # app/controller/IndexController.php # app/functions.php # app/middleware/AuthCheck.php # app/view/index/qrcode.html # composer.json # composer.lock # config/middleware.php # config/route.php # config/whitelist.php
65 lines
1.5 KiB
PHP
65 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\logic\PermLogic;
|
|
use support\Cache;
|
|
|
|
class PermService
|
|
{
|
|
protected $logic;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->logic = (new PermLogic());
|
|
}
|
|
|
|
// 用户资源缓存key
|
|
protected static function cacheKey(int $userId): string
|
|
{
|
|
return "user_resources_{$userId}";
|
|
}
|
|
|
|
/**
|
|
* 获取用户所有资源(合并角色+直接分配),结果缓存
|
|
*/
|
|
public function getUserResources(int $userId)
|
|
{
|
|
$cacheKey = self::cacheKey($userId);
|
|
$cached = Cache::get($cacheKey);
|
|
if ($cached !== null) {
|
|
return $cached;
|
|
}
|
|
// 调用 Logic 层获取资源
|
|
$resources = $this->logic->getUserResources($userId);
|
|
// 缓存结果
|
|
Cache::set($cacheKey, $resources, 3600);
|
|
return $resources;
|
|
}
|
|
|
|
/**
|
|
* 构建目录/菜单树,节点包含权限与打开方式
|
|
*/
|
|
public function getMenuTree(int $userId): array
|
|
{
|
|
// 调用 Logic 层处理菜单构建
|
|
$resources = $this->getUserResources($userId);
|
|
return $this->logic->buildTree($resources);
|
|
}
|
|
|
|
/**
|
|
* 获取用户所有权限code
|
|
*/
|
|
public function getPermissionCodes(int $userId): array
|
|
{
|
|
return $this->logic->getPermissionCodes($userId);
|
|
}
|
|
|
|
/**
|
|
* 清除资源相关缓存
|
|
*/
|
|
public function clearCache(int $userId): void
|
|
{
|
|
Cache::delete(self::cacheKey($userId));
|
|
}
|
|
} |