From 3da2449a151143333985f739545b762459e5aafe Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 18 Jun 2024 09:20:49 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91SYSTEM?= =?UTF-8?q?=EF=BC=9A=E8=8E=B7=E5=8F=96=E8=8F=9C=E5=8D=95=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=8E=92=E9=99=A4=E7=88=B6=20ID=20=E9=9D=9E=200=20?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/permission/MenuService.java | 2 +- .../service/permission/MenuServiceImpl.java | 48 ++++++++----------- 2 files changed, 20 insertions(+), 30 deletions(-) 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 5c3700db9..d74dc6134 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 @@ -56,7 +56,7 @@ public interface MenuService { * 过滤掉关闭的菜单及其子菜单 * * @param list 菜单列表 - * @return List 过滤后的菜单列表 + * @return 过滤后的菜单列表 */ 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 7cc293258..730958f82 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.hutool.core.util.ObjUtil; 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; @@ -14,7 +15,6 @@ 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; @@ -22,11 +22,10 @@ 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; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; @@ -116,56 +115,47 @@ public class MenuServiceImpl implements MenuService { return menus; } - /** - * 过滤关闭的菜单节点及其子节点 - * - * @param menuList 所有菜单列表 - * @return List 过滤后的菜单列表 - */ @Override public List filterDisableMenus(List menuList) { - if(CollectionUtils.isEmpty(menuList)){ + if (CollUtil.isEmpty(menuList)){ return Collections.emptyList(); } + Map menuMap = convertMap(menuList, MenuDO::getId); - Map menuMap = new HashMap<>(); - - for (MenuDO menuDO : menuList) { - menuMap.put(menuDO.getId(),menuDO); - } - - // 存下递归搜索过被禁用的菜单,防止重复的搜索 - Set disabledMenuIds = new HashSet<>(); - + // 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果 List enabledMenus = new ArrayList<>(); + Set disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索 for (MenuDO menu : menuList) { - if (!isMenuDisabled(menu, menuMap, disabledMenuIds)) { - enabledMenus.add(menu); + if (isMenuDisabled(menu, menuMap, disabledMenuCache)) { + continue; } + enabledMenus.add(menu); } return enabledMenus; } - private boolean isMenuDisabled(MenuDO node, Map menuMap, Set disabledMenuIds) { - if (disabledMenuIds.contains(node.getId())) { + private boolean isMenuDisabled(MenuDO node, Map menuMap, Set disabledMenuCache) { + // 如果已经判定是禁用的节点,直接结束 + if (disabledMenuCache.contains(node.getId())) { return true; } + // 1. 遍历到 parentId 为根节点,则无需判断 Long parentId = node.getParentId(); - if (parentId == 0) { - if (!node.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())) { - disabledMenuIds.add(node.getId()); + if (ObjUtil.equal(parentId, ID_ROOT)) { + if (CommonStatusEnum.isDisable(node.getStatus())) { + disabledMenuCache.add(node.getId()); return true; } return false; } + // 2. 继续遍历 parent 节点 MenuDO parent = menuMap.get(parentId); - if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuIds)) { - disabledMenuIds.add(node.getId()); + if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) { + disabledMenuCache.add(node.getId()); return true; } - return false; }