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

65 lines
1.7 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
2025-07-25 18:14:39 +08:00
protected static function cacheKey(int $userId, string $systemCode): string
2025-07-01 19:39:13 +08:00
{
2025-07-25 18:14:39 +08:00
return "user_resources_{$userId}_{$systemCode}";
2025-07-01 19:39:13 +08:00
}
/**
* 获取用户所有资源(合并角色+直接分配),结果缓存
*/
2025-07-25 18:10:11 +08:00
public function getUserResources(int $userId, string $systemCode)
2025-07-01 19:39:13 +08:00
{
2025-07-25 18:14:39 +08:00
$cacheKey = self::cacheKey($userId, $systemCode);
2025-07-01 19:39:13 +08:00
$cached = Cache::get($cacheKey);
if ($cached !== null) {
return $cached;
}
2025-07-11 15:21:00 +08:00
// 调用 Logic 层获取资源
2025-07-25 18:10:11 +08:00
$resources = $this->logic->getUserResources($userId, $systemCode);
2025-07-11 15:21:00 +08:00
// 缓存结果
Cache::set($cacheKey, $resources, 3600);
return $resources;
2025-07-01 19:39:13 +08:00
}
/**
* 构建目录/菜单树,节点包含权限与打开方式
*/
2025-07-25 18:10:11 +08:00
public function getMenuTree(int $userId, string $systemCode): array
2025-07-01 19:39:13 +08:00
{
2025-07-11 15:21:00 +08:00
// 调用 Logic 层处理菜单构建
2025-07-25 18:10:11 +08:00
$resources = $this->getUserResources($userId, $systemCode);
2025-07-11 15:21:00 +08:00
return $this->logic->buildTree($resources);
2025-07-01 19:39:13 +08:00
}
/**
* 获取用户所有权限code
*/
2025-07-25 18:10:11 +08:00
public function getPermissionCodes(int $userId, string $systemCode): array
2025-07-01 19:39:13 +08:00
{
2025-07-25 18:10:11 +08:00
return $this->logic->getPermissionCodes($userId, $systemCode);
2025-07-01 19:39:13 +08:00
}
/**
* 清除资源相关缓存
*/
2025-07-25 18:14:39 +08:00
public function clearCache(int $userId, string $systemCode): void
2025-07-01 19:39:13 +08:00
{
2025-07-25 18:14:39 +08:00
Cache::delete(self::cacheKey($userId, $systemCode));
2025-07-01 19:39:13 +08:00
}
}