Commit ae9c5f704870b63fa994de44a9377803f5facd27
Committed by
GitHub
Merge pull request #561 from dongdongxiang/master_jdk17_fix_menu_v20240615
【修复】获取菜单精简信息列表接口没有排除父 ID 非 0 的节点
Showing
4 changed files
with
69 additions
and
3 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
| @@ -109,7 +109,7 @@ public class AuthController { | @@ -109,7 +109,7 @@ public class AuthController { | ||
| 109 | // 1.3 获得菜单列表 | 109 | // 1.3 获得菜单列表 |
| 110 | Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); | 110 | Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); |
| 111 | List<MenuDO> menuList = menuService.getMenuList(menuIds); | 111 | List<MenuDO> menuList = menuService.getMenuList(menuIds); |
| 112 | - menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单 | 112 | + menuList = menuService.filterDisableMenus(menuList); |
| 113 | 113 | ||
| 114 | // 2. 拼接结果返回 | 114 | // 2. 拼接结果返回 |
| 115 | return success(AuthConvert.INSTANCE.convert(user, roles, menuList)); | 115 | return success(AuthConvert.INSTANCE.convert(user, roles, menuList)); |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java
| @@ -72,6 +72,7 @@ public class MenuController { | @@ -72,6 +72,7 @@ public class MenuController { | ||
| 72 | public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() { | 72 | public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() { |
| 73 | List<MenuDO> list = menuService.getMenuListByTenant( | 73 | List<MenuDO> list = menuService.getMenuListByTenant( |
| 74 | new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())); | 74 | new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 75 | + list = menuService.filterDisableMenus(list); | ||
| 75 | list.sort(Comparator.comparing(MenuDO::getSort)); | 76 | list.sort(Comparator.comparing(MenuDO::getSort)); |
| 76 | return success(BeanUtils.toBean(list, MenuSimpleRespVO.class)); | 77 | return success(BeanUtils.toBean(list, MenuSimpleRespVO.class)); |
| 77 | } | 78 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java
| @@ -53,6 +53,14 @@ public interface MenuService { | @@ -53,6 +53,14 @@ public interface MenuService { | ||
| 53 | List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO); | 53 | List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO); |
| 54 | 54 | ||
| 55 | /** | 55 | /** |
| 56 | + * 过滤掉关闭的菜单及其子菜单 | ||
| 57 | + * | ||
| 58 | + * @param list 菜单列表 | ||
| 59 | + * @return List<MenuDO> 过滤后的菜单列表 | ||
| 60 | + */ | ||
| 61 | + List<MenuDO> filterDisableMenus(List<MenuDO> list); | ||
| 62 | + | ||
| 63 | + /** | ||
| 56 | * 筛选菜单列表 | 64 | * 筛选菜单列表 |
| 57 | * | 65 | * |
| 58 | * @param reqVO 筛选条件请求 VO | 66 | * @param reqVO 筛选条件请求 VO |
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.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||
| 4 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 5 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 5 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | 6 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| 6 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO; | 7 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO; |
| @@ -13,14 +14,16 @@ import com.google.common.annotations.VisibleForTesting; | @@ -13,14 +14,16 @@ import com.google.common.annotations.VisibleForTesting; | ||
| 13 | import com.google.common.collect.Lists; | 14 | import com.google.common.collect.Lists; |
| 14 | import jakarta.annotation.Resource; | 15 | import jakarta.annotation.Resource; |
| 15 | import lombok.extern.slf4j.Slf4j; | 16 | import lombok.extern.slf4j.Slf4j; |
| 17 | +import org.apache.commons.collections4.CollectionUtils; | ||
| 16 | import org.springframework.cache.annotation.CacheEvict; | 18 | import org.springframework.cache.annotation.CacheEvict; |
| 17 | import org.springframework.cache.annotation.Cacheable; | 19 | import org.springframework.cache.annotation.Cacheable; |
| 18 | import org.springframework.context.annotation.Lazy; | 20 | import org.springframework.context.annotation.Lazy; |
| 19 | import org.springframework.stereotype.Service; | 21 | import org.springframework.stereotype.Service; |
| 20 | import org.springframework.transaction.annotation.Transactional; | 22 | import org.springframework.transaction.annotation.Transactional; |
| 21 | 23 | ||
| 22 | -import java.util.Collection; | ||
| 23 | -import java.util.List; | 24 | +import java.util.*; |
| 25 | +import java.util.function.Function; | ||
| 26 | +import java.util.stream.Collectors; | ||
| 24 | 27 | ||
| 25 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 28 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 26 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 29 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| @@ -106,12 +109,66 @@ public class MenuServiceImpl implements MenuService { | @@ -106,12 +109,66 @@ public class MenuServiceImpl implements MenuService { | ||
| 106 | 109 | ||
| 107 | @Override | 110 | @Override |
| 108 | public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) { | 111 | public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) { |
| 112 | + // 查询所有菜单,并过滤掉关闭的节点 | ||
| 109 | List<MenuDO> menus = getMenuList(reqVO); | 113 | List<MenuDO> menus = getMenuList(reqVO); |
| 110 | // 开启多租户的情况下,需要过滤掉未开通的菜单 | 114 | // 开启多租户的情况下,需要过滤掉未开通的菜单 |
| 111 | tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); | 115 | tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
| 112 | return menus; | 116 | return menus; |
| 113 | } | 117 | } |
| 114 | 118 | ||
| 119 | + /** | ||
| 120 | + * 过滤关闭的菜单节点及其子节点 | ||
| 121 | + * | ||
| 122 | + * @param menuList 所有菜单列表 | ||
| 123 | + * @return List<MenuDO> 过滤后的菜单列表 | ||
| 124 | + */ | ||
| 125 | + @Override | ||
| 126 | + public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) { | ||
| 127 | + if(CollectionUtils.isEmpty(menuList)){ | ||
| 128 | + return Collections.emptyList(); | ||
| 129 | + } | ||
| 130 | + | ||
| 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 | + | ||
| 140 | + List<MenuDO> enabledMenus = new ArrayList<>(); | ||
| 141 | + for (MenuDO menu : menuList) { | ||
| 142 | + if (!isMenuDisabled(menu, menuMap, disabledMenuIds)) { | ||
| 143 | + enabledMenus.add(menu); | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + return enabledMenus; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuIds) { | ||
| 150 | + if (disabledMenuIds.contains(node.getId())) { | ||
| 151 | + return true; | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + Long parentId = node.getParentId(); | ||
| 155 | + if (parentId == 0) { | ||
| 156 | + if (!node.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())) { | ||
| 157 | + disabledMenuIds.add(node.getId()); | ||
| 158 | + return true; | ||
| 159 | + } | ||
| 160 | + return false; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + MenuDO parent = menuMap.get(parentId); | ||
| 164 | + if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuIds)) { | ||
| 165 | + disabledMenuIds.add(node.getId()); | ||
| 166 | + return true; | ||
| 167 | + } | ||
| 168 | + | ||
| 169 | + return false; | ||
| 170 | + } | ||
| 171 | + | ||
| 115 | @Override | 172 | @Override |
| 116 | public List<MenuDO> getMenuList(MenuListReqVO reqVO) { | 173 | public List<MenuDO> getMenuList(MenuListReqVO reqVO) { |
| 117 | return menuMapper.selectList(reqVO); | 174 | return menuMapper.selectList(reqVO); |