find($userId); if (!$user) { return collect(); } $all = collect(); foreach ($user->roles as $role) { $all = $all->merge($role->resources); } $all = $all->merge($user->resources); $unique = $all->unique('id')->values(); Cache::set($cacheKey, $unique, 3600); return $unique; } /** * 构建目录/菜单树,节点包含权限与打开方式 */ public static function getMenuTree(int $userId): array { $resources = self::getUserResources($userId); $menus = $resources ->filter(fn($r) => in_array($r->type, ['directory','menu'])) ->values(); return self::buildTree($menus, $resources); } protected static function buildTree($nodes, $allResources, int $parentId = 0): array { $tree = []; foreach ($nodes as $node) { if ($node->parent_id !== $parentId) continue; $children = self::buildTree($nodes, $allResources, $node->id); $authList = $allResources ->filter(fn($r) => $r->type === 'permission' && $r->parent_id === $node->id) ->map(fn($r) => [ 'id' => $r->id, 'title' => $r->name, 'auth_mark' => $r->code, ])->values()->all(); $tree[] = [ 'id' => $node->id, 'name' => $node->name, 'path' => $node->url, 'meta' => [ 'title' => $node->name, 'icon' => $node->icon, 'keepAlive' => true, 'authList' => $authList, 'openType' => $node->open_type, // 'internal' | 'external' | 'iframe' ], 'children' => $children, ]; } return $tree; } /** * 获取用户所有权限code */ public static function getPermissionCodes(int $userId): array { return self::getUserResources($userId) ->filter(fn($res) => $res->type === 'permission') ->pluck('code') ->unique() ->values() ->all(); } /** * 清除资源相关缓存 */ public static function clearCache(int $userId): void { Cache::delete(self::cacheKey($userId)); } }