mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 01:01:52 +08:00
优化过滤菜单
This commit is contained in:
parent
7deaf1cdbf
commit
fbfa1050d2
@ -109,8 +109,7 @@ 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.filterDisableMenus(menuList);
|
||||||
menuList = menuService.filterClosedMenus(menuList);
|
|
||||||
|
|
||||||
// 2. 拼接结果返回
|
// 2. 拼接结果返回
|
||||||
return success(AuthConvert.INSTANCE.convert(user, roles, menuList));
|
return success(AuthConvert.INSTANCE.convert(user, roles, menuList));
|
||||||
|
@ -72,8 +72,7 @@ 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().setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
// 过滤掉关闭的菜单及其子菜单
|
list = menuService.filterDisableMenus(list);
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
@ -55,10 +55,10 @@ public interface MenuService {
|
|||||||
/**
|
/**
|
||||||
* 过滤掉关闭的菜单及其子菜单
|
* 过滤掉关闭的菜单及其子菜单
|
||||||
*
|
*
|
||||||
* @param menuList
|
* @param list 菜单列表
|
||||||
* @return
|
* @return List<MenuDO> 过滤后的菜单列表
|
||||||
*/
|
*/
|
||||||
List<MenuDO> filterClosedMenus(List<MenuDO> menuList);
|
List<MenuDO> filterDisableMenus(List<MenuDO> list);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 筛选菜单列表
|
* 筛选菜单列表
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
@ -119,54 +120,54 @@ public class MenuServiceImpl implements MenuService {
|
|||||||
* 过滤关闭的菜单节点及其子节点
|
* 过滤关闭的菜单节点及其子节点
|
||||||
*
|
*
|
||||||
* @param menuList 所有菜单列表
|
* @param menuList 所有菜单列表
|
||||||
* @return
|
* @return List<MenuDO> 过滤后的菜单列表
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<MenuDO> filterClosedMenus(List<MenuDO> menuList) {
|
public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) {
|
||||||
if(CollectionUtils.isEmpty(menuList)){
|
if(CollectionUtils.isEmpty(menuList)){
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
List<MenuDO> allMenuList = getMenuList();
|
|
||||||
|
|
||||||
// 根据parentId快速查找子节点
|
Map<Long, MenuDO> menuMap = new HashMap<>();
|
||||||
Map<Long, List<MenuDO>> childrenMap = allMenuList.stream()
|
|
||||||
.collect(Collectors.groupingBy(MenuDO::getParentId));
|
|
||||||
|
|
||||||
// 所有关闭的节点ID
|
for (MenuDO menuDO : menuList) {
|
||||||
Set<Long> closedNodeIds = new HashSet<>();
|
menuMap.put(menuDO.getId(),menuDO);
|
||||||
|
}
|
||||||
|
|
||||||
// 标记所有关闭的节点
|
// 存下递归搜索过被禁用的菜单,防止重复的搜索
|
||||||
for (MenuDO menu : allMenuList) {
|
Set<Long> disabledMenuIds = new HashSet<>();
|
||||||
if (!Objects.equals(menu.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
|
|
||||||
markClosedNodes(menu.getId(), childrenMap, closedNodeIds);
|
List<MenuDO> enabledMenus = new ArrayList<>();
|
||||||
|
for (MenuDO menu : menuList) {
|
||||||
|
if (!isMenuDisabled(menu, menuMap, disabledMenuIds)) {
|
||||||
|
enabledMenus.add(menu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 移除掉关闭的节点及其子节点
|
return enabledMenus;
|
||||||
return menuList.stream()
|
|
||||||
.filter(menu -> !closedNodeIds.contains(menu.getId()))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuIds) {
|
||||||
* 递归标记关闭的节点及其子节点
|
if (disabledMenuIds.contains(node.getId())) {
|
||||||
*
|
return true;
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
@Override
|
||||||
public List<MenuDO> getMenuList(MenuListReqVO reqVO) {
|
public List<MenuDO> getMenuList(MenuListReqVO reqVO) {
|
||||||
|
Loading…
Reference in New Issue
Block a user