Files
fdesk/app/common/service/PermService.php
T

65 lines
1.5 KiB
PHP
Raw Normal View History

2025-07-01 19:39:13 +08:00
<?php
namespace app\common\service;
2025-07-11 15:21:00 +08:00
use app\common\logic\PermLogic;
2025-07-01 19:39:13 +08:00
use support\Cache;
class PermService
{
2025-07-11 15:21:00 +08:00
protected $logic;
public function __construct()
{
$this->logic = (new PermLogic());
}
2025-07-01 19:39:13 +08:00
// 用户资源缓存key
protected static function cacheKey(int $userId): string
{
return "user_resources_{$userId}";
}
/**
* 获取用户所有资源(合并角色+直接分配),结果缓存
*/
2025-07-11 15:21:00 +08:00
public function getUserResources(int $userId)
2025-07-01 19:39:13 +08:00
{
$cacheKey = self::cacheKey($userId);
$cached = Cache::get($cacheKey);
if ($cached !== null) {
return $cached;
}
2025-07-11 15:21:00 +08:00
// 调用 Logic 层获取资源
$resources = $this->logic->getUserResources($userId);
// 缓存结果
Cache::set($cacheKey, $resources, 3600);
return $resources;
2025-07-01 19:39:13 +08:00
}
/**
* 构建目录/菜单树,节点包含权限与打开方式
*/
2025-07-11 15:21:00 +08:00
public function getMenuTree(int $userId): array
2025-07-01 19:39:13 +08:00
{
2025-07-11 15:21:00 +08:00
// 调用 Logic 层处理菜单构建
$resources = $this->getUserResources($userId);
return $this->logic->buildTree($resources);
2025-07-01 19:39:13 +08:00
}
/**
* 获取用户所有权限code
*/
2025-07-11 15:21:00 +08:00
public function getPermissionCodes(int $userId): array
2025-07-01 19:39:13 +08:00
{
2025-07-11 15:21:00 +08:00
return $this->logic->getPermissionCodes($userId);
2025-07-01 19:39:13 +08:00
}
/**
* 清除资源相关缓存
*/
2025-07-11 15:21:00 +08:00
public function clearCache(int $userId): void
2025-07-01 19:39:13 +08:00
{
Cache::delete(self::cacheKey($userId));
}
}