♻️ Refactoring code. 顶部菜单优化
This commit is contained in:
1 parent
a67550857a
commit
c0faa3d075
5 files changed
+135
-28
No files matched your search
-5
@@ -53,11 +53,6 @@ public interface CommonConstants {
|
||||
*/
|
||||
String STATUS_LOCK = "9";
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
*/
|
||||
String MENU = "0";
|
||||
|
||||
/**
|
||||
* 菜单树根节点
|
||||
*/
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.pig4cloud.pigx.common.core.constant.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020-02-17
|
||||
* <p>
|
||||
* 菜单类型
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum MenuTypeEnum {
|
||||
|
||||
/**
|
||||
* 左侧菜单
|
||||
*/
|
||||
LEFT_MENU("0", "left"),
|
||||
|
||||
/**
|
||||
* 顶部菜单
|
||||
*/
|
||||
TOP_MENU("2", "top"),
|
||||
|
||||
/**
|
||||
* 按钮
|
||||
*/
|
||||
BUTTON("1", "button");
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private String type;
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
}
|
||||
+8
-19
@@ -19,13 +19,9 @@
|
||||
|
||||
package com.pig4cloud.pigx.admin.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
|
||||
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
|
||||
import com.pig4cloud.pigx.admin.api.vo.TreeUtil;
|
||||
import com.pig4cloud.pigx.admin.service.SysMenuService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.log.annotation.SysLog;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
@@ -35,9 +31,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -55,35 +49,30 @@ public class SysMenuController {
|
||||
/**
|
||||
* 返回当前用户的树形菜单集合
|
||||
*
|
||||
* @param type 类型
|
||||
* @param parentId 父节点ID
|
||||
* @return 当前用户的树形菜单
|
||||
*/
|
||||
@GetMapping
|
||||
public R getUserMenu(Integer parentId) {
|
||||
public R getUserMenu(String type, Integer parentId) {
|
||||
|
||||
// 获取符合条件的菜单
|
||||
Set<MenuVO> all = new HashSet<>();
|
||||
SecurityUtils.getRoles()
|
||||
.forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId)));
|
||||
List<MenuTree> menuTreeList = all.stream()
|
||||
.filter(menuVo -> CommonConstants.MENU.equals(menuVo.getType()))
|
||||
.map(MenuTree::new)
|
||||
.sorted(Comparator.comparingInt(MenuTree::getSort))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return R.ok(TreeUtil.build(menuTreeList, parentId == null
|
||||
? CommonConstants.MENU_TREE_ROOT_ID : parentId));
|
||||
return R.ok(sysMenuService.filterMenu(all,type,parentId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回树形菜单集合
|
||||
*
|
||||
* @param lazy 是否是懒加载
|
||||
* @param parentId 父节点ID
|
||||
* @return 树形菜单
|
||||
*/
|
||||
@GetMapping(value = "/tree")
|
||||
public R getTree() {
|
||||
return R.ok(TreeUtil.buildTree(sysMenuService
|
||||
.list(Wrappers.<SysMenu>lambdaQuery()
|
||||
.orderByAsc(SysMenu::getSort)), CommonConstants.MENU_TREE_ROOT_ID));
|
||||
public R getTree(boolean lazy, Integer parentId) {
|
||||
return R.ok(sysMenuService.treeMenu(lazy, parentId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+20
@@ -21,11 +21,13 @@ package com.pig4cloud.pigx.admin.service;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
|
||||
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -59,4 +61,22 @@ public interface SysMenuService extends IService<SysMenu> {
|
||||
* @return 成功、失败
|
||||
*/
|
||||
Boolean updateMenuById(SysMenu sysMenu);
|
||||
|
||||
/**
|
||||
* 构建树
|
||||
*
|
||||
* @param lazy 是否是懒加载
|
||||
* @param parentId 父节点ID
|
||||
* @return
|
||||
*/
|
||||
List<MenuTree> treeMenu(boolean lazy, Integer parentId);
|
||||
|
||||
/**
|
||||
* 查询菜单
|
||||
*
|
||||
* @param menuVOSet
|
||||
* @param parentId
|
||||
* @return
|
||||
*/
|
||||
List<MenuTree> filterMenu(Set<MenuVO> menuVOSet, String type, Integer parentId);
|
||||
}
|
||||
+68
-4
@@ -22,13 +22,17 @@ package com.pig4cloud.pigx.admin.service.impl;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysRoleMenu;
|
||||
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
|
||||
import com.pig4cloud.pigx.admin.api.vo.TreeUtil;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysMenuMapper;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysRoleMenuMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysMenuService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.enums.MenuTypeEnum;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
@@ -36,7 +40,11 @@ import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -68,10 +76,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
return R.failed("菜单含有下级不能删除");
|
||||
}
|
||||
|
||||
sysRoleMenuMapper
|
||||
.delete(Wrappers.<SysRoleMenu>query()
|
||||
.lambda().eq(SysRoleMenu::getMenuId, id));
|
||||
|
||||
sysRoleMenuMapper.delete(Wrappers.<SysRoleMenu>query()
|
||||
.lambda().eq(SysRoleMenu::getMenuId, id));
|
||||
//删除当前菜单及其子菜单
|
||||
return R.ok(this.removeById(id));
|
||||
}
|
||||
@@ -81,4 +87,62 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
public Boolean updateMenuById(SysMenu sysMenu) {
|
||||
return this.updateById(sysMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建树查询
|
||||
* 1. 不是懒加载情况,查询全部
|
||||
* 2. 是懒加载,根据parentId 查询
|
||||
* 2.1 父节点为空,则查询ID -1
|
||||
*
|
||||
* @param lazy 是否是懒加载
|
||||
* @param parentId 父节点ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<MenuTree> treeMenu(boolean lazy, Integer parentId) {
|
||||
if (!lazy) {
|
||||
return TreeUtil.buildTree(baseMapper.selectList(Wrappers.<SysMenu>lambdaQuery()
|
||||
.orderByAsc(SysMenu::getSort)), CommonConstants.MENU_TREE_ROOT_ID);
|
||||
}
|
||||
|
||||
Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
|
||||
return TreeUtil.buildTree(baseMapper
|
||||
.selectList(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, parent)
|
||||
.orderByAsc(SysMenu::getSort)), parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询菜单
|
||||
*
|
||||
* @param all 全部菜单
|
||||
* @param type 类型
|
||||
* @param parentId 父节点ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<MenuTree> filterMenu(Set<MenuVO> all, String type, Integer parentId) {
|
||||
List<MenuTree> menuTreeList = all.stream()
|
||||
.filter(menuTypePredicate(type))
|
||||
.map(MenuTree::new)
|
||||
.sorted(Comparator.comparingInt(MenuTree::getSort))
|
||||
.collect(Collectors.toList());
|
||||
Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
|
||||
return TreeUtil.build(menuTreeList, parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* menu 类型断言
|
||||
*
|
||||
* @param type 类型
|
||||
* @return Predicate
|
||||
*/
|
||||
private Predicate<MenuVO> menuTypePredicate(String type) {
|
||||
return vo -> {
|
||||
if (MenuTypeEnum.TOP_MENU.getDescription().equals(type)) {
|
||||
return MenuTypeEnum.TOP_MENU.getType().equals(vo.getType());
|
||||
}
|
||||
// 其他查询 左侧 + 顶部
|
||||
return !MenuTypeEnum.BUTTON.getType().equals(vo.getType());
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user