|
@@ -10,6 +10,7 @@ import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuLi
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuRespVO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper;
|
|
|
import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants;
|
|
@@ -31,6 +32,8 @@ 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;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
|
|
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
|
import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
|
|
@@ -52,6 +55,8 @@ public class MenuServiceImpl implements MenuService {
|
|
|
private TenantService tenantService;
|
|
|
@Resource
|
|
|
private AdminUserService userService;
|
|
|
+ @Resource
|
|
|
+ private RoleService roleService;
|
|
|
|
|
|
@Override
|
|
|
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#createReqVO.permission",
|
|
@@ -175,22 +180,33 @@ public class MenuServiceImpl implements MenuService {
|
|
|
|
|
|
@Override
|
|
|
public List<MenuRespVO> getPrimarySecondaryMenus() {
|
|
|
- // 获取所有一级菜单
|
|
|
- List<MenuDO> primaryMenus = getMenuList(new MenuListReqVO().setParentId(0L));
|
|
|
- List<MenuDO> secondaryMenus = new ArrayList<>();
|
|
|
- // 将一级菜单转换为响应对象,并为其添加二级菜单(如果有的话)
|
|
|
- List<MenuRespVO> respMenus = primaryMenus.stream()
|
|
|
- .map(primaryMenu -> {
|
|
|
- MenuRespVO respMenu = BeanUtils.toBean(primaryMenu, MenuRespVO.class);
|
|
|
- // 获取当前一级菜单下的所有二级菜单
|
|
|
- List<MenuDO> childMenus = getMenuList(new MenuListReqVO().setParentId(primaryMenu.getId()));
|
|
|
- respMenu.setChildren(childMenus);
|
|
|
-
|
|
|
- // 这里我们假设直接返回了简化版的一级菜单响应对象
|
|
|
- return respMenu;
|
|
|
- })
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
+// // 获取所有一级菜单
|
|
|
+// List<MenuDO> primaryMenus = getMenuList(new MenuListReqVO().setParentId(0L));
|
|
|
+// List<MenuDO> secondaryMenus = new ArrayList<>();
|
|
|
+// // 将一级菜单转换为响应对象,并为其添加二级菜单(如果有的话)
|
|
|
+// List<MenuRespVO> respMenus = primaryMenus.stream()
|
|
|
+// .map(primaryMenu -> {
|
|
|
+// MenuRespVO respMenu = BeanUtils.toBean(primaryMenu, MenuRespVO.class);
|
|
|
+// // 获取当前一级菜单下的所有二级菜单
|
|
|
+// List<MenuDO> childMenus = getMenuList(new MenuListReqVO().setParentId(primaryMenu.getId()));
|
|
|
+// respMenu.setChildren(childMenus);
|
|
|
+//
|
|
|
+// // 这里我们假设直接返回了简化版的一级菜单响应对象
|
|
|
+// return respMenu;
|
|
|
+// })
|
|
|
+// .collect(Collectors.toList());
|
|
|
+ Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(getLoginUserId());
|
|
|
+ if (CollUtil.isEmpty(roleIds)) {
|
|
|
+ throw exception(ROLE_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ // 获得角色列表
|
|
|
+ List<RoleDO> roles = roleService.getRoleList(roleIds);
|
|
|
+ roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色
|
|
|
+ // 获得菜单列表
|
|
|
+ Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId));
|
|
|
+ List<MenuDO> menuList = this.getMenuList(menuIds);
|
|
|
+ menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单
|
|
|
+ List<MenuRespVO> respMenus = this.buildMenuTree(menuList);
|
|
|
return respMenus;
|
|
|
}
|
|
|
|
|
@@ -307,4 +323,44 @@ public class MenuServiceImpl implements MenuService {
|
|
|
return menuIds;
|
|
|
}
|
|
|
|
|
|
+ private List<MenuRespVO> buildMenuTree(List<MenuDO> menuDOList) {
|
|
|
+ List<MenuRespVO> menuList = BeanUtils.toBean(menuDOList, MenuRespVO.class);
|
|
|
+ List<MenuRespVO> pList = new ArrayList<>(); // 用于存放parentId为0的菜单项
|
|
|
+ Map<Long, MenuRespVO> idMap = new HashMap<>(); // 用于快速查找
|
|
|
+
|
|
|
+ // 第一步:构建idMap并筛选出parentId为0的菜单项
|
|
|
+ for (MenuRespVO menu : menuList) {
|
|
|
+ idMap.put(menu.getId(), menu);
|
|
|
+ if (menu.getParentId() == 0) {
|
|
|
+ pList.add(menu);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第二步:为pList中的每个元素设置children
|
|
|
+ for (MenuRespVO parent : pList) {
|
|
|
+ List<MenuRespVO> children = new ArrayList<>();
|
|
|
+ for (MenuRespVO menu : menuList) {
|
|
|
+ if (menu.getParentId() == parent.getId()) {
|
|
|
+ children.add(menu);
|
|
|
+ // 递归处理子菜单的子菜单(如果需要的话)
|
|
|
+// buildChildren(menu, idMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ parent.setChildren(children);
|
|
|
+ }
|
|
|
+ return pList;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有需要递归处理子菜单的子菜单,可以添加这样的方法
|
|
|
+ private void buildChildren(MenuRespVO menu, Map<Long, MenuRespVO> idMap) {
|
|
|
+ List<MenuRespVO> children = new ArrayList<>();
|
|
|
+ for (MenuRespVO subMenu : idMap.values()) {
|
|
|
+ if (subMenu.getParentId() == menu.getId()) {
|
|
|
+ children.add(subMenu);
|
|
|
+ buildChildren(subMenu, idMap); // 递归
|
|
|
+ }
|
|
|
+ }
|
|
|
+ menu.setChildren(children);
|
|
|
+ }
|
|
|
+
|
|
|
}
|