mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 02:39:19 +08:00
完善动态路由菜单接口
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.position.BatchAssignPositionMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.PositionMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.PositionCompanyMenuService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 岗位权限管理控制器
|
||||
*/
|
||||
@Tag(name = "组织-岗位权限管理", description = "岗位菜单权限的分配和管理接口")
|
||||
@RestController
|
||||
@RequestMapping("/position-permission")
|
||||
public class PositionPermissionController {
|
||||
|
||||
private final PositionCompanyMenuService positionCompanyMenuService;
|
||||
|
||||
public PositionPermissionController(PositionCompanyMenuService positionCompanyMenuService) {
|
||||
this.positionCompanyMenuService = positionCompanyMenuService;
|
||||
}
|
||||
|
||||
@Operation(summary = "批量分配岗位菜单权限", description = "为指定岗位批量分配菜单权限")
|
||||
@PostMapping("/assign")
|
||||
public ApiResponse<Void> batchAssignPositionMenus(@Valid @RequestBody BatchAssignPositionMenuDTO batchAssignDTO) {
|
||||
positionCompanyMenuService.batchAssignPositionMenus(batchAssignDTO);
|
||||
return ApiResponse.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "移除岗位菜单权限", description = "移除指定岗位的某个菜单权限")
|
||||
@DeleteMapping("/remove/{positionId}/{companyMenuId}")
|
||||
public ApiResponse<Void> removePositionMenu(
|
||||
@Parameter(description = "岗位ID") @PathVariable Long positionId,
|
||||
@Parameter(description = "公司菜单权限ID") @PathVariable Long companyMenuId) {
|
||||
positionCompanyMenuService.removePositionMenu(positionId, companyMenuId);
|
||||
return ApiResponse.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "查询岗位菜单权限列表", description = "根据岗位ID查询该岗位拥有的所有菜单权限")
|
||||
@GetMapping("/menus/{positionId}")
|
||||
public ApiResponse<List<PositionMenuVO>> getMenusByPositionId(
|
||||
@Parameter(description = "岗位ID") @PathVariable Long positionId) {
|
||||
List<PositionMenuVO> menus = positionCompanyMenuService.getMenusByPositionId(positionId);
|
||||
return ApiResponse.success(menus);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量查询岗位菜单权限", description = "根据岗位ID列表批量查询菜单权限")
|
||||
@PostMapping("/menus/batch")
|
||||
public ApiResponse<List<PositionMenuVO>> getMenusByPositionIds(@RequestBody List<Long> positionIds) {
|
||||
List<PositionMenuVO> menus = positionCompanyMenuService.getMenusByPositionIds(positionIds);
|
||||
return ApiResponse.success(menus);
|
||||
}
|
||||
|
||||
@Operation(summary = "检查岗位菜单权限", description = "检查指定岗位是否拥有某个菜单权限")
|
||||
@GetMapping("/check/{positionId}/{companyMenuId}")
|
||||
public ApiResponse<Boolean> hasPositionMenu(
|
||||
@Parameter(description = "岗位ID") @PathVariable Long positionId,
|
||||
@Parameter(description = "公司菜单权限ID") @PathVariable Long companyMenuId) {
|
||||
boolean hasPermission = positionCompanyMenuService.hasPositionMenu(positionId, companyMenuId);
|
||||
return ApiResponse.success(hasPermission);
|
||||
}
|
||||
}
|
||||
-20
@@ -2,31 +2,11 @@ package top.xyquan.ecomdmbbackend.modules.org.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.po.PositionCompanyMenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.PositionMenuVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 岗位公司菜单权限Mapper接口
|
||||
*/
|
||||
@Mapper
|
||||
public interface PositionCompanyMenuMapper extends BaseMapper<PositionCompanyMenuPO> {
|
||||
|
||||
/**
|
||||
* 根据岗位ID查询菜单权限列表
|
||||
*
|
||||
* @param positionId 岗位ID
|
||||
* @return 菜单权限列表
|
||||
*/
|
||||
List<PositionMenuVO> selectMenusByPositionId(@Param("positionId") Long positionId);
|
||||
|
||||
/**
|
||||
* 根据岗位ID列表查询菜单权限列表
|
||||
*
|
||||
* @param positionIds 岗位ID列表
|
||||
* @return 菜单权限列表
|
||||
*/
|
||||
List<PositionMenuVO> selectMenusByPositionIds(@Param("positionIds") List<Long> positionIds);
|
||||
}
|
||||
|
||||
+17
@@ -5,6 +5,7 @@ import top.xyquan.ecomdmbbackend.modules.org.model.dto.company.menu.CreateCompan
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.company.menu.UpdateCompanyMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.po.CompanyMenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.CompanyMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -52,4 +53,20 @@ public interface CompanyMenuService extends IService<CompanyMenuPO> {
|
||||
* @param menuIds 菜单ID列表
|
||||
*/
|
||||
void batchDeleteCompanyMenu(Long companyId, List<Long> menuIds);
|
||||
|
||||
/**
|
||||
* 获取公司所有菜单(以MenuVO格式返回,用于动态路由)
|
||||
*
|
||||
* @param companyId 公司ID
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<MenuVO> getCompanyMenusForRoute(Long companyId);
|
||||
|
||||
/**
|
||||
* 根据职位获取菜单(以MenuVO格式返回,用于动态路由)
|
||||
*
|
||||
* @param positionId 职位ID
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<MenuVO> getPositionMenusForRoute(Long positionId);
|
||||
}
|
||||
|
||||
+142
-1
@@ -11,13 +11,18 @@ import top.xyquan.ecomdmbbackend.modules.org.model.dto.company.menu.CreateCompan
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.company.menu.UpdateCompanyMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.po.CompanyMenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.CompanyMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.PositionMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.CompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.PositionCompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.po.MenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.MenuService;
|
||||
import top.xyquan.ecomdmbbackend.utils.SecurityUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 公司菜单服务实现类
|
||||
@@ -27,9 +32,11 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
implements CompanyMenuService {
|
||||
|
||||
private final MenuService menuService;
|
||||
private final PositionCompanyMenuService positionCompanyMenuService;
|
||||
|
||||
public CompanyMenuServiceImpl(MenuService menuService) {
|
||||
public CompanyMenuServiceImpl(MenuService menuService, PositionCompanyMenuService positionCompanyMenuService) {
|
||||
this.menuService = menuService;
|
||||
this.positionCompanyMenuService = positionCompanyMenuService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -172,6 +179,140 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
this.remove(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuVO> getCompanyMenusForRoute(Long companyId) {
|
||||
// 查询公司所有菜单
|
||||
LambdaQueryWrapper<CompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CompanyMenuPO::getCompanyId, companyId)
|
||||
.eq(CompanyMenuPO::getStatus, 1) // 只查询启用的菜单
|
||||
.orderByAsc(CompanyMenuPO::getCustomOrder);
|
||||
|
||||
List<CompanyMenuPO> companyMenus = this.list(wrapper);
|
||||
|
||||
if (companyMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取菜单ID列表
|
||||
List<Long> menuIds = companyMenus.stream()
|
||||
.map(CompanyMenuPO::getMenuId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询系统菜单信息
|
||||
LambdaQueryWrapper<MenuPO> menuWrapper = new LambdaQueryWrapper<>();
|
||||
menuWrapper.in(MenuPO::getId, menuIds)
|
||||
.eq(MenuPO::getStatus, 1) // 只查询启用的菜单
|
||||
.orderByAsc(MenuPO::getOrder);
|
||||
|
||||
List<MenuPO> menus = menuService.list(menuWrapper);
|
||||
|
||||
// 构建公司菜单ID到公司菜单的映射
|
||||
Map<Long, CompanyMenuPO> companyMenuMap = companyMenus.stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getMenuId, cm -> cm));
|
||||
|
||||
// 转换为MenuVO,使用公司自定义字段
|
||||
return menus.stream()
|
||||
.map(menu -> {
|
||||
MenuVO menuVO = new MenuVO();
|
||||
BeanUtils.copyProperties(menu, menuVO);
|
||||
|
||||
// 使用公司自定义字段覆盖系统菜单字段
|
||||
CompanyMenuPO companyMenu = companyMenuMap.get(menu.getId());
|
||||
if (companyMenu != null) {
|
||||
if (companyMenu.getCustomName() != null) {
|
||||
menuVO.setName(companyMenu.getCustomName());
|
||||
}
|
||||
if (companyMenu.getCustomTitle() != null) {
|
||||
menuVO.setTitle(companyMenu.getCustomTitle());
|
||||
}
|
||||
if (companyMenu.getCustomIcon() != null) {
|
||||
menuVO.setIcon(companyMenu.getCustomIcon());
|
||||
}
|
||||
if (companyMenu.getCustomOrder() != null) {
|
||||
menuVO.setOrder(companyMenu.getCustomOrder());
|
||||
}
|
||||
}
|
||||
|
||||
return menuVO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuVO> getPositionMenusForRoute(Long positionId) {
|
||||
// 使用PositionCompanyMenuService的现有方法获取职位菜单权限
|
||||
List<PositionMenuVO> positionMenus = positionCompanyMenuService.getMenusByPositionId(positionId);
|
||||
|
||||
if (positionMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取公司菜单ID列表
|
||||
List<Long> companyMenuIds = positionMenus.stream()
|
||||
.map(PositionMenuVO::getCompanyMenuId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询公司菜单信息
|
||||
LambdaQueryWrapper<CompanyMenuPO> companyMenuWrapper = new LambdaQueryWrapper<>();
|
||||
companyMenuWrapper.in(CompanyMenuPO::getId, companyMenuIds)
|
||||
.eq(CompanyMenuPO::getStatus, 1) // 只查询启用的菜单
|
||||
.orderByAsc(CompanyMenuPO::getCustomOrder);
|
||||
|
||||
List<CompanyMenuPO> companyMenus = this.list(companyMenuWrapper);
|
||||
|
||||
if (companyMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取菜单ID列表
|
||||
List<Long> menuIds = companyMenus.stream()
|
||||
.map(CompanyMenuPO::getMenuId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询系统菜单信息
|
||||
LambdaQueryWrapper<MenuPO> menuWrapper = new LambdaQueryWrapper<>();
|
||||
menuWrapper.in(MenuPO::getId, menuIds)
|
||||
.eq(MenuPO::getStatus, 1) // 只查询启用的菜单
|
||||
.orderByAsc(MenuPO::getOrder);
|
||||
|
||||
List<MenuPO> menus = menuService.list(menuWrapper);
|
||||
|
||||
// 构建公司菜单ID到公司菜单的映射
|
||||
Map<Long, CompanyMenuPO> companyMenuMap = companyMenus.stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getId, cm -> cm));
|
||||
|
||||
// 转换为MenuVO,使用公司自定义字段
|
||||
return menus.stream()
|
||||
.map(menu -> {
|
||||
MenuVO menuVO = new MenuVO();
|
||||
BeanUtils.copyProperties(menu, menuVO);
|
||||
|
||||
// 查找对应的公司菜单
|
||||
CompanyMenuPO companyMenu = companyMenuMap.values().stream()
|
||||
.filter(cm -> cm.getMenuId().equals(menu.getId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (companyMenu != null) {
|
||||
if (companyMenu.getCustomName() != null) {
|
||||
menuVO.setName(companyMenu.getCustomName());
|
||||
}
|
||||
if (companyMenu.getCustomTitle() != null) {
|
||||
menuVO.setTitle(companyMenu.getCustomTitle());
|
||||
}
|
||||
if (companyMenu.getCustomIcon() != null) {
|
||||
menuVO.setIcon(companyMenu.getCustomIcon());
|
||||
}
|
||||
if (companyMenu.getCustomOrder() != null) {
|
||||
menuVO.setOrder(companyMenu.getCustomOrder());
|
||||
}
|
||||
}
|
||||
|
||||
return menuVO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为VO
|
||||
*/
|
||||
|
||||
+122
-12
@@ -1,32 +1,43 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.mapper.PositionCompanyMenuMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.position.BatchAssignPositionMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.po.CompanyMenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.po.PositionCompanyMenuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.po.PositionPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.PositionMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.CompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.PositionCompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.PositionService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 岗位公司菜单权限服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class PositionCompanyMenuServiceImpl implements PositionCompanyMenuService {
|
||||
public class PositionCompanyMenuServiceImpl extends ServiceImpl<PositionCompanyMenuMapper, PositionCompanyMenuPO>
|
||||
implements PositionCompanyMenuService {
|
||||
|
||||
private final PositionCompanyMenuMapper positionCompanyMenuMapper;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final PositionService positionService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
|
||||
public PositionCompanyMenuServiceImpl(PositionCompanyMenuMapper positionCompanyMenuMapper,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.positionCompanyMenuMapper = positionCompanyMenuMapper;
|
||||
public PositionCompanyMenuServiceImpl(CurrentUserContext currentUserContext,
|
||||
PositionService positionService,
|
||||
CompanyMenuService companyMenuService) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.positionService = positionService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,7 +49,7 @@ public class PositionCompanyMenuServiceImpl implements PositionCompanyMenuServic
|
||||
// 先删除该岗位的所有菜单权限
|
||||
LambdaQueryWrapper<PositionCompanyMenuPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(PositionCompanyMenuPO::getPositionId, positionId);
|
||||
positionCompanyMenuMapper.delete(deleteWrapper);
|
||||
this.remove(deleteWrapper);
|
||||
|
||||
// 批量插入新的菜单权限
|
||||
List<PositionCompanyMenuPO> positionMenus = new ArrayList<>();
|
||||
@@ -51,7 +62,7 @@ public class PositionCompanyMenuServiceImpl implements PositionCompanyMenuServic
|
||||
}
|
||||
|
||||
if (!positionMenus.isEmpty()) {
|
||||
positionMenus.forEach(positionCompanyMenuMapper::insert);
|
||||
this.saveBatch(positionMenus);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,15 +72,62 @@ public class PositionCompanyMenuServiceImpl implements PositionCompanyMenuServic
|
||||
wrapper.eq(PositionCompanyMenuPO::getPositionId, positionId)
|
||||
.eq(PositionCompanyMenuPO::getCompanyMenuId, companyMenuId);
|
||||
|
||||
int result = positionCompanyMenuMapper.delete(wrapper);
|
||||
if (result == 0) {
|
||||
boolean result = this.remove(wrapper);
|
||||
if (!result) {
|
||||
throw new BusinessException("岗位菜单权限不存在");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PositionMenuVO> getMenusByPositionId(Long positionId) {
|
||||
return positionCompanyMenuMapper.selectMenusByPositionId(positionId);
|
||||
// 查询岗位菜单权限关联记录
|
||||
LambdaQueryWrapper<PositionCompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(PositionCompanyMenuPO::getPositionId, positionId);
|
||||
List<PositionCompanyMenuPO> positionMenus = this.list(wrapper);
|
||||
|
||||
if (positionMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取岗位信息
|
||||
PositionPO position = positionService.getById(positionId);
|
||||
if (position == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取公司菜单ID列表
|
||||
List<Long> companyMenuIds = positionMenus.stream()
|
||||
.map(PositionCompanyMenuPO::getCompanyMenuId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询公司菜单信息
|
||||
LambdaQueryWrapper<CompanyMenuPO> companyMenuWrapper = new LambdaQueryWrapper<>();
|
||||
companyMenuWrapper.in(CompanyMenuPO::getId, companyMenuIds);
|
||||
List<CompanyMenuPO> companyMenus = companyMenuService.list(companyMenuWrapper);
|
||||
|
||||
// 构建映射关系
|
||||
Map<Long, CompanyMenuPO> companyMenuMap = companyMenus.stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getId, cm -> cm));
|
||||
|
||||
// 转换为VO
|
||||
return positionMenus.stream()
|
||||
.map(pm -> {
|
||||
PositionMenuVO vo = new PositionMenuVO();
|
||||
vo.setId(pm.getId());
|
||||
vo.setPositionId(pm.getPositionId());
|
||||
vo.setPositionName(position.getName());
|
||||
vo.setCompanyMenuId(pm.getCompanyMenuId());
|
||||
vo.setCreateTime(pm.getCreateTime());
|
||||
|
||||
CompanyMenuPO companyMenu = companyMenuMap.get(pm.getCompanyMenuId());
|
||||
if (companyMenu != null) {
|
||||
vo.setMenuName(companyMenu.getCustomName());
|
||||
vo.setMenuTitle(companyMenu.getCustomTitle());
|
||||
}
|
||||
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,7 +135,59 @@ public class PositionCompanyMenuServiceImpl implements PositionCompanyMenuServic
|
||||
if (positionIds == null || positionIds.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return positionCompanyMenuMapper.selectMenusByPositionIds(positionIds);
|
||||
|
||||
// 查询岗位菜单权限关联记录
|
||||
LambdaQueryWrapper<PositionCompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.in(PositionCompanyMenuPO::getPositionId, positionIds);
|
||||
List<PositionCompanyMenuPO> positionMenus = this.list(wrapper);
|
||||
|
||||
if (positionMenus.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取岗位信息
|
||||
List<PositionPO> positions = positionService.listByIds(positionIds);
|
||||
Map<Long, PositionPO> positionMap = positions.stream()
|
||||
.collect(Collectors.toMap(PositionPO::getId, p -> p));
|
||||
|
||||
// 获取公司菜单ID列表
|
||||
List<Long> companyMenuIds = positionMenus.stream()
|
||||
.map(PositionCompanyMenuPO::getCompanyMenuId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询公司菜单信息
|
||||
LambdaQueryWrapper<CompanyMenuPO> companyMenuWrapper = new LambdaQueryWrapper<>();
|
||||
companyMenuWrapper.in(CompanyMenuPO::getId, companyMenuIds);
|
||||
List<CompanyMenuPO> companyMenus = companyMenuService.list(companyMenuWrapper);
|
||||
|
||||
// 构建映射关系
|
||||
Map<Long, CompanyMenuPO> companyMenuMap = companyMenus.stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getId, cm -> cm));
|
||||
|
||||
// 转换为VO
|
||||
return positionMenus.stream()
|
||||
.map(pm -> {
|
||||
PositionMenuVO vo = new PositionMenuVO();
|
||||
vo.setId(pm.getId());
|
||||
vo.setPositionId(pm.getPositionId());
|
||||
vo.setCompanyMenuId(pm.getCompanyMenuId());
|
||||
vo.setCreateTime(pm.getCreateTime());
|
||||
|
||||
PositionPO position = positionMap.get(pm.getPositionId());
|
||||
if (position != null) {
|
||||
vo.setPositionName(position.getName());
|
||||
}
|
||||
|
||||
CompanyMenuPO companyMenu = companyMenuMap.get(pm.getCompanyMenuId());
|
||||
if (companyMenu != null) {
|
||||
vo.setMenuName(companyMenu.getCustomName());
|
||||
vo.setMenuTitle(companyMenu.getCustomTitle());
|
||||
}
|
||||
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,6 +196,6 @@ public class PositionCompanyMenuServiceImpl implements PositionCompanyMenuServic
|
||||
wrapper.eq(PositionCompanyMenuPO::getPositionId, positionId)
|
||||
.eq(PositionCompanyMenuPO::getCompanyMenuId, companyMenuId);
|
||||
|
||||
return positionCompanyMenuMapper.selectCount(wrapper) > 0;
|
||||
return this.count(wrapper) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
+46
-1
@@ -6,10 +6,12 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.CompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.menu.CreateMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.menu.UpdateMenuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.MenuService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -22,9 +24,13 @@ import java.util.List;
|
||||
public class MenuController {
|
||||
|
||||
private final MenuService menuService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public MenuController(MenuService menuService) {
|
||||
public MenuController(MenuService menuService, CompanyMenuService companyMenuService, CurrentUserContext currentUserContext) {
|
||||
this.menuService = menuService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Operation(summary = "创建菜单", description = "创建新的菜单")
|
||||
@@ -79,4 +85,43 @@ public class MenuController {
|
||||
List<MenuVO> menuTree = menuService.getMenuTreeByRole(userRole);
|
||||
return ApiResponse.success(menuTree);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户菜单(动态路由)", description = "根据用户角色获取对应的菜单,用于前端动态路由")
|
||||
@GetMapping("/all")
|
||||
public ApiResponse<List<MenuVO>> getUserMenus() {
|
||||
// 获取当前用户信息
|
||||
String userRole = currentUserContext.getCurrentUserRole();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long positionId = currentUserContext.getCurrentUserPositionId();
|
||||
|
||||
List<MenuVO> menus;
|
||||
|
||||
switch (userRole) {
|
||||
case "super":
|
||||
// 超级管理员获取所有系统菜单
|
||||
menus = menuService.getAllMenus();
|
||||
break;
|
||||
case "admin":
|
||||
// 公司管理员获取公司所有菜单
|
||||
if (companyId == null) {
|
||||
menus = List.of();
|
||||
} else {
|
||||
menus = companyMenuService.getCompanyMenusForRoute(companyId);
|
||||
}
|
||||
break;
|
||||
case "user":
|
||||
// 普通用户根据职位获取菜单
|
||||
if (positionId == null) {
|
||||
menus = List.of();
|
||||
} else {
|
||||
menus = companyMenuService.getPositionMenusForRoute(positionId);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
menus = List.of();
|
||||
break;
|
||||
}
|
||||
|
||||
return ApiResponse.success(menus);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user