【修复】获取菜单精简信息列表接口没有排除父 ID 非 0 的节点

This commit is contained in:
dongdong.xiang 2024-06-16 21:21:55 +08:00
parent 312f7e4890
commit 6419aef36c
4 changed files with 29 additions and 13 deletions

View File

@ -109,6 +109,8 @@ public class AuthController {
// 1.3 获得菜单列表 // 1.3 获得菜单列表
Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId));
List<MenuDO> menuList = menuService.getMenuList(menuIds); List<MenuDO> menuList = menuService.getMenuList(menuIds);
// 过滤掉关闭的菜单及其子菜单
menuList = menuService.filterClosedMenus(menuList);
// 2. 拼接结果返回 // 2. 拼接结果返回
return success(AuthConvert.INSTANCE.convert(user, roles, menuList)); return success(AuthConvert.INSTANCE.convert(user, roles, menuList));

View File

@ -71,7 +71,9 @@ public class MenuController {
"在多租户的场景下,会只返回租户所在套餐有的菜单") "在多租户的场景下,会只返回租户所在套餐有的菜单")
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() { public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
List<MenuDO> list = menuService.getMenuListByTenant( List<MenuDO> list = menuService.getMenuListByTenant(
new MenuListReqVO()); new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
// 过滤掉关闭的菜单及其子菜单
list = menuService.filterClosedMenus(list);
list.sort(Comparator.comparing(MenuDO::getSort)); list.sort(Comparator.comparing(MenuDO::getSort));
return success(BeanUtils.toBean(list, MenuSimpleRespVO.class)); return success(BeanUtils.toBean(list, MenuSimpleRespVO.class));
} }

View File

@ -52,6 +52,14 @@ public interface MenuService {
*/ */
List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO); List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO);
/**
* 过滤掉关闭的菜单及其子菜单
*
* @param menuList
* @return
*/
List<MenuDO> filterClosedMenus(List<MenuDO> menuList);
/** /**
* 筛选菜单列表 * 筛选菜单列表
* *

View File

@ -109,7 +109,7 @@ public class MenuServiceImpl implements MenuService {
@Override @Override
public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) { public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) {
// 查询所有菜单并过滤掉关闭的节点 // 查询所有菜单并过滤掉关闭的节点
List<MenuDO> menus = filterClosedNodes(getMenuList(reqVO)); List<MenuDO> menus = getMenuList(reqVO);
// 开启多租户的情况下需要过滤掉未开通的菜单 // 开启多租户的情况下需要过滤掉未开通的菜单
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId())));
return menus; return menus;
@ -119,9 +119,13 @@ public class MenuServiceImpl implements MenuService {
* 过滤关闭的菜单节点及其子节点 * 过滤关闭的菜单节点及其子节点
* *
* @param menuList 所有菜单列表 * @param menuList 所有菜单列表
* @return 过滤后的菜单列表 * @return
*/ */
public List<MenuDO> filterClosedNodes(List<MenuDO> menuList) { @Override
public List<MenuDO> filterClosedMenus(List<MenuDO> menuList) {
if(CollectionUtils.isEmpty(menuList)){
return Collections.emptyList();
}
// 根据parentId快速查找子节点 // 根据parentId快速查找子节点
Map<Long, List<MenuDO>> childrenMap = menuList.stream() Map<Long, List<MenuDO>> childrenMap = menuList.stream()
.collect(Collectors.groupingBy(MenuDO::getParentId)); .collect(Collectors.groupingBy(MenuDO::getParentId));
@ -135,8 +139,7 @@ public class MenuServiceImpl implements MenuService {
markClosedNodes(menu.getId(), childrenMap, closedNodeIds); markClosedNodes(menu.getId(), childrenMap, closedNodeIds);
} }
} }
// 移除掉关闭的节点及其子节点
// 过滤掉关闭的节点及其子节点
return menuList.stream() return menuList.stream()
.filter(menu -> !closedNodeIds.contains(menu.getId())) .filter(menu -> !closedNodeIds.contains(menu.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -152,12 +155,13 @@ public class MenuServiceImpl implements MenuService {
private void markClosedNodes(Long nodeId, Map<Long, private void markClosedNodes(Long nodeId, Map<Long,
List<MenuDO>> childrenMap, List<MenuDO>> childrenMap,
Set<Long> closedNodeIds) { Set<Long> closedNodeIds) {
closedNodeIds.add(nodeId); // 如果已经标记过则直接返回
List<MenuDO> children = childrenMap.get(nodeId); if (!closedNodeIds.add(nodeId)) {
if (CollectionUtils.isNotEmpty(children)) { return;
for (MenuDO child : children) { }
markClosedNodes(child.getId(), childrenMap, closedNodeIds); List<MenuDO> 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)) { if (CollUtil.isEmpty(ids)) {
return Lists.newArrayList(); return Lists.newArrayList();
} }
return filterClosedNodes(menuMapper.selectBatchIds(ids)); return menuMapper.selectBatchIds(ids);
} }
/** /**