From 312f7e4890a1d3b5102e54b199234c621f8e6887 Mon Sep 17 00:00:00 2001 From: "dongdong.xiang" Date: Sat, 15 Jun 2024 22:19:07 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=8F=9C=E5=8D=95=E7=B2=BE=E7=AE=80=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E7=88=B6=20ID=20=E9=9D=9E=200=20=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/auth/AuthController.java | 1 - .../admin/permission/MenuController.java | 2 +- .../service/permission/MenuServiceImpl.java | 58 +++++++++++++++++-- 3 files changed, 55 insertions(+), 6 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 cb002d3a2..8e2871e48 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,7 +109,6 @@ public class AuthController { // 1.3 获得菜单列表 Set menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); List menuList = menuService.getMenuList(menuIds); - menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单 // 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 104141751..1804096a1 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,7 @@ public class MenuController { "在多租户的场景下,会只返回租户所在套餐有的菜单") public CommonResult> getSimpleMenuList() { List list = menuService.getMenuListByTenant( - new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())); + new MenuListReqVO()); 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/MenuServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java index beee296d1..88aed2a92 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 @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.system.service.permission; import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO; @@ -13,14 +14,15 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Collection; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; @@ -106,12 +108,60 @@ public class MenuServiceImpl implements MenuService { @Override public List getMenuListByTenant(MenuListReqVO reqVO) { - List menus = getMenuList(reqVO); + // 查询所有菜单,并过滤掉关闭的节点 + List menus = filterClosedNodes(getMenuList(reqVO)); // 开启多租户的情况下,需要过滤掉未开通的菜单 tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); return menus; } + /** + * 过滤关闭的菜单节点及其子节点 + * + * @param menuList 所有菜单列表 + * @return 过滤后的菜单列表 + */ + public List filterClosedNodes(List menuList) { + // 根据parentId快速查找子节点 + Map> childrenMap = menuList.stream() + .collect(Collectors.groupingBy(MenuDO::getParentId)); + + // 所有关闭的节点ID + Set closedNodeIds = new HashSet<>(); + + // 标记所有关闭的节点 + for (MenuDO menu : menuList) { + if (Objects.equals(menu.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { + markClosedNodes(menu.getId(), childrenMap, closedNodeIds); + } + } + + // 过滤掉关闭的节点及其子节点 + return menuList.stream() + .filter(menu -> !closedNodeIds.contains(menu.getId())) + .collect(Collectors.toList()); + } + + /** + * 递归标记关闭的节点及其子节点 + * + * @param nodeId 节点ID + * @param childrenMap 子节点Map + * @param closedNodeIds 关闭节点ID集合 + */ + 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); + } + } + } + + @Override public List getMenuList(MenuListReqVO reqVO) { return menuMapper.selectList(reqVO); @@ -135,7 +185,7 @@ public class MenuServiceImpl implements MenuService { if (CollUtil.isEmpty(ids)) { return Lists.newArrayList(); } - return menuMapper.selectBatchIds(ids); + return filterClosedNodes(menuMapper.selectBatchIds(ids)); } /** From 6419aef36c7a8496407e538bfbfae102c8e49c16 Mon Sep 17 00:00:00 2001 From: "dongdong.xiang" Date: Sun, 16 Jun 2024 21:21:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=8F=9C=E5=8D=95=E7=B2=BE=E7=AE=80=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E7=88=B6=20ID=20=E9=9D=9E=200=20=E7=9A=84?= =?UTF-8?q?=E8=8A=82=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); } /** From 1c7ba5c0d6f2dbb95e916aa45eec32a94103a96f Mon Sep 17 00:00:00 2001 From: "dongdong.xiang" Date: Sun, 16 Jun 2024 22:41:32 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=8F=9C=E5=8D=95=E7=B2=BE=E7=AE=80=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E7=88=B6=20ID=20=E9=9D=9E=200=20=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/admin/auth/AuthController.java | 3 ++- .../module/system/service/permission/MenuServiceImpl.java | 8 +++++--- 2 files changed, 7 insertions(+), 4 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 20e9e9d6b..6ebaba212 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,9 +109,10 @@ public class AuthController { // 1.3 获得菜单列表 Set menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); List menuList = menuService.getMenuList(menuIds); - // 过滤掉关闭的菜单及其子菜单 + // 过滤掉关闭的菜单 menuList = menuService.filterClosedMenus(menuList); + menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单 // 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/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 bba798f84..104da073d 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 @@ -126,16 +126,18 @@ public class MenuServiceImpl implements MenuService { if(CollectionUtils.isEmpty(menuList)){ return Collections.emptyList(); } + List allMenuList = getMenuList(); + // 根据parentId快速查找子节点 - Map> childrenMap = menuList.stream() + Map> childrenMap = allMenuList.stream() .collect(Collectors.groupingBy(MenuDO::getParentId)); // 所有关闭的节点ID Set closedNodeIds = new HashSet<>(); // 标记所有关闭的节点 - for (MenuDO menu : menuList) { - if (Objects.equals(menu.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { + for (MenuDO menu : allMenuList) { + if (!Objects.equals(menu.getStatus(), CommonStatusEnum.ENABLE.getStatus())) { markClosedNodes(menu.getId(), childrenMap, closedNodeIds); } } From 7deaf1cdbf3f59151c2a466a5d227ab627a959a4 Mon Sep 17 00:00:00 2001 From: "dongdong.xiang" Date: Sun, 16 Jun 2024 22:45:02 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=8F=9C=E5=8D=95=E7=B2=BE=E7=AE=80=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E7=88=B6=20ID=20=E9=9D=9E=200=20=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/system/controller/admin/auth/AuthController.java | 1 - 1 file changed, 1 deletion(-) 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 6ebaba212..1192d15b0 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 @@ -112,7 +112,6 @@ public class AuthController { // 过滤掉关闭的菜单 menuList = menuService.filterClosedMenus(menuList); - menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单 // 2. 拼接结果返回 return success(AuthConvert.INSTANCE.convert(user, roles, menuList)); } From fbfa1050d2d587784ae33cf229bf9fbc79c39bf5 Mon Sep 17 00:00:00 2001 From: "dongdong.xiang" Date: Mon, 17 Jun 2024 22:19:47 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/auth/AuthController.java | 3 +- .../admin/permission/MenuController.java | 3 +- .../service/permission/MenuService.java | 6 +- .../service/permission/MenuServiceImpl.java | 69 ++++++++++--------- 4 files changed, 40 insertions(+), 41 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 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) {