privileges; // 检查是否有对应控制器的权限 return isset($privileges[$controller]) && is_array($privileges[$controller]) && !empty($privileges[$controller]); } /** * 获取菜单树形结构(用于前端显示) */ public static function getMenuTree($userInfo): array { $menus = self::getMenusByPermission($userInfo); return self::buildMenuTree($menus); } /** * 构建菜单树形结构 */ private static function buildMenuTree($menus): array { $tree = []; foreach ($menus as $menu) { $item = [ 'id' => $menu['id'], 'title' => $menu['title'], 'icon' => $menu['icon'] ?? '', 'url' => $menu['url'] ?? '', 'controller' => $menu['controller'] ?? '', 'level' => $menu['level'], 'sort' => $menu['sort'] ?? 0, ]; if (isset($menu['children']) && is_array($menu['children'])) { $item['children'] = self::buildMenuTree($menu['children']); $item['spread'] = false; // 默认不展开 } $tree[] = $item; } // 按sort字段排序 usort($tree, function($a, $b) { return ($a['sort'] ?? 0) - ($b['sort'] ?? 0); }); return $tree; } /** * 获取所有控制器权限配置 */ public static function getControllerPermissions(): array { return Config::get('menu.controller_permissions', []); } /** * 根据菜单ID获取菜单信息 */ public static function getMenuById($menuId): ?array { $allMenus = self::getAllMenus(); return self::findMenuById($allMenus, $menuId); } /** * 递归查找菜单 */ private static function findMenuById($menus, $menuId): ?array { foreach ($menus as $menu) { if ($menu['id'] == $menuId) { return $menu; } if (isset($menu['children']) && is_array($menu['children'])) { $found = self::findMenuById($menu['children'], $menuId); if ($found) { return $found; } } } return null; } /** * 获取面包屑导航 */ public static function getBreadcrumb($menuId): array { $breadcrumb = []; $allMenus = self::getAllMenus(); $path = self::getMenuPath($allMenus, $menuId); foreach ($path as $menu) { $breadcrumb[] = [ 'id' => $menu['id'], 'title' => $menu['title'], 'url' => $menu['url'] ?? '' ]; } return $breadcrumb; } /** * 获取菜单路径 */ private static function getMenuPath($menus, $menuId, $path = []): array { foreach ($menus as $menu) { $currentPath = array_merge($path, [$menu]); if ($menu['id'] == $menuId) { return $currentPath; } if (isset($menu['children']) && is_array($menu['children'])) { $found = self::getMenuPath($menu['children'], $menuId, $currentPath); if (!empty($found)) { return $found; } } } return []; } }