| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- declare (strict_types=1);
- namespace app\service;
- use think\facade\Config;
- class MenuService
- {
- /**
- * 获取所有菜单配置
- */
- public static function getAllMenus(): array
- {
- return Config::get('menu.menus', []);
- }
- /**
- * 根据用户权限过滤菜单
- */
- public static function getMenusByPermission($userInfo): array
- {
- $allMenus = self::getAllMenus();
- return self::filterMenusByPermission($allMenus, $userInfo);
- }
- /**
- * 递归过滤菜单权限
- */
- private static function filterMenusByPermission($menus, $userInfo): array
- {
- $filteredMenus = [];
-
- foreach ($menus as $menu) {
- // 如果菜单指定了控制器,需要检查权限
- if (!empty($menu['controller'])) {
- if (!self::hasControllerPermission($userInfo, $menu['controller'])) {
- continue;
- }
- }
-
- // 处理子菜单
- if (isset($menu['children']) && is_array($menu['children'])) {
- $children = self::filterMenusByPermission($menu['children'], $userInfo);
- // 如果有子菜单权限,才显示父菜单
- if (!empty($children)) {
- $menu['children'] = $children;
- $filteredMenus[] = $menu;
- }
- } else {
- // 没有子菜单的情况
- if (empty($menu['controller']) || self::hasControllerPermission($userInfo, $menu['controller'])) {
- $filteredMenus[] = $menu;
- }
- }
- }
-
- return $filteredMenus;
- }
- /**
- * 检查用户是否有控制器权限
- */
- private static function hasControllerPermission($userInfo, $controller): bool
- {
- // 超级管理员拥有所有权限
- $superAdminRoleId = Config::get('permission.super_admin_role_id', 1);
- if ($userInfo['user_role'] == $superAdminRoleId) {
- return true;
- }
- // 获取用户角色权限
- $role = \app\model\UserRoleModel::getRoleById($userInfo['user_role'], $userInfo['merchant_id']);
- if (!$role) {
- return false;
- }
- $privileges = $role->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 [];
- }
- }
|