1. 简化 ruoyi 获取菜单列表时,和前端耦合性比较重的情况

2. 删除 MenuDO 表非关键性字段,进一步简化模型
pull/2/head
YunaiV 2021-01-07 01:57:19 +08:00
parent c9372b0a5c
commit 3bf173d744
15 changed files with 131 additions and 329 deletions

View File

@ -1,40 +0,0 @@
package com.ruoyi.web.controller.system;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;
public class SysLoginController {
/**
*
*
* @return
*/
@GetMapping("getRouters")
public AjaxResult getRouters()
{
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
// 用户信息
SysUser user = loginUser.getUser();
List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
return AjaxResult.success(menuService.buildMenus(menus));
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.web.controller.system;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
@ -33,8 +34,7 @@ import com.ruoyi.system.service.ISysMenuService;
*/
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController
{
public class SysMenuController extends BaseController {
@Autowired
private ISysMenuService menuService;
@ -46,8 +46,7 @@ public class SysMenuController extends BaseController
*/
@PreAuthorize("@ss.hasPermi('system:menu:list')")
@GetMapping("/list")
public AjaxResult list(SysMenu menu)
{
public AjaxResult list(SysMenu menu) {
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
Long userId = loginUser.getUser().getUserId();
List<SysMenu> menus = menuService.selectMenuList(menu, userId);
@ -59,8 +58,7 @@ public class SysMenuController extends BaseController
*/
@PreAuthorize("@ss.hasPermi('system:menu:query')")
@GetMapping(value = "/{menuId}")
public AjaxResult getInfo(@PathVariable Long menuId)
{
public AjaxResult getInfo(@PathVariable Long menuId) {
return AjaxResult.success(menuService.selectMenuById(menuId));
}
@ -68,8 +66,7 @@ public class SysMenuController extends BaseController
*
*/
@GetMapping("/treeselect")
public AjaxResult treeselect(SysMenu menu)
{
public AjaxResult treeselect(SysMenu menu) {
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
Long userId = loginUser.getUser().getUserId();
List<SysMenu> menus = menuService.selectMenuList(menu, userId);
@ -80,8 +77,7 @@ public class SysMenuController extends BaseController
*
*/
@GetMapping(value = "/roleMenuTreeselect/{roleId}")
public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
{
public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
List<SysMenu> menus = menuService.selectMenuList(loginUser.getUser().getUserId());
AjaxResult ajax = AjaxResult.success();
@ -96,15 +92,11 @@ public class SysMenuController extends BaseController
@PreAuthorize("@ss.hasPermi('system:menu:add')")
@Log(title = "菜单管理", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody SysMenu menu)
{
if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
{
public AjaxResult add(@Validated @RequestBody SysMenu menu) {
if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
}
else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
&& !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
{
} else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
&& !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) {
return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败地址必须以http(s)://开头");
}
menu.setCreateBy(SecurityUtils.getUsername());
@ -117,19 +109,13 @@ public class SysMenuController extends BaseController
@PreAuthorize("@ss.hasPermi('system:menu:edit')")
@Log(title = "菜单管理", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@Validated @RequestBody SysMenu menu)
{
if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
{
public AjaxResult edit(@Validated @RequestBody SysMenu menu) {
if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
}
else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
&& !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
{
} else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
&& !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) {
return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败地址必须以http(s)://开头");
}
else if (menu.getMenuId().equals(menu.getParentId()))
{
} else if (menu.getMenuId().equals(menu.getParentId())) {
return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己");
}
menu.setUpdateBy(SecurityUtils.getUsername());
@ -142,14 +128,11 @@ public class SysMenuController extends BaseController
@PreAuthorize("@ss.hasPermi('system:menu:remove')")
@Log(title = "菜单管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{menuId}")
public AjaxResult remove(@PathVariable("menuId") Long menuId)
{
if (menuService.hasChildByMenuId(menuId))
{
public AjaxResult remove(@PathVariable("menuId") Long menuId) {
if (menuService.hasChildByMenuId(menuId)) {
return AjaxResult.error("存在子菜单,不允许删除");
}
if (menuService.checkMenuExistRole(menuId))
{
if (menuService.checkMenuExistRole(menuId)) {
return AjaxResult.error("菜单已分配,不允许删除");
}
return toAjax(menuService.deleteMenuById(menuId));

View File

@ -18,7 +18,7 @@ export function login(username, password, code, uuid) {
// 获取用户详细信息
export function getInfo() {
return request({
url: '/get-info',
url: '/get-permission-info',
method: 'get'
})
}

View File

@ -3,7 +3,7 @@ import request from '@/utils/request'
// 获取路由
export const getRouters = () => {
return request({
url: '/get-routers',
url: '/list-menus',
method: 'get'
})
}

View File

@ -43,18 +43,27 @@ const permission = {
// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap, isRewrite = false) {
return asyncRouterMap.filter(route => {
if (isRewrite && route.children) {
route.children = filterChildren(route.children)
// 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处
// 处理 meta 属性
route.meta = {
title: route.menuName,
icon: route.icon
}
if (route.component) {
// Layout ParentView 组件特殊处理
if (route.component === 'Layout') {
// 处理 component 属性
if (route.children) { // 父节点
// debugger
if (route.parentId === 0) {
route.component = Layout
} else if (route.component === 'ParentView') {
route.component = ParentView
} else {
route.component = ParentView
}
} else { // 根节点
route.component = loadView(route.component)
}
// filterChildren
if (isRewrite && route.children) {
route.children = filterChildren(route.children)
}
if (route.children != null && route.children && route.children.length) {
route.children = filterAsyncRouter(route.children, route, isRewrite)

View File

@ -9,12 +9,12 @@ Content-Type: application/json
"code": "1024"
}
### 请求 /get-info 接口 => 成功
GET {{baseUrl}}/get-info
### 请求 /get-permission-info 接口 => 成功
GET {{baseUrl}}/get-permission-info
Authorization: Bearer {{token}}
### 请求 /get-routers 接口 => 成功
GET {{baseUrl}}/get-routers
### 请求 /list-menus 接口 => 成功
GET {{baseUrl}}/list-menus
Authorization: Bearer {{token}}
### 请求 /druid/xxx 接口 => 失败 TODO 临时测试

View File

@ -1,8 +1,8 @@
package cn.iocoder.dashboard.modules.system.controller.auth;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthLoginReqVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthLoginRespVO;
import cn.iocoder.dashboard.modules.system.service.auth.SysAuthService;
@ -35,17 +35,17 @@ public class SysAuthController {
return success(SysAuthLoginRespVO.builder().token(token).build());
}
@ApiOperation("获取登陆用户的信息")
@GetMapping("/get-info")
public CommonResult<SysAuthGetInfoRespVO> getInfo() {
SysAuthGetInfoRespVO respVO = authService.getInfo(getLoginUserId(), getLoginUserRoleIds());
@ApiOperation("获取登陆用户的权限信息")
@GetMapping("/get-permission-info")
public CommonResult<SysAuthPermissionInfoRespVO> getPermissionInfo() {
SysAuthPermissionInfoRespVO respVO = authService.getPermissionInfo(getLoginUserId(), getLoginUserRoleIds());
return success(respVO);
}
@ApiOperation("获得菜单 Vue 路由")
@GetMapping("get-routers")
public CommonResult<List<SysAuthGetRouterRespVO>> getRouters() {
List<SysAuthGetRouterRespVO> respVOList = authService.getRouters(getLoginUserId(), getLoginUserRoleIds());
@ApiOperation("获得登陆用户的菜单列表")
@GetMapping("list-menus")
public CommonResult<List<SysAuthMenuRespVO>> listMenus() {
List<SysAuthMenuRespVO> respVOList = authService.listMenus(getLoginUserId(), getLoginUserRoleIds());
return success(respVOList);
}

View File

@ -1,93 +0,0 @@
package cn.iocoder.dashboard.modules.system.controller.auth.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
*
*
* @author ruoyi
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SysAuthGetRouterRespVO {
/**
*
*/
private Long menuId;
/**
*
*/
private Long parentId;
/**
*
*/
private String name;
/**
*
*/
private String path;
/**
* true
*/
private boolean hidden;
/**
* noRedirect
*/
private String redirect;
/**
*
*/
private String component;
/**
* children 1--
*/
private Boolean alwaysShow;
/**
*
*/
private MetaVO meta;
/**
*
*/
private List<SysAuthGetRouterRespVO> children;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class MetaVO {
/**
*
*/
private String title;
/**
* src/assets/icons/svg
*/
private String icon;
/**
* true <keep-alive>
*/
private boolean noCache;
}
}

View File

@ -0,0 +1,51 @@
package cn.iocoder.dashboard.modules.system.controller.auth.vo;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@ApiModel("登陆用户的菜单信息 Response VO")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SysAuthMenuRespVO {
/**
*
*/
private Long menuId;
/**
*
*/
private Long parentId;
/**
*
*/
private String menuName;
/**
*
*/
private String path;
/**
*
*/
private String component;
/**
*
*/
private String icon;
/**
*
*/
private List<SysAuthMenuRespVO> children;
}

View File

@ -9,20 +9,20 @@ import lombok.NoArgsConstructor;
import java.util.Set;
@ApiModel("获得用户信息 Resp VO")
@ApiModel(value = "登陆用户的权限信息 Response VO", description = "额外包括用户信息和角色列表")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SysAuthGetInfoRespVO {
public class SysAuthPermissionInfoRespVO {
@ApiModelProperty(value = "用户信息", required = true)
private UserVO user;
@ApiModelProperty(value = "角色权限数组", required = true)
@ApiModelProperty(value = "角色标识数组", required = true)
private Set<String> roles;
@ApiModelProperty(value = "菜单权限数组", required = true)
@ApiModelProperty(value = "操作权限数组", required = true)
private Set<String> permissions;
@ApiModel("用户信息 VO")

View File

@ -1,17 +1,12 @@
package cn.iocoder.dashboard.modules.system.convert.auth;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.modules.system.enums.permission.MenuIdEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum;
import cn.iocoder.dashboard.util.collection.CollectionUtils;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@ -21,107 +16,19 @@ import java.util.List;
@Mapper
public interface SysAuthConvert {
/** Layout组件标识 */
public final static String LAYOUT = "Layout";
/** ParentView组件标识 */
public final static String PARENT_VIEW = "ParentView";
SysAuthConvert INSTANCE = Mappers.getMapper(SysAuthConvert.class);
@Mapping(source = "updateTime", target = "updateTime", ignore = true) // 字段相同,但是含义不同,忽略
LoginUser convert(SysUserDO bean);
default SysAuthGetInfoRespVO convert(SysUserDO user, List<SysRoleDO> roleList, List<SysMenuDO> menuList) {
return SysAuthGetInfoRespVO.builder()
.user(SysAuthGetInfoRespVO.UserVO.builder().nickname(user.getNickname()).avatar(user.getAvatar()).build())
default SysAuthPermissionInfoRespVO convert(SysUserDO user, List<SysRoleDO> roleList, List<SysMenuDO> menuList) {
return SysAuthPermissionInfoRespVO.builder()
.user(SysAuthPermissionInfoRespVO.UserVO.builder().nickname(user.getNickname()).avatar(user.getAvatar()).build())
.roles(CollectionUtils.convertSet(roleList, SysRoleDO::getRoleKey))
.permissions(CollectionUtils.convertSet(menuList, SysMenuDO::getPerms))
.permissions(CollectionUtils.convertSet(menuList, SysMenuDO::getPermission))
.build();
}
default SysAuthGetRouterRespVO convertTreeNode(SysMenuDO menu) {
SysAuthGetRouterRespVO.SysAuthGetRouterRespVOBuilder respVOBuilder = SysAuthGetRouterRespVO.builder();
respVOBuilder.menuId(menu.getMenuId()).parentId(menu.getParentId())
.hidden("1".equals(menu.getVisible())) // TODO 芋艿:需要处理
.name(getRouteName(menu)).path(menu.getPath()).component(getComponent(menu))
.meta(SysAuthGetRouterRespVO.MetaVO.builder().title(menu.getMenuName()).icon(menu.getIcon())
.noCache("1".equals(menu.getIsCache())).build())
;
return respVOBuilder.build();
}
// TODO 芋艿:需要预处理。存储的时候
default String getRouteName(SysMenuDO menu) {
String routerName = StringUtil.capitalize(menu.getPath()); // TODO 芋艿:看看怎么去除掉
// 非外链并且是一级目录(类型为目录)
if (isMenuFrame(menu)) {
routerName = StringUtils.EMPTY;
}
return routerName;
}
/**
*
*
* @param menu
* @return
*/
// TODO 芋艿:思考下是不是可以重构下
default boolean isMenuFrame(SysMenuDO menu) {
return MenuIdEnum.ROOT.getId().equals(menu.getParentId())
&& MenuTypeEnum.MENU.getType().equals(menu.getMenuType())
&& CommonStatusEnum.ENABLE.getStatus().equals(menu.getIsFrame()); // TODO 芋艿:思考是不是用这个好
}
// /** // TODO 芋艿:后面重构下
// * 获取路由地址
// *
// * @param menu 菜单信息
// * @return 路由地址
// */
// default String getRouterPath(SysMenuDO menu) {
// String routerPath = menu.getPath();
// // 非外链并且是一级目录(类型为目录)
// if (MenuIdEnum.ROOT.getId().equals(menu.getParentId())
// && MenuTypeEnum.MENU.getType().equals(menu.getMenuType())
// && CommonStatusEnum.DISABLE.getStatus().equals(menu.getIsFrame())) {
// routerPath = "/" + menu.getPath();
// }
// // 非外链并且是一级目录(类型为菜单)
// else if (isMenuFrame(menu)) {
// routerPath = "/";
// }
// return routerPath;
// }
/**
*
*
* @param menu
* @return
*/
default String getComponent(SysMenuDO menu) {
String component = LAYOUT;
if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu)) {
component = menu.getComponent();
} else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu)) {
component = PARENT_VIEW;
}
return component;
}
/**
* parent_view
*
* @param menu
* @return
*/
default boolean isParentView(SysMenuDO menu) {
return !MenuIdEnum.ROOT.getId().equals(menu.getParentId())
&& MenuTypeEnum.DIR.getType().equals(menu.getMenuType());
}
SysAuthMenuRespVO convertTreeNode(SysMenuDO menu);
}

View File

@ -55,27 +55,12 @@ public class SysMenuDO extends BaseDO {
@Size(max = 200, message = "组件路径不能超过255个字符")
private String component;
/**
* 0 1
*/
private String isFrame;
/**
* 0 1
*/
private String isCache;
/**
* M C F
*/
@NotBlank(message = "菜单类型不能为空")
private String menuType;
/**
* 0 1
*/
private String visible;
/**
* 0 1
*/
@ -85,7 +70,7 @@ public class SysMenuDO extends BaseDO {
*
*/
@Size(max = 100, message = "权限标识长度不能超过100个字符")
private String perms;
private String permission;
/**
*

View File

@ -1,8 +1,8 @@
package cn.iocoder.dashboard.modules.system.service.auth;
import cn.iocoder.dashboard.framework.security.core.service.SecurityFrameworkService;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO;
import java.util.List;
import java.util.Set;
@ -28,7 +28,7 @@ public interface SysAuthService extends SecurityFrameworkService {
* @param roleIds
* @return
*/
SysAuthGetInfoRespVO getInfo(Long userId, Set<Long> roleIds);
SysAuthPermissionInfoRespVO getPermissionInfo(Long userId, Set<Long> roleIds);
/**
* Vue
@ -40,6 +40,6 @@ public interface SysAuthService extends SecurityFrameworkService {
* @param roleIds
* @return Vue
*/
List<SysAuthGetRouterRespVO> getRouters(Long userId, Set<Long> roleIds);
List<SysAuthMenuRespVO> listMenus(Long userId, Set<Long> roleIds);
}

View File

@ -5,8 +5,8 @@ import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.security.config.SecurityProperties;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO;
import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
@ -225,7 +225,7 @@ public class SysAuthServiceImpl implements SysAuthService {
}
@Override
public SysAuthGetInfoRespVO getInfo(Long userId, Set<Long> roleIds) {
public SysAuthPermissionInfoRespVO getPermissionInfo(Long userId, Set<Long> roleIds) {
// 获得用户信息
SysUserDO user = userService.getUser(userId);
if (user == null) {
@ -242,7 +242,7 @@ public class SysAuthServiceImpl implements SysAuthService {
}
@Override
public List<SysAuthGetRouterRespVO> getRouters(Long userId, Set<Long> roleIds) {
public List<SysAuthMenuRespVO> listMenus(Long userId, Set<Long> roleIds) {
List<SysMenuDO> menuList = permissionService.listRoleMenusFromCache(roleIds,
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType()),
SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus()));
@ -250,17 +250,17 @@ public class SysAuthServiceImpl implements SysAuthService {
return buildRouterTree(menuList);
}
private static List<SysAuthGetRouterRespVO> buildRouterTree(List<SysMenuDO> menuList) {
private static List<SysAuthMenuRespVO> buildRouterTree(List<SysMenuDO> menuList) {
// 排序,保证菜单的有序性
menuList.sort(Comparator.comparing(SysMenuDO::getOrderNum));
// 构建菜单树
// 使用 LinkedHashMap 的原因,是为了排序 。实际也可以用 Stream API ,就是太丑了。
Map<Long, SysAuthGetRouterRespVO> treeNodeMap = new LinkedHashMap<>();
Map<Long, SysAuthMenuRespVO> treeNodeMap = new LinkedHashMap<>();
menuList.forEach(menu -> treeNodeMap.put(menu.getMenuId(), SysAuthConvert.INSTANCE.convertTreeNode(menu)));
// 处理父子关系
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(MenuIdEnum.ROOT.getId())).forEach((childNode) -> {
// 获得父节点
SysAuthGetRouterRespVO parentNode = treeNodeMap.get(childNode.getParentId());
SysAuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
if (parentNode == null) {
log.error("[buildRouterTree][resource({}) 找不到父资源({})]", childNode.getMenuId(), childNode.getParentId());
return;

View File

@ -34,7 +34,7 @@ public class SysMenuServiceImpl implements SysMenuService {
private volatile Map<Long, SysMenuDO> menuCache;
/**
*
* key {@link SysMenuDO#getPerms()}
* key {@link SysMenuDO#getPermission()}
* valueSysMenuDO SysMenuDO
*
* volatile
@ -55,7 +55,7 @@ public class SysMenuServiceImpl implements SysMenuService {
ImmutableMultimap.Builder<String, SysMenuDO> permMenuCacheBuilder = ImmutableMultimap.builder();
menuList.forEach(menuDO -> {
menuCacheBuilder.put(menuDO.getMenuId(), menuDO);
permMenuCacheBuilder.put(menuDO.getPerms(), menuDO);
permMenuCacheBuilder.put(menuDO.getPermission(), menuDO);
});
menuCache = menuCacheBuilder.build();
permMenuCache = permMenuCacheBuilder.build();