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
| @@ -56,7 +56,7 @@ public interface MenuService { | @@ -56,7 +56,7 @@ public interface MenuService { | ||
| 56 | * 过滤掉关闭的菜单及其子菜单 | 56 | * 过滤掉关闭的菜单及其子菜单 |
| 57 | * | 57 | * |
| 58 | * @param list 菜单列表 | 58 | * @param list 菜单列表 |
| 59 | - * @return List<MenuDO> 过滤后的菜单列表 | 59 | + * @return 过滤后的菜单列表 |
| 60 | */ | 60 | */ |
| 61 | List<MenuDO> filterDisableMenus(List<MenuDO> list); | 61 | List<MenuDO> filterDisableMenus(List<MenuDO> list); |
| 62 | 62 |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java
| 1 | package cn.iocoder.yudao.module.system.service.permission; | 1 | package cn.iocoder.yudao.module.system.service.permission; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.util.ObjUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | 7 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| @@ -14,7 +15,6 @@ import com.google.common.annotations.VisibleForTesting; | @@ -14,7 +15,6 @@ import com.google.common.annotations.VisibleForTesting; | ||
| 14 | import com.google.common.collect.Lists; | 15 | import com.google.common.collect.Lists; |
| 15 | import jakarta.annotation.Resource; | 16 | import jakarta.annotation.Resource; |
| 16 | import lombok.extern.slf4j.Slf4j; | 17 | import lombok.extern.slf4j.Slf4j; |
| 17 | -import org.apache.commons.collections4.CollectionUtils; | ||
| 18 | import org.springframework.cache.annotation.CacheEvict; | 18 | import org.springframework.cache.annotation.CacheEvict; |
| 19 | import org.springframework.cache.annotation.Cacheable; | 19 | import org.springframework.cache.annotation.Cacheable; |
| 20 | import org.springframework.context.annotation.Lazy; | 20 | import org.springframework.context.annotation.Lazy; |
| @@ -22,11 +22,10 @@ import org.springframework.stereotype.Service; | @@ -22,11 +22,10 @@ import org.springframework.stereotype.Service; | ||
| 22 | import org.springframework.transaction.annotation.Transactional; | 22 | import org.springframework.transaction.annotation.Transactional; |
| 23 | 23 | ||
| 24 | import java.util.*; | 24 | import java.util.*; |
| 25 | -import java.util.function.Function; | ||
| 26 | -import java.util.stream.Collectors; | ||
| 27 | 25 | ||
| 28 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 26 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 29 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 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 | import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; | 29 | import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; |
| 31 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 32 | 31 | ||
| @@ -116,56 +115,47 @@ public class MenuServiceImpl implements MenuService { | @@ -116,56 +115,47 @@ public class MenuServiceImpl implements MenuService { | ||
| 116 | return menus; | 115 | return menus; |
| 117 | } | 116 | } |
| 118 | 117 | ||
| 119 | - /** | ||
| 120 | - * 过滤关闭的菜单节点及其子节点 | ||
| 121 | - * | ||
| 122 | - * @param menuList 所有菜单列表 | ||
| 123 | - * @return List<MenuDO> 过滤后的菜单列表 | ||
| 124 | - */ | ||
| 125 | @Override | 118 | @Override |
| 126 | public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) { | 119 | public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) { |
| 127 | - if(CollectionUtils.isEmpty(menuList)){ | 120 | + if (CollUtil.isEmpty(menuList)){ |
| 128 | return Collections.emptyList(); | 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 | List<MenuDO> enabledMenus = new ArrayList<>(); | 126 | List<MenuDO> enabledMenus = new ArrayList<>(); |
| 127 | + Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索 | ||
| 141 | for (MenuDO menu : menuList) { | 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 | return enabledMenus; | 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 | return true; | 140 | return true; |
| 152 | } | 141 | } |
| 153 | 142 | ||
| 143 | + // 1. 遍历到 parentId 为根节点,则无需判断 | ||
| 154 | Long parentId = node.getParentId(); | 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 | return true; | 148 | return true; |
| 159 | } | 149 | } |
| 160 | return false; | 150 | return false; |
| 161 | } | 151 | } |
| 162 | 152 | ||
| 153 | + // 2. 继续遍历 parent 节点 | ||
| 163 | MenuDO parent = menuMap.get(parentId); | 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 | return true; | 157 | return true; |
| 167 | } | 158 | } |
| 168 | - | ||
| 169 | return false; | 159 | return false; |
| 170 | } | 160 | } |
| 171 | 161 |