From 6419aef36c7a8496407e538bfbfae102c8e49c16 Mon Sep 17 00:00:00 2001 From: "dongdong.xiang" Date: Sun, 16 Jun 2024 21:21:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=8F=9C=E5=8D=95=E7=B2=BE=E7=AE=80=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=B2=A1=E6=9C=89=E6=8E=92?= =?UTF-8?q?=E9=99=A4=E7=88=B6=20ID=20=E9=9D=9E=200=20=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/auth/AuthController.java | 2 ++ .../admin/permission/MenuController.java | 4 ++- .../service/permission/MenuService.java | 8 ++++++ .../service/permission/MenuServiceImpl.java | 28 +++++++++++-------- 4 files changed, 29 insertions(+), 13 deletions(-) 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 8e2871e48..20e9e9d6b 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,6 +109,8 @@ public class AuthController { // 1.3 获得菜单列表 Set menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); List menuList = menuService.getMenuList(menuIds); + // 过滤掉关闭的菜单及其子菜单 + menuList = menuService.filterClosedMenus(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 1804096a1..0ec9ac804 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 @@ -71,7 +71,9 @@ public class MenuController { "在多租户的场景下,会只返回租户所在套餐有的菜单") public CommonResult> getSimpleMenuList() { List list = menuService.getMenuListByTenant( - new MenuListReqVO()); + new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())); + // 过滤掉关闭的菜单及其子菜单 + list = menuService.filterClosedMenus(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 adc2b3151..ed21ce378 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 @@ -52,6 +52,14 @@ public interface MenuService { */ List getMenuListByTenant(MenuListReqVO reqVO); + /** + * 过滤掉关闭的菜单及其子菜单 + * + * @param menuList + * @return + */ + List filterClosedMenus(List menuList); + /** * 筛选菜单列表 * 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 88aed2a92..bba798f84 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 @@ -109,7 +109,7 @@ public class MenuServiceImpl implements MenuService { @Override public List getMenuListByTenant(MenuListReqVO reqVO) { // 查询所有菜单,并过滤掉关闭的节点 - List menus = filterClosedNodes(getMenuList(reqVO)); + List menus = getMenuList(reqVO); // 开启多租户的情况下,需要过滤掉未开通的菜单 tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); return menus; @@ -119,9 +119,13 @@ public class MenuServiceImpl implements MenuService { * 过滤关闭的菜单节点及其子节点 * * @param menuList 所有菜单列表 - * @return 过滤后的菜单列表 + * @return */ - public List filterClosedNodes(List menuList) { + @Override + public List filterClosedMenus(List menuList) { + if(CollectionUtils.isEmpty(menuList)){ + return Collections.emptyList(); + } // 根据parentId快速查找子节点 Map> childrenMap = menuList.stream() .collect(Collectors.groupingBy(MenuDO::getParentId)); @@ -135,8 +139,7 @@ public class MenuServiceImpl implements MenuService { markClosedNodes(menu.getId(), childrenMap, closedNodeIds); } } - - // 过滤掉关闭的节点及其子节点 + // 移除掉关闭的节点及其子节点 return menuList.stream() .filter(menu -> !closedNodeIds.contains(menu.getId())) .collect(Collectors.toList()); @@ -152,12 +155,13 @@ public class MenuServiceImpl implements MenuService { private void markClosedNodes(Long nodeId, Map> childrenMap, Set closedNodeIds) { - closedNodeIds.add(nodeId); - List children = childrenMap.get(nodeId); - if (CollectionUtils.isNotEmpty(children)) { - for (MenuDO child : children) { - markClosedNodes(child.getId(), childrenMap, closedNodeIds); - } + // 如果已经标记过,则直接返回 + if (!closedNodeIds.add(nodeId)) { + return; + } + List children = childrenMap.getOrDefault(nodeId,Collections.emptyList()); + for (MenuDO child : children) { + markClosedNodes(child.getId(), childrenMap, closedNodeIds); } } @@ -185,7 +189,7 @@ public class MenuServiceImpl implements MenuService { if (CollUtil.isEmpty(ids)) { return Lists.newArrayList(); } - return filterClosedNodes(menuMapper.selectBatchIds(ids)); + return menuMapper.selectBatchIds(ids); } /**