refactor: ✂️递归获取菜单、部门属性列表代码重构优化
This commit is contained in:
@@ -73,4 +73,9 @@ public class SysMenu extends BaseEntity {
|
|||||||
*/
|
*/
|
||||||
private String redirect;
|
private String redirect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 父节点路径,以英文逗号(,)分割
|
||||||
|
*/
|
||||||
|
private String treePath;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -20,14 +20,11 @@ public class MenuVO {
|
|||||||
@Schema(description = "菜单名称")
|
@Schema(description = "菜单名称")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Schema(description = "ICON")
|
@Schema(description="菜单类型")
|
||||||
private String icon;
|
private MenuTypeEnum type;
|
||||||
|
|
||||||
@Schema(description = "路由名称")
|
@Schema(description = "路由路径")
|
||||||
private String routeName;
|
private String path;
|
||||||
|
|
||||||
@Schema(description = "路由相对路径")
|
|
||||||
private String routePath;
|
|
||||||
|
|
||||||
@Schema(description = "组件路径")
|
@Schema(description = "组件路径")
|
||||||
private String component;
|
private String component;
|
||||||
@@ -38,12 +35,12 @@ public class MenuVO {
|
|||||||
@Schema(description = "菜单是否可见(1:显示;0:隐藏)")
|
@Schema(description = "菜单是否可见(1:显示;0:隐藏)")
|
||||||
private Integer visible;
|
private Integer visible;
|
||||||
|
|
||||||
|
@Schema(description = "ICON")
|
||||||
|
private String icon;
|
||||||
|
|
||||||
@Schema(description = "跳转路径")
|
@Schema(description = "跳转路径")
|
||||||
private String redirect;
|
private String redirect;
|
||||||
|
|
||||||
@Schema(description="菜单类型")
|
|
||||||
private MenuTypeEnum type;
|
|
||||||
|
|
||||||
@Schema(description="按钮权限标识")
|
@Schema(description="按钮权限标识")
|
||||||
private String perm;
|
private String perm;
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
package com.youlai.system.service.impl;
|
package com.youlai.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.lang.Validator;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.youlai.system.common.constant.SystemConstants;
|
import com.youlai.system.common.constant.SystemConstants;
|
||||||
import com.youlai.system.common.enums.StatusEnum;
|
import com.youlai.system.common.enums.StatusEnum;
|
||||||
import com.youlai.system.pojo.vo.Option;
|
|
||||||
import com.youlai.system.converter.DeptConverter;
|
import com.youlai.system.converter.DeptConverter;
|
||||||
import com.youlai.system.mapper.SysDeptMapper;
|
import com.youlai.system.mapper.SysDeptMapper;
|
||||||
import com.youlai.system.pojo.entity.SysDept;
|
import com.youlai.system.pojo.entity.SysDept;
|
||||||
import com.youlai.system.pojo.form.DeptForm;
|
import com.youlai.system.pojo.form.DeptForm;
|
||||||
import com.youlai.system.pojo.query.DeptQuery;
|
import com.youlai.system.pojo.query.DeptQuery;
|
||||||
import com.youlai.system.pojo.vo.DeptVO;
|
import com.youlai.system.pojo.vo.DeptVO;
|
||||||
|
import com.youlai.system.pojo.vo.Option;
|
||||||
import com.youlai.system.service.SysDeptService;
|
import com.youlai.system.service.SysDeptService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,35 +52,20 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
.orderByAsc(SysDept::getSort)
|
.orderByAsc(SysDept::getSort)
|
||||||
);
|
);
|
||||||
|
|
||||||
List<DeptVO> list = new ArrayList<>();
|
Set<Long> deptIds = deptList.stream()
|
||||||
|
|
||||||
if (CollectionUtil.isNotEmpty(deptList)) {
|
|
||||||
|
|
||||||
Set<Long> cacheDeptIds = deptList.stream()
|
|
||||||
.map(SysDept::getId)
|
.map(SysDept::getId)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
Set<Long> parentIds = deptList.stream()
|
||||||
|
.map(SysDept::getParentId)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
for (SysDept dept : deptList) {
|
List<Long> rootIds = CollectionUtil.subtractToList(parentIds, deptIds);
|
||||||
Long parentId = dept.getParentId();
|
|
||||||
// 不在缓存ID列表的parentId是顶级节点ID,以此作为递归开始
|
|
||||||
if (!cacheDeptIds.contains(parentId)) {
|
|
||||||
list.addAll(recurDeptList(parentId, deptList));
|
|
||||||
cacheDeptIds.add(parentId); // 避免重复递归
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 列表为空说明所有的节点都是独立的
|
List<DeptVO> list = new ArrayList<>();
|
||||||
if (list.isEmpty()) {
|
for (Long rootId : rootIds) {
|
||||||
return deptList.stream().map(item -> {
|
list.addAll(recurDeptList(rootId, deptList));
|
||||||
DeptVO deptVO = new DeptVO();
|
|
||||||
BeanUtil.copyProperties(item, deptVO);
|
|
||||||
return deptVO;
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +77,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<DeptVO> recurDeptList(Long parentId, List<SysDept> deptList) {
|
public List<DeptVO> recurDeptList(Long parentId, List<SysDept> deptList) {
|
||||||
List<DeptVO> list = deptList.stream()
|
return deptList.stream()
|
||||||
.filter(dept -> dept.getParentId().equals(parentId))
|
.filter(dept -> dept.getParentId().equals(parentId))
|
||||||
.map(dept -> {
|
.map(dept -> {
|
||||||
DeptVO deptVO = deptConverter.entity2Vo(dept);
|
DeptVO deptVO = deptConverter.entity2Vo(dept);
|
||||||
@@ -100,10 +85,8 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
deptVO.setChildren(children);
|
deptVO.setChildren(children);
|
||||||
return deptVO;
|
return deptVO;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门下拉选项
|
* 部门下拉选项
|
||||||
*
|
*
|
||||||
@@ -111,7 +94,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Option> listDeptOptions() {
|
public List<Option> listDeptOptions() {
|
||||||
List<Option> options =new ArrayList<>();
|
|
||||||
|
|
||||||
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
|
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
|
||||||
.eq(SysDept::getStatus, StatusEnum.ENABLE.getValue())
|
.eq(SysDept::getStatus, StatusEnum.ENABLE.getValue())
|
||||||
@@ -119,19 +101,21 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
.orderByAsc(SysDept::getSort)
|
.orderByAsc(SysDept::getSort)
|
||||||
);
|
);
|
||||||
|
|
||||||
Set<Long> parentIds = deptList.stream().map(SysDept::getParentId)
|
Set<Long> parentIds = deptList.stream()
|
||||||
|
.map(SysDept::getParentId)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
Set<Long> childrenIds = deptList.stream().map(SysDept::getId)
|
Set<Long> deptIds = deptList.stream()
|
||||||
|
.map(SysDept::getId)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
for (Long parentId : parentIds) {
|
List<Long> rootIds = CollectionUtil.subtractToList(parentIds, deptIds);
|
||||||
if(!childrenIds.contains(parentId)){
|
|
||||||
options.addAll(recurDeptTreeOptions(parentId, deptList));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return options;
|
List<Option> list = new ArrayList<>();
|
||||||
|
for (Long rootId : rootIds) {
|
||||||
|
list.addAll(recurDeptTreeOptions(rootId, deptList));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -166,11 +150,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static List<Option> recurDeptTreeOptions(long parentId, List<SysDept> deptList) {
|
public static List<Option> recurDeptTreeOptions(long parentId, List<SysDept> deptList) {
|
||||||
if (CollectionUtil.isEmpty(deptList)) {
|
List<Option> list = CollectionUtil.emptyIfNull(deptList).stream()
|
||||||
return Collections.EMPTY_LIST;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Option> list = deptList.stream()
|
|
||||||
.filter(dept -> dept.getParentId().equals(parentId))
|
.filter(dept -> dept.getParentId().equals(parentId))
|
||||||
.map(dept -> {
|
.map(dept -> {
|
||||||
Option option = new Option(dept.getId(), dept.getName());
|
Option option = new Option(dept.getId(), dept.getName());
|
||||||
@@ -181,7 +161,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
return option;
|
return option;
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,21 +205,20 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
SysDept::getSort
|
SysDept::getSort
|
||||||
));
|
));
|
||||||
|
|
||||||
DeptForm deptForm = deptConverter.entity2Form(entity);
|
return deptConverter.entity2Form(entity);
|
||||||
return deptForm;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门路径生成
|
* 部门路径生成
|
||||||
*
|
*
|
||||||
* @param parentId
|
* @param parentId 父ID
|
||||||
* @return
|
* @return 父节点路径以英文逗号(, )分割,eg: 1,2,3
|
||||||
*/
|
*/
|
||||||
private String generateDeptTreePath(Long parentId) {
|
private String generateDeptTreePath(Long parentId) {
|
||||||
String treePath = null;
|
String treePath = null;
|
||||||
if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
|
if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
|
||||||
treePath = parentId + "";
|
treePath = String.valueOf(parentId);
|
||||||
} else {
|
} else {
|
||||||
SysDept parent = this.getById(parentId);
|
SysDept parent = this.getById(parentId);
|
||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
@@ -249,6 +227,4 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
}
|
}
|
||||||
return treePath;
|
return treePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.youlai.system.service.impl;
|
package com.youlai.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.util.NumberUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
@@ -9,22 +9,24 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||||||
import com.youlai.system.common.constant.SystemConstants;
|
import com.youlai.system.common.constant.SystemConstants;
|
||||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||||
import com.youlai.system.common.enums.StatusEnum;
|
import com.youlai.system.common.enums.StatusEnum;
|
||||||
import com.youlai.system.pojo.entity.SysDept;
|
|
||||||
import com.youlai.system.pojo.form.MenuForm;
|
|
||||||
import com.youlai.system.pojo.vo.Option;
|
|
||||||
import com.youlai.system.converter.MenuConverter;
|
import com.youlai.system.converter.MenuConverter;
|
||||||
import com.youlai.system.mapper.SysMenuMapper;
|
import com.youlai.system.mapper.SysMenuMapper;
|
||||||
import com.youlai.system.pojo.entity.SysMenu;
|
|
||||||
import com.youlai.system.pojo.bo.RouteBO;
|
import com.youlai.system.pojo.bo.RouteBO;
|
||||||
|
import com.youlai.system.pojo.entity.SysMenu;
|
||||||
|
import com.youlai.system.pojo.form.MenuForm;
|
||||||
import com.youlai.system.pojo.query.MenuQuery;
|
import com.youlai.system.pojo.query.MenuQuery;
|
||||||
import com.youlai.system.pojo.vo.MenuVO;
|
import com.youlai.system.pojo.vo.MenuVO;
|
||||||
|
import com.youlai.system.pojo.vo.Option;
|
||||||
import com.youlai.system.pojo.vo.RouteVO;
|
import com.youlai.system.pojo.vo.RouteVO;
|
||||||
import com.youlai.system.service.SysMenuService;
|
import com.youlai.system.service.SysMenuService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,6 +42,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜单列表
|
* 菜单列表
|
||||||
|
*
|
||||||
|
* @param queryParams {@link MenuQuery}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<MenuVO> listMenus(MenuQuery queryParams) {
|
public List<MenuVO> listMenus(MenuQuery queryParams) {
|
||||||
@@ -48,19 +52,20 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
.orderByAsc(SysMenu::getSort)
|
.orderByAsc(SysMenu::getSort)
|
||||||
);
|
);
|
||||||
|
|
||||||
Set<Long> cacheMenuIds = menus.stream()
|
Set<Long> parentIds = menus.stream()
|
||||||
.map(menu -> menu.getId())
|
.map(SysMenu::getParentId)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
List<MenuVO> list = menus.stream().map(menu -> {
|
Set<Long> menuIds = menus.stream()
|
||||||
Long parentId = menu.getParentId();
|
.map(SysMenu::getId)
|
||||||
// parentId不在当前菜单ID的列表,说明为顶级菜单ID,根据此ID作为递归的开始节点
|
.collect(Collectors.toSet());
|
||||||
if (!cacheMenuIds.contains(parentId)) {
|
|
||||||
cacheMenuIds.add(parentId);
|
List<Long> rootIds = CollectionUtil.subtractToList(parentIds, menuIds); // 求差集,得到 parentIds 中 menuIds 没有的元素
|
||||||
return recurMenus(parentId, menus);
|
|
||||||
|
List<MenuVO> list = new ArrayList<>();
|
||||||
|
for (Long rootId : rootIds) {
|
||||||
|
list.addAll(recurMenus(rootId, menus)); // 递归
|
||||||
}
|
}
|
||||||
return new LinkedList<MenuVO>();
|
|
||||||
}).collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,18 +78,20 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
|
|
||||||
MenuTypeEnum menuType = menuForm.getType(); // 菜单类型
|
MenuTypeEnum menuType = menuForm.getType(); // 菜单类型
|
||||||
switch (menuType) {
|
switch (menuType) {
|
||||||
case CATALOG: // 目录
|
case CATALOG -> { // 目录
|
||||||
Assert.isTrue(path.startsWith("/"), "目录路由路径格式错误,必须以/开始");
|
if (NumberUtil.equals(menuForm.getParentId(), 0) && !path.startsWith("/")) {
|
||||||
|
menuForm.setPath("/" + path); // 一级目录需以 / 开头
|
||||||
|
}
|
||||||
menuForm.setComponent("Layout");
|
menuForm.setComponent("Layout");
|
||||||
break;
|
}
|
||||||
case EXTLINK: // 外链
|
case EXTLINK -> // 外链
|
||||||
menuForm.setComponent(null);
|
menuForm.setComponent(null);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
SysMenu entity = menuConverter.form2Entity(menuForm);
|
SysMenu entity = menuConverter.form2Entity(menuForm);
|
||||||
|
|
||||||
boolean result = this.saveOrUpdate(entity);
|
String treePath = generateMenuTreePath(menuForm.getParentId());
|
||||||
return result;
|
entity.setTreePath(treePath);
|
||||||
|
return this.saveOrUpdate(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,8 +100,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
@Override
|
@Override
|
||||||
public List<Option> listMenuOptions() {
|
public List<Option> listMenuOptions() {
|
||||||
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getSort));
|
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getSort));
|
||||||
List<Option> menus = recurMenuOptions(SystemConstants.ROOT_NODE_ID, menuList);
|
return recurMenuOptions(SystemConstants.ROOT_NODE_ID, menuList);
|
||||||
return menus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,8 +110,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
@Cacheable(cacheNames = "system", key = "'routes'")
|
@Cacheable(cacheNames = "system", key = "'routes'")
|
||||||
public List<RouteVO> listRoutes() {
|
public List<RouteVO> listRoutes() {
|
||||||
List<RouteBO> menuList = this.baseMapper.listRoutes();
|
List<RouteBO> menuList = this.baseMapper.listRoutes();
|
||||||
List<RouteVO> routeList = recurRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
return recurRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||||
return routeList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,14 +121,14 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private List<RouteVO> recurRoutes(Long parentId, List<RouteBO> menuList) {
|
private List<RouteVO> recurRoutes(Long parentId, List<RouteBO> menuList) {
|
||||||
List<RouteVO> list = new ArrayList<>();
|
return CollectionUtil.emptyIfNull(menuList).stream()
|
||||||
Optional.ofNullable(menuList).ifPresent(menus -> menus.stream()
|
|
||||||
.filter(menu -> menu.getParentId().equals(parentId))
|
.filter(menu -> menu.getParentId().equals(parentId))
|
||||||
.forEach(menu -> {
|
.map(menu -> {
|
||||||
RouteVO routeVO = new RouteVO();
|
RouteVO routeVO = new RouteVO();
|
||||||
MenuTypeEnum menuTypeEnum = menu.getType();
|
MenuTypeEnum menuTypeEnum = menu.getType();
|
||||||
if (MenuTypeEnum.MENU.equals(menuTypeEnum)) {
|
if (MenuTypeEnum.MENU.equals(menuTypeEnum)) {
|
||||||
routeVO.setName(menu.getPath()); // 根据name路由跳转 this.$router.push({name:xxx})
|
String routeName = StringUtils.capitalize(StrUtil.toCamelCase(menu.getPath(), '-')); // 路由 name 需要驼峰,首字母大写
|
||||||
|
routeVO.setName(routeName); // 根据name路由跳转 this.$router.push({name:xxx})
|
||||||
}
|
}
|
||||||
routeVO.setPath(menu.getPath()); // 根据path路由跳转 this.$router.push({path:xxx})
|
routeVO.setPath(menu.getPath()); // 根据path路由跳转 this.$router.push({path:xxx})
|
||||||
routeVO.setRedirect(menu.getRedirectUrl());
|
routeVO.setRedirect(menu.getRedirectUrl());
|
||||||
@@ -139,9 +144,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
|
|
||||||
List<RouteVO> children = recurRoutes(menu.getId(), menuList);
|
List<RouteVO> children = recurRoutes(menu.getId(), menuList);
|
||||||
routeVO.setChildren(children);
|
routeVO.setChildren(children);
|
||||||
list.add(routeVO);
|
return routeVO;
|
||||||
}));
|
}).toList();
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -154,11 +158,10 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean updateMenuVisible(Long menuId, Integer visible) {
|
public boolean updateMenuVisible(Long menuId, Integer visible) {
|
||||||
boolean result = this.update(new LambdaUpdateWrapper<SysMenu>()
|
return this.update(new LambdaUpdateWrapper<SysMenu>()
|
||||||
.eq(SysMenu::getId, menuId)
|
.eq(SysMenu::getId, menuId)
|
||||||
.set(SysMenu::getVisible, visible)
|
.set(SysMenu::getVisible, visible)
|
||||||
);
|
);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -211,19 +214,15 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private List<MenuVO> recurMenus(Long parentId, List<SysMenu> menuList) {
|
private List<MenuVO> recurMenus(Long parentId, List<SysMenu> menuList) {
|
||||||
if (CollectionUtil.isEmpty(menuList)) {
|
return CollectionUtil.emptyIfNull(menuList)
|
||||||
return Collections.EMPTY_LIST;
|
.stream()
|
||||||
}
|
|
||||||
|
|
||||||
List<MenuVO> menus = menuList.stream()
|
|
||||||
.filter(menu -> menu.getParentId().equals(parentId))
|
.filter(menu -> menu.getParentId().equals(parentId))
|
||||||
.map(entity -> {
|
.map(entity -> {
|
||||||
MenuVO menuVO = menuConverter.entity2Vo(entity);
|
MenuVO menuVO = menuConverter.entity2Vo(entity);
|
||||||
List<MenuVO> children = recurMenus(entity.getId(), menuList);
|
List<MenuVO> children = recurMenus(entity.getId(), menuList);
|
||||||
menuVO.setChildren(children);
|
menuVO.setChildren(children);
|
||||||
return menuVO;
|
return menuVO;
|
||||||
}).collect(Collectors.toList());
|
}).toList();
|
||||||
return menus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -234,11 +233,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static List<Option> recurMenuOptions(Long parentId, List<SysMenu> menuList) {
|
private static List<Option> recurMenuOptions(Long parentId, List<SysMenu> menuList) {
|
||||||
if (CollectionUtil.isEmpty(menuList)) {
|
List<Option> menus = CollectionUtil.emptyIfNull(menuList).stream()
|
||||||
return Collections.EMPTY_LIST;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Option> menus = menuList.stream()
|
|
||||||
.filter(menu -> menu.getParentId().equals(parentId))
|
.filter(menu -> menu.getParentId().equals(parentId))
|
||||||
.map(menu -> new Option(menu.getId(), menu.getName(), recurMenuOptions(menu.getId(), menuList)))
|
.map(menu -> new Option(menu.getId(), menu.getName(), recurMenuOptions(menu.getId(), menuList)))
|
||||||
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||||
@@ -246,4 +241,24 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 部门路径生成
|
||||||
|
*
|
||||||
|
* @param parentId 父ID
|
||||||
|
* @return 父节点路径以英文逗号(, )分割,eg: 1,2,3
|
||||||
|
*/
|
||||||
|
private String generateMenuTreePath(Long parentId) {
|
||||||
|
String treePath = null;
|
||||||
|
if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
|
||||||
|
treePath = String.valueOf(parentId);
|
||||||
|
} else {
|
||||||
|
SysMenu parent = this.getById(parentId);
|
||||||
|
if (parent != null) {
|
||||||
|
treePath = parent.getTreePath() + "," + parent.getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return treePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user