Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public CommonResult<AuthPermissionInfoRespVO> getPermissionInfo() {
// 1.3 获得菜单列表
Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId));
List<MenuDO> menuList = menuService.getMenuList(menuIds);
menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单
dongdongxiang marked this conversation as resolved.
Show resolved Hide resolved
// 过滤掉关闭的菜单
menuList = menuService.filterClosedMenus(menuList);

// 2. 拼接结果返回
return success(AuthConvert.INSTANCE.convert(user, roles, menuList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public CommonResult<List<MenuRespVO>> getMenuList(MenuListReqVO reqVO) {
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
List<MenuDO> list = menuService.getMenuListByTenant(
new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
// 过滤掉关闭的菜单及其子菜单
list = menuService.filterClosedMenus(list);
list.sort(Comparator.comparing(MenuDO::getSort));
return success(BeanUtils.toBean(list, MenuSimpleRespVO.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public interface MenuService {
*/
List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO);

/**
* 过滤掉关闭的菜单及其子菜单
*
* @param menuList
* @return
*/
List<MenuDO> filterClosedMenus(List<MenuDO> menuList);
dongdongxiang marked this conversation as resolved.
Show resolved Hide resolved

/**
* 筛选菜单列表
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.system.service.permission;

import cn.hutool.core.collection.CollUtil;
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;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO;
Expand All @@ -13,14 +14,15 @@
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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.*;
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;
Expand Down Expand Up @@ -106,12 +108,66 @@ public List<MenuDO> getMenuList() {

@Override
public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) {
// 查询所有菜单,并过滤掉关闭的节点
dongdongxiang marked this conversation as resolved.
Show resolved Hide resolved
List<MenuDO> menus = getMenuList(reqVO);
// 开启多租户的情况下,需要过滤掉未开通的菜单
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId())));
return menus;
}

/**
* 过滤关闭的菜单节点及其子节点
*
* @param menuList 所有菜单列表
* @return
*/
@Override
public List<MenuDO> filterClosedMenus(List<MenuDO> menuList) {
if(CollectionUtils.isEmpty(menuList)){
return Collections.emptyList();
}
List<MenuDO> allMenuList = getMenuList();

// 根据parentId快速查找子节点
dongdongxiang marked this conversation as resolved.
Show resolved Hide resolved
Map<Long, List<MenuDO>> childrenMap = allMenuList.stream()
.collect(Collectors.groupingBy(MenuDO::getParentId));

// 所有关闭的节点ID
Set<Long> closedNodeIds = new HashSet<>();

// 标记所有关闭的节点
for (MenuDO menu : allMenuList) {
if (!Objects.equals(menu.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
markClosedNodes(menu.getId(), childrenMap, closedNodeIds);
}
}
// 移除掉关闭的节点及其子节点
return menuList.stream()
.filter(menu -> !closedNodeIds.contains(menu.getId()))
.collect(Collectors.toList());
}

/**
* 递归标记关闭的节点及其子节点
*
* @param nodeId 节点ID
* @param childrenMap 子节点Map
* @param closedNodeIds 关闭节点ID集合
*/
private void markClosedNodes(Long nodeId, Map<Long,
List<MenuDO>> childrenMap,
Set<Long> closedNodeIds) {
// 如果已经标记过,则直接返回
if (!closedNodeIds.add(nodeId)) {
return;
}
List<MenuDO> children = childrenMap.getOrDefault(nodeId,Collections.emptyList());
for (MenuDO child : children) {
markClosedNodes(child.getId(), childrenMap, closedNodeIds);
}
}


@Override
public List<MenuDO> getMenuList(MenuListReqVO reqVO) {
return menuMapper.selectList(reqVO);
Expand Down