mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 02:39:19 +08:00
wip: 岗位权限
This commit is contained in:
+7
-7
@@ -7,7 +7,7 @@ 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.common.annotation.RequireRole;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.CompanyMenuVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,19 +44,19 @@ public class CompanyMenuController {
|
||||
return ApiResponse.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取公司所有菜单列表", description = "获取当前用户公司的所有菜单列表,中间表只作为过滤条件,不覆盖系统菜单字段")
|
||||
@Operation(summary = "获取公司所有菜单列表", description = "获取当前用户公司的所有菜单列表,包含公司菜单中间表ID,中间表只作为过滤条件,不覆盖系统菜单字段")
|
||||
@GetMapping("/menus")
|
||||
@RequireRole(description = "获取公司菜单列表", allowUser = false)
|
||||
public ApiResponse<List<MenuVO>> getCompanyMenus() {
|
||||
List<MenuVO> menus = companyMenuService.getCompanyMenus();
|
||||
public ApiResponse<List<CompanyMenuVO>> getCompanyMenus() {
|
||||
List<CompanyMenuVO> menus = companyMenuService.getCompanyMenus();
|
||||
return ApiResponse.success(menus);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取公司完整菜单树", description = "获取当前用户公司的完整菜单树结构,中间表只作为过滤条件,不覆盖系统菜单字段")
|
||||
@Operation(summary = "获取公司完整菜单树", description = "获取当前用户公司的完整菜单树结构,包含公司菜单中间表ID,中间表只作为过滤条件,不覆盖系统菜单字段")
|
||||
@GetMapping("/menu-tree")
|
||||
@RequireRole(description = "获取公司菜单树", allowUser = false)
|
||||
public ApiResponse<List<MenuVO>> getCompanyMenuTree() {
|
||||
List<MenuVO> menuTree = companyMenuService.getCompanyMenuTree();
|
||||
public ApiResponse<List<CompanyMenuVO>> getCompanyMenuTree() {
|
||||
List<CompanyMenuVO> menuTree = companyMenuService.getCompanyMenuTree();
|
||||
return ApiResponse.success(menuTree);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-57
@@ -1,69 +1,19 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 公司菜单VO
|
||||
* 继承MenuVO,添加公司菜单中间表ID字段
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "公司菜单VO")
|
||||
public class CompanyMenuVO {
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class CompanyMenuVO extends MenuVO {
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
* 公司菜单中间表ID
|
||||
*/
|
||||
@Schema(description = "公司ID", example = "1")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 公司名称
|
||||
*/
|
||||
@Schema(description = "公司名称", example = "某某科技有限公司")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 公司地址
|
||||
*/
|
||||
@Schema(description = "公司地址", example = "北京市朝阳区xxx街道xxx号")
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 联系人
|
||||
*/
|
||||
@Schema(description = "联系人", example = "张三")
|
||||
private String contactPerson;
|
||||
|
||||
/**
|
||||
* 联系电话
|
||||
*/
|
||||
@Schema(description = "联系电话", example = "13800138000")
|
||||
private String contactPhone;
|
||||
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
@Schema(description = "邮箱", example = "contact@company.com")
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* logo
|
||||
*/
|
||||
@Schema(description = "公司logo", example = "https://example.com/logo.png")
|
||||
private String logo;
|
||||
|
||||
/**
|
||||
* 状态 (0-禁用 1-正常)
|
||||
*/
|
||||
@Schema(description = "公司状态", example = "1", allowableValues = { "0", "1" })
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 菜单列表
|
||||
*/
|
||||
@Schema(description = "菜单列表")
|
||||
private List<MenuVO> menus;
|
||||
}
|
||||
private Long companyMenuId;
|
||||
}
|
||||
+2
-2
@@ -50,13 +50,13 @@ public interface CompanyMenuService extends IService<CompanyMenuPO> {
|
||||
*
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<MenuVO> getCompanyMenus();
|
||||
List<CompanyMenuVO> getCompanyMenus();
|
||||
|
||||
/**
|
||||
* 获取公司完整菜单树
|
||||
*
|
||||
* @return 菜单树列表
|
||||
*/
|
||||
List<MenuVO> getCompanyMenuTree();
|
||||
List<CompanyMenuVO> getCompanyMenuTree();
|
||||
|
||||
}
|
||||
|
||||
+75
-7
@@ -214,7 +214,7 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
* @return 菜单列表
|
||||
*/
|
||||
@Override
|
||||
public List<MenuVO> getCompanyMenus() {
|
||||
public List<CompanyMenuVO> getCompanyMenus() {
|
||||
// 从用户上下文获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
@@ -244,9 +244,13 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
|
||||
List<MenuPO> menus = menuService.list(menuWrapper);
|
||||
|
||||
// 转换为MenuVO,不覆盖自定义字段
|
||||
// 构建公司菜单ID到CompanyMenuPO的映射
|
||||
Map<Long, CompanyMenuPO> companyMenuMap = companyMenus.stream()
|
||||
.collect(Collectors.toMap(CompanyMenuPO::getMenuId, cm -> cm));
|
||||
|
||||
// 转换为CompanyMenuVO,包含companyMenuId
|
||||
return menus.stream()
|
||||
.map(this::convertMenuToVO)
|
||||
.map(menu -> convertToCompanyMenuVO(menu, companyMenuMap.get(menu.getId())))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -255,16 +259,28 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
* @return 菜单树列表
|
||||
*/
|
||||
@Override
|
||||
public List<MenuVO> getCompanyMenuTree() {
|
||||
public List<CompanyMenuVO> getCompanyMenuTree() {
|
||||
// 获取公司所有菜单列表
|
||||
List<MenuVO> menuList = getCompanyMenus();
|
||||
List<CompanyMenuVO> menuList = getCompanyMenus();
|
||||
|
||||
// 构建树形结构
|
||||
return buildMenuTree(menuList);
|
||||
return buildCompanyMenuTree(menuList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树
|
||||
* 构建公司菜单树
|
||||
*/
|
||||
private List<CompanyMenuVO> buildCompanyMenuTree(List<CompanyMenuVO> menuList) {
|
||||
// 按父ID分组
|
||||
Map<Long, List<CompanyMenuVO>> parentMap = menuList.stream()
|
||||
.collect(Collectors.groupingBy(menu -> menu.getParentId() != null ? menu.getParentId() : 0L));
|
||||
|
||||
// 递归构建树
|
||||
return buildCompanyMenuTreeRecursive(parentMap, 0L, new ArrayList<>(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树(保留原方法用于其他用途)
|
||||
*/
|
||||
private List<MenuVO> buildMenuTree(List<MenuVO> menuList) {
|
||||
// 按父ID分组
|
||||
@@ -308,6 +324,58 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
||||
return children;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归构建公司菜单树形结构(带循环引用检测)
|
||||
*/
|
||||
private List<CompanyMenuVO> buildCompanyMenuTreeRecursive(Map<Long, List<CompanyMenuVO>> parentMap, Long parentId,
|
||||
List<Long> visitedIds, int depth) {
|
||||
// 防止无限递归,设置最大深度限制
|
||||
if (depth > 100) {
|
||||
throw new BusinessException("菜单层级过深,可能存在循环引用");
|
||||
}
|
||||
|
||||
List<CompanyMenuVO> children = parentMap.get(parentId);
|
||||
if (children == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
for (CompanyMenuVO menu : children) {
|
||||
// 检查是否已经访问过该节点,防止循环引用
|
||||
if (visitedIds.contains(menu.getId())) {
|
||||
throw new BusinessException("检测到菜单循环引用,菜单ID: " + menu.getId());
|
||||
}
|
||||
|
||||
// 创建新访问路径,包含当前节点
|
||||
List<Long> newVisitedIds = new ArrayList<>(visitedIds);
|
||||
newVisitedIds.add(menu.getId());
|
||||
|
||||
// 递归构建子菜单
|
||||
List<CompanyMenuVO> childList = buildCompanyMenuTreeRecursive(parentMap, menu.getId(), newVisitedIds, depth + 1);
|
||||
menu.setChildren((List<MenuVO>) (List<?>) childList);
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换MenuPO为CompanyMenuVO(包含权限信息和公司菜单ID)
|
||||
*/
|
||||
private CompanyMenuVO convertToCompanyMenuVO(MenuPO menu, CompanyMenuPO companyMenu) {
|
||||
CompanyMenuVO companyMenuVO = new CompanyMenuVO();
|
||||
BeanUtils.copyProperties(menu, companyMenuVO);
|
||||
|
||||
// 设置公司菜单ID
|
||||
if (companyMenu != null) {
|
||||
companyMenuVO.setCompanyMenuId(companyMenu.getId());
|
||||
}
|
||||
|
||||
// 查询菜单关联的权限
|
||||
List<PermissionVO> permissions = menuMapper.selectPermissionsByMenuId(menu.getId());
|
||||
companyMenuVO.setPermissions(permissions);
|
||||
|
||||
return companyMenuVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换MenuPO为MenuVO(包含权限信息)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user