mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 10:29:19 +08:00
调整获取用户路由的接口
This commit is contained in:
+1
-45
@@ -6,12 +6,10 @@ 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;
|
||||
|
||||
@@ -24,13 +22,9 @@ import java.util.List;
|
||||
public class MenuController {
|
||||
|
||||
private final MenuService menuService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public MenuController(MenuService menuService, CompanyMenuService companyMenuService, CurrentUserContext currentUserContext) {
|
||||
public MenuController(MenuService menuService) {
|
||||
this.menuService = menuService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Operation(summary = "创建菜单", description = "创建新的菜单")
|
||||
@@ -86,42 +80,4 @@ public class MenuController {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.RouteVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.RouteService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 路由管理控制器
|
||||
*/
|
||||
@Tag(name = "系统-路由管理", description = "动态路由相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/route")
|
||||
public class RouteController {
|
||||
|
||||
private final RouteService routeService;
|
||||
|
||||
public RouteController(RouteService routeService) {
|
||||
this.routeService = routeService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户路由(动态路由)", description = "根据用户角色获取对应的路由,用于前端动态路由")
|
||||
@GetMapping("/routes")
|
||||
public ApiResponse<List<RouteVO>> getUserRoutes() {
|
||||
List<RouteVO> routes = routeService.getUserRoutes();
|
||||
return ApiResponse.success(routes);
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 路由VO
|
||||
*/
|
||||
@Data
|
||||
public class RouteVO {
|
||||
|
||||
/**
|
||||
* 路由ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 父路由ID
|
||||
*/
|
||||
private Long parentId;
|
||||
|
||||
/**
|
||||
* 路由名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 路由标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 路由路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 页面组件
|
||||
*/
|
||||
private String component;
|
||||
|
||||
/**
|
||||
* 重定向地址
|
||||
*/
|
||||
private String redirect;
|
||||
|
||||
/**
|
||||
* 路由图标
|
||||
*/
|
||||
private String icon;
|
||||
|
||||
/**
|
||||
* 顺序
|
||||
*/
|
||||
private Integer order;
|
||||
|
||||
/**
|
||||
* 路由类型 (目录,菜单,按钮,内联,外链)
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 是否显示在菜单中 (0-否 1-是)
|
||||
*/
|
||||
private Integer visible;
|
||||
|
||||
/**
|
||||
* 状态 (0-禁用 1-启用)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 是否缓存
|
||||
*/
|
||||
private Integer keepAlive;
|
||||
|
||||
/**
|
||||
* 子路由列表
|
||||
*/
|
||||
private List<RouteVO> children;
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.RouteVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 路由服务接口
|
||||
*/
|
||||
public interface RouteService {
|
||||
|
||||
/**
|
||||
* 获取用户路由(动态路由)
|
||||
* 根据用户角色获取对应的路由,用于前端动态路由
|
||||
*
|
||||
* @return 路由列表
|
||||
*/
|
||||
List<RouteVO> getUserRoutes();
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.service.impl;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.CompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.PositionCompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.MenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.RouteVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.MenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.RouteService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 路由服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class RouteServiceImpl implements RouteService {
|
||||
|
||||
private final MenuService menuService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
private final PositionCompanyMenuService positionCompanyMenuService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public RouteServiceImpl(MenuService menuService,
|
||||
CompanyMenuService companyMenuService,
|
||||
PositionCompanyMenuService positionCompanyMenuService,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.menuService = menuService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
this.positionCompanyMenuService = positionCompanyMenuService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RouteVO> getUserRoutes() {
|
||||
// 获取当前用户信息
|
||||
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 = positionCompanyMenuService.getPositionMenusForRoute(positionId);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
menus = List.of();
|
||||
break;
|
||||
}
|
||||
|
||||
// 转换为RouteVO
|
||||
return convertToRouteVO(menus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将MenuVO转换为RouteVO
|
||||
*/
|
||||
private List<RouteVO> convertToRouteVO(List<MenuVO> menus) {
|
||||
return menus.stream()
|
||||
.map(this::convertMenuToRoute)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将单个MenuVO转换为RouteVO
|
||||
*/
|
||||
private RouteVO convertMenuToRoute(MenuVO menu) {
|
||||
RouteVO route = new RouteVO();
|
||||
BeanUtils.copyProperties(menu, route);
|
||||
|
||||
// 如果有子菜单,递归转换
|
||||
if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
|
||||
List<RouteVO> childRoutes = menu.getChildren().stream()
|
||||
.map(this::convertMenuToRoute)
|
||||
.toList();
|
||||
route.setChildren(childRoutes);
|
||||
}
|
||||
|
||||
return route;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user