feat(menu): 添加菜单的父路径信息
- 在 MenuDO 和 AuthPermissionInfoRespVO.MenuVO 中添加 parentPath 字段 - 在构建菜单树时,为每个菜单设置父菜单路径 - 优化菜单查询逻辑,批量获取父菜单信息
This commit is contained in:
parent
fd3e8fd23f
commit
23bab6a228
@ -62,6 +62,9 @@ public class AuthPermissionInfoRespVO {
|
||||
@Schema(description = "父菜单 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long parentId;
|
||||
|
||||
@Schema(description = "父菜单 路由地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private String parentPath;
|
||||
|
||||
@Schema(description = "菜单名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道")
|
||||
private String name;
|
||||
|
||||
|
@ -59,7 +59,11 @@ public interface AuthConvert {
|
||||
// 构建菜单树
|
||||
// 使用 LinkedHashMap 的原因,是为了排序 。实际也可以用 Stream API ,就是太丑了。
|
||||
Map<Long, AuthPermissionInfoRespVO.MenuVO> treeNodeMap = new LinkedHashMap<>();
|
||||
menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
|
||||
menuList.forEach(menu -> {
|
||||
AuthPermissionInfoRespVO.MenuVO menuVO = AuthConvert.INSTANCE.convertTreeNode(menu);
|
||||
menuVO.setParentPath(menu.getParentPath());
|
||||
treeNodeMap.put(menu.getId(), menuVO);
|
||||
});
|
||||
// 处理父子关系
|
||||
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
|
||||
// 获得父节点
|
||||
|
@ -59,6 +59,10 @@ public class MenuDO extends BaseDO {
|
||||
* 父菜单ID
|
||||
*/
|
||||
private Long parentId;
|
||||
/**
|
||||
* 父菜单 路由地址
|
||||
*/
|
||||
private String parentPath;
|
||||
/**
|
||||
* 路由地址
|
||||
*
|
||||
|
@ -22,6 +22,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
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;
|
||||
@ -184,7 +186,37 @@ public class MenuServiceImpl implements MenuService {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
return menuMapper.selectBatchIds(ids);
|
||||
|
||||
// 根据 ids 获取菜单列表
|
||||
List<MenuDO> dos = menuMapper.selectBatchIds(ids);
|
||||
if (CollUtil.isEmpty(dos)) {
|
||||
return dos;
|
||||
}
|
||||
|
||||
// 过滤出父ID不为空且不为0的菜单,获取父菜单ID列表
|
||||
List<Long> parentMenuIds = dos.stream()
|
||||
.filter(menu -> menu.getParentId() != null && menu.getParentId() != 0)
|
||||
.map(MenuDO::getParentId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollUtil.isNotEmpty(parentMenuIds)) {
|
||||
// 根据父菜单ID列表获取父菜单列表,并转换为Map
|
||||
Map<Long, MenuDO> parentMenuMap = menuMapper.selectBatchIds(parentMenuIds).stream()
|
||||
.collect(Collectors.toMap(MenuDO::getId, Function.identity()));
|
||||
|
||||
// 为每个菜单设置父菜单路径
|
||||
dos.forEach(menu -> {
|
||||
if (menu.getParentId() != null && menu.getParentId() != 0) {
|
||||
MenuDO parentMenu = parentMenuMap.get(menu.getParentId());
|
||||
if (parentMenu != null) {
|
||||
menu.setParentPath(parentMenu.getPath());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return dos;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user