diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java index 1192d15b0..afdf82481 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java @@ -109,8 +109,7 @@ public class AuthController { // 1.3 获得菜单列表 Set menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); List menuList = menuService.getMenuList(menuIds); - // 过滤掉关闭的菜单 - menuList = menuService.filterClosedMenus(menuList); + menuList = menuService.filterDisableMenus(menuList); // 2. 拼接结果返回 return success(AuthConvert.INSTANCE.convert(user, roles, menuList)); diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java index 0ec9ac804..b6d067f1f 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java @@ -72,8 +72,7 @@ public class MenuController { public CommonResult> getSimpleMenuList() { List list = menuService.getMenuListByTenant( new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())); - // 过滤掉关闭的菜单及其子菜单 - list = menuService.filterClosedMenus(list); + list = menuService.filterDisableMenus(list); list.sort(Comparator.comparing(MenuDO::getSort)); return success(BeanUtils.toBean(list, MenuSimpleRespVO.class)); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java index ed21ce378..5c3700db9 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java @@ -55,10 +55,10 @@ public interface MenuService { /** * 过滤掉关闭的菜单及其子菜单 * - * @param menuList - * @return + * @param list 菜单列表 + * @return List 过滤后的菜单列表 */ - List filterClosedMenus(List menuList); + List filterDisableMenus(List list); /** * 筛选菜单列表 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java index 104da073d..7cc293258 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java @@ -22,6 +22,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -119,54 +120,54 @@ public class MenuServiceImpl implements MenuService { * 过滤关闭的菜单节点及其子节点 * * @param menuList 所有菜单列表 - * @return + * @return List 过滤后的菜单列表 */ @Override - public List filterClosedMenus(List menuList) { + public List filterDisableMenus(List menuList) { if(CollectionUtils.isEmpty(menuList)){ return Collections.emptyList(); } - List allMenuList = getMenuList(); - // 根据parentId快速查找子节点 - Map> childrenMap = allMenuList.stream() - .collect(Collectors.groupingBy(MenuDO::getParentId)); + Map menuMap = new HashMap<>(); - // 所有关闭的节点ID - Set closedNodeIds = new HashSet<>(); + for (MenuDO menuDO : menuList) { + menuMap.put(menuDO.getId(),menuDO); + } - // 标记所有关闭的节点 - for (MenuDO menu : allMenuList) { - if (!Objects.equals(menu.getStatus(), CommonStatusEnum.ENABLE.getStatus())) { - markClosedNodes(menu.getId(), childrenMap, closedNodeIds); + // 存下递归搜索过被禁用的菜单,防止重复的搜索 + Set disabledMenuIds = new HashSet<>(); + + List enabledMenus = new ArrayList<>(); + for (MenuDO menu : menuList) { + if (!isMenuDisabled(menu, menuMap, disabledMenuIds)) { + enabledMenus.add(menu); } } - // 移除掉关闭的节点及其子节点 - return menuList.stream() - .filter(menu -> !closedNodeIds.contains(menu.getId())) - .collect(Collectors.toList()); + return enabledMenus; } - /** - * 递归标记关闭的节点及其子节点 - * - * @param nodeId 节点ID - * @param childrenMap 子节点Map - * @param closedNodeIds 关闭节点ID集合 - */ - private void markClosedNodes(Long nodeId, Map> childrenMap, - Set closedNodeIds) { - // 如果已经标记过,则直接返回 - if (!closedNodeIds.add(nodeId)) { - return; + private boolean isMenuDisabled(MenuDO node, Map menuMap, Set disabledMenuIds) { + if (disabledMenuIds.contains(node.getId())) { + return true; } - List children = childrenMap.getOrDefault(nodeId,Collections.emptyList()); - for (MenuDO child : children) { - markClosedNodes(child.getId(), childrenMap, closedNodeIds); - } - } + Long parentId = node.getParentId(); + if (parentId == 0) { + if (!node.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())) { + disabledMenuIds.add(node.getId()); + return true; + } + return false; + } + + MenuDO parent = menuMap.get(parentId); + if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuIds)) { + disabledMenuIds.add(node.getId()); + return true; + } + + return false; + } @Override public List getMenuList(MenuListReqVO reqVO) {