优化过滤菜单

This commit is contained in:
dongdong.xiang 2024-06-17 22:19:47 +08:00
parent 7deaf1cdbf
commit fbfa1050d2
4 changed files with 40 additions and 41 deletions

View File

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

View File

@ -72,8 +72,7 @@ public class MenuController {
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
List<MenuDO> list = menuService.getMenuListByTenant(
new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
// 过滤掉关闭的菜单及其子菜单
list = menuService.filterClosedMenus(list);
list = menuService.filterDisableMenus(list);
list.sort(Comparator.comparing(MenuDO::getSort));
return success(BeanUtils.toBean(list, MenuSimpleRespVO.class));
}

View File

@ -55,10 +55,10 @@ public interface MenuService {
/**
* 过滤掉关闭的菜单及其子菜单
*
* @param menuList
* @return
* @param list 菜单列表
* @return List<MenuDO> 过滤后的菜单列表
*/
List<MenuDO> filterClosedMenus(List<MenuDO> menuList);
List<MenuDO> filterDisableMenus(List<MenuDO> list);
/**
* 筛选菜单列表

View File

@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -119,54 +120,54 @@ public class MenuServiceImpl implements MenuService {
* 过滤关闭的菜单节点及其子节点
*
* @param menuList 所有菜单列表
* @return
* @return List<MenuDO> 过滤后的菜单列表
*/
@Override
public List<MenuDO> filterClosedMenus(List<MenuDO> menuList) {
public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) {
if(CollectionUtils.isEmpty(menuList)){
return Collections.emptyList();
}
List<MenuDO> allMenuList = getMenuList();
// 根据parentId快速查找子节点
Map<Long, List<MenuDO>> childrenMap = allMenuList.stream()
.collect(Collectors.groupingBy(MenuDO::getParentId));
Map<Long, MenuDO> menuMap = new HashMap<>();
// 所有关闭的节点ID
Set<Long> closedNodeIds = new HashSet<>();
for (MenuDO menuDO : menuList) {
menuMap.put(menuDO.getId(),menuDO);
}
// 标记所有关闭的节点
for (MenuDO menu : allMenuList) {
if (!Objects.equals(menu.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
markClosedNodes(menu.getId(), childrenMap, closedNodeIds);
// 存下递归搜索过被禁用的菜单防止重复的搜索
Set<Long> disabledMenuIds = new HashSet<>();
List<MenuDO> enabledMenus = new ArrayList<>();
for (MenuDO menu : menuList) {
if (!isMenuDisabled(menu, menuMap, disabledMenuIds)) {
enabledMenus.add(menu);
}
}
// 移除掉关闭的节点及其子节点
return menuList.stream()
.filter(menu -> !closedNodeIds.contains(menu.getId()))
.collect(Collectors.toList());
return enabledMenus;
}
/**
* 递归标记关闭的节点及其子节点
*
* @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;
private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuIds) {
if (disabledMenuIds.contains(node.getId())) {
return true;
}
List<MenuDO> children = childrenMap.getOrDefault(nodeId,Collections.emptyList());
for (MenuDO child : children) {
markClosedNodes(child.getId(), childrenMap, closedNodeIds);
}
}
Long parentId = node.getParentId();
if (parentId == 0) {
if (!node.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())) {
disabledMenuIds.add(node.getId());
return true;
}
return false;
}
MenuDO parent = menuMap.get(parentId);
if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuIds)) {
disabledMenuIds.add(node.getId());
return true;
}
return false;
}
@Override
public List<MenuDO> getMenuList(MenuListReqVO reqVO) {