Commit 3da2449a151143333985f739545b762459e5aafe
1 parent
cb73e7fe
【修复】SYSTEM:获取菜单精简信息列表接口没有排除父 ID 非 0 的节点
Showing
2 changed files
with
20 additions
and
30 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.permission; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.util.ObjUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| ... | ... | @@ -14,7 +15,6 @@ import com.google.common.annotations.VisibleForTesting; |
| 14 | 15 | import com.google.common.collect.Lists; |
| 15 | 16 | import jakarta.annotation.Resource; |
| 16 | 17 | import lombok.extern.slf4j.Slf4j; |
| 17 | -import org.apache.commons.collections4.CollectionUtils; | |
| 18 | 18 | import org.springframework.cache.annotation.CacheEvict; |
| 19 | 19 | import org.springframework.cache.annotation.Cacheable; |
| 20 | 20 | import org.springframework.context.annotation.Lazy; |
| ... | ... | @@ -22,11 +22,10 @@ import org.springframework.stereotype.Service; |
| 22 | 22 | import org.springframework.transaction.annotation.Transactional; |
| 23 | 23 | |
| 24 | 24 | import java.util.*; |
| 25 | -import java.util.function.Function; | |
| 26 | -import java.util.stream.Collectors; | |
| 27 | 25 | |
| 28 | 26 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 29 | 27 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 28 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 30 | 29 | import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; |
| 31 | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 32 | 31 | |
| ... | ... | @@ -116,56 +115,47 @@ public class MenuServiceImpl implements MenuService { |
| 116 | 115 | return menus; |
| 117 | 116 | } |
| 118 | 117 | |
| 119 | - /** | |
| 120 | - * 过滤关闭的菜单节点及其子节点 | |
| 121 | - * | |
| 122 | - * @param menuList 所有菜单列表 | |
| 123 | - * @return List<MenuDO> 过滤后的菜单列表 | |
| 124 | - */ | |
| 125 | 118 | @Override |
| 126 | 119 | public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) { |
| 127 | - if(CollectionUtils.isEmpty(menuList)){ | |
| 120 | + if (CollUtil.isEmpty(menuList)){ | |
| 128 | 121 | return Collections.emptyList(); |
| 129 | 122 | } |
| 123 | + Map<Long, MenuDO> menuMap = convertMap(menuList, MenuDO::getId); | |
| 130 | 124 | |
| 131 | - Map<Long, MenuDO> menuMap = new HashMap<>(); | |
| 132 | - | |
| 133 | - for (MenuDO menuDO : menuList) { | |
| 134 | - menuMap.put(menuDO.getId(),menuDO); | |
| 135 | - } | |
| 136 | - | |
| 137 | - // 存下递归搜索过被禁用的菜单,防止重复的搜索 | |
| 138 | - Set<Long> disabledMenuIds = new HashSet<>(); | |
| 139 | - | |
| 125 | + // 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果 | |
| 140 | 126 | List<MenuDO> enabledMenus = new ArrayList<>(); |
| 127 | + Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索 | |
| 141 | 128 | for (MenuDO menu : menuList) { |
| 142 | - if (!isMenuDisabled(menu, menuMap, disabledMenuIds)) { | |
| 143 | - enabledMenus.add(menu); | |
| 129 | + if (isMenuDisabled(menu, menuMap, disabledMenuCache)) { | |
| 130 | + continue; | |
| 144 | 131 | } |
| 132 | + enabledMenus.add(menu); | |
| 145 | 133 | } |
| 146 | 134 | return enabledMenus; |
| 147 | 135 | } |
| 148 | 136 | |
| 149 | - private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuIds) { | |
| 150 | - if (disabledMenuIds.contains(node.getId())) { | |
| 137 | + private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuCache) { | |
| 138 | + // 如果已经判定是禁用的节点,直接结束 | |
| 139 | + if (disabledMenuCache.contains(node.getId())) { | |
| 151 | 140 | return true; |
| 152 | 141 | } |
| 153 | 142 | |
| 143 | + // 1. 遍历到 parentId 为根节点,则无需判断 | |
| 154 | 144 | Long parentId = node.getParentId(); |
| 155 | - if (parentId == 0) { | |
| 156 | - if (!node.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())) { | |
| 157 | - disabledMenuIds.add(node.getId()); | |
| 145 | + if (ObjUtil.equal(parentId, ID_ROOT)) { | |
| 146 | + if (CommonStatusEnum.isDisable(node.getStatus())) { | |
| 147 | + disabledMenuCache.add(node.getId()); | |
| 158 | 148 | return true; |
| 159 | 149 | } |
| 160 | 150 | return false; |
| 161 | 151 | } |
| 162 | 152 | |
| 153 | + // 2. 继续遍历 parent 节点 | |
| 163 | 154 | MenuDO parent = menuMap.get(parentId); |
| 164 | - if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuIds)) { | |
| 165 | - disabledMenuIds.add(node.getId()); | |
| 155 | + if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) { | |
| 156 | + disabledMenuCache.add(node.getId()); | |
| 166 | 157 | return true; |
| 167 | 158 | } |
| 168 | - | |
| 169 | 159 | return false; |
| 170 | 160 | } |
| 171 | 161 | ... | ... |