mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
wip:动态路由相关
This commit is contained in:
-32
@@ -1,32 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -8,10 +8,14 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.ChangePasswordDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.dto.user.UpdateUserInfoDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.UserMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.UserService;
|
||||
import top.xyquan.ecomdmbbackend.utils.FileUploadUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户管理控制器
|
||||
*/
|
||||
@@ -21,9 +25,11 @@ import top.xyquan.ecomdmbbackend.utils.FileUploadUtils;
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final UserMenuService userMenuService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
public UserController(UserService userService, UserMenuService userMenuService) {
|
||||
this.userService = userService;
|
||||
this.userMenuService = userMenuService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户信息", description = "获取当前登录用户的详细信息")
|
||||
@@ -58,4 +64,11 @@ public class UserController {
|
||||
UserVO userVO = userService.uploadAvatar(file);
|
||||
return ApiResponse.success(userVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户菜单", description = "根据用户角色获取对应的菜单,用于前端动态路由")
|
||||
@GetMapping("/menu")
|
||||
public ApiResponse<List<UserMenuVO>> getUserRoutes() {
|
||||
List<UserMenuVO> routes = userMenuService.getUserRoutes();
|
||||
return ApiResponse.success(routes);
|
||||
}
|
||||
}
|
||||
|
||||
-82
@@ -1,82 +0,0 @@
|
||||
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;
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 路由VO - 符合 vben 动态路由标准,保留原有数据库字段
|
||||
*/
|
||||
@Data
|
||||
public class UserMenuVO {
|
||||
|
||||
/**
|
||||
* 路由名称 (必填,用于 Vue Router 的 name 属性)
|
||||
* 对应数据库: name 字段
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 路由路径
|
||||
* 对应数据库: path 字段
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 页面组件路径 (字符串类型,指向组件文件路径)
|
||||
* 对应数据库: component 字段
|
||||
*/
|
||||
private String component;
|
||||
|
||||
/**
|
||||
* 重定向地址
|
||||
* 对应数据库: redirect 字段
|
||||
*/
|
||||
private String redirect;
|
||||
|
||||
/**
|
||||
* 路由元信息 - 包含所有原有数据库字段
|
||||
*/
|
||||
private RouteMeta meta;
|
||||
|
||||
/**
|
||||
* 子路由列表 (扁平化处理,不嵌套)
|
||||
*/
|
||||
private List<UserMenuVO> children;
|
||||
|
||||
/**
|
||||
* 路由元信息 - 包含所有原有数据库字段
|
||||
*/
|
||||
@Data
|
||||
public static class RouteMeta {
|
||||
/**
|
||||
* 路由ID
|
||||
* 对应数据库: id 字段
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 父路由ID
|
||||
* 对应数据库: parent_id 字段
|
||||
*/
|
||||
private Long parentId;
|
||||
|
||||
/**
|
||||
* 路由标题
|
||||
* 对应数据库: title 字段
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 路由图标
|
||||
* 对应数据库: icon 字段
|
||||
*/
|
||||
private String icon;
|
||||
|
||||
/**
|
||||
* 顺序
|
||||
* 对应数据库: order 字段
|
||||
*/
|
||||
private Integer order;
|
||||
|
||||
/**
|
||||
* 路由类型 (catalog-目录,menu-菜单,button-按钮,embedded-内联,link-外链)
|
||||
* 对应数据库: type 字段
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 是否显示在菜单中 (0-否 1-是)
|
||||
* 对应数据库: visible 字段
|
||||
*/
|
||||
private Integer visible;
|
||||
|
||||
/**
|
||||
* 状态 (0-禁用 1-启用)
|
||||
* 对应数据库: status 字段
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 是否缓存 (vben 标准: boolean,但保留数据库的 Integer 类型)
|
||||
* 对应数据库: keep_alive 字段
|
||||
*/
|
||||
private Boolean keepAlive;
|
||||
|
||||
/**
|
||||
* 是否超级菜单 (0-否 1-是)
|
||||
* 对应数据库: super 字段
|
||||
*/
|
||||
private Integer superMenu;
|
||||
|
||||
/**
|
||||
* 权限码列表 (从 sys_menu_permission_map 表获取)
|
||||
*/
|
||||
private List<String> access;
|
||||
|
||||
/**
|
||||
* 是否隐藏菜单 (vben 标准字段)
|
||||
*/
|
||||
private Boolean hideInMenu;
|
||||
|
||||
/**
|
||||
* 是否隐藏面包屑 (vben 标准字段)
|
||||
*/
|
||||
private Boolean hideInBreadcrumb;
|
||||
|
||||
/**
|
||||
* 是否隐藏标签页 (vben 标准字段)
|
||||
*/
|
||||
private Boolean hideInTabs;
|
||||
|
||||
/**
|
||||
* 是否固定标签页 (vben 标准字段)
|
||||
*/
|
||||
private Boolean affixTab;
|
||||
|
||||
/**
|
||||
* 是否在菜单中显示子菜单 (vben 标准字段)
|
||||
*/
|
||||
private Boolean showChildrenInMenu;
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,13 +1,13 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.RouteVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.vo.UserMenuVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 路由服务接口
|
||||
*/
|
||||
public interface RouteService {
|
||||
public interface UserMenuService {
|
||||
|
||||
/**
|
||||
* 获取用户路由(动态路由)
|
||||
@@ -15,5 +15,5 @@ public interface RouteService {
|
||||
*
|
||||
* @return 路由列表
|
||||
*/
|
||||
List<RouteVO> getUserRoutes();
|
||||
List<UserMenuVO> getUserRoutes();
|
||||
}
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
+252
@@ -0,0 +1,252 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.service.impl;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
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.UserMenuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.MenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.service.UserMenuService;
|
||||
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 UserMenuServiceImpl implements UserMenuService {
|
||||
|
||||
private final MenuService menuService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
private final PositionCompanyMenuService positionCompanyMenuService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public UserMenuServiceImpl(MenuService menuService,
|
||||
CompanyMenuService companyMenuService,
|
||||
PositionCompanyMenuService positionCompanyMenuService,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.menuService = menuService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
this.positionCompanyMenuService = positionCompanyMenuService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserMenuVO> 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;
|
||||
}
|
||||
|
||||
// 如果菜单为空,返回测试数据(临时用于调试)
|
||||
if (menus.isEmpty()) {
|
||||
menus = createTestMenus();
|
||||
}
|
||||
|
||||
// 转换为RouteVO
|
||||
return convertToRouteVO(menus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将MenuVO转换为RouteVO - 符合 vben 动态路由标准
|
||||
* 构建树形结构,只返回根级路由,子路由放入children中
|
||||
*/
|
||||
private List<UserMenuVO> convertToRouteVO(List<MenuVO> menus) {
|
||||
// 构建树形结构
|
||||
return buildRouteTree(menus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建路由树形结构
|
||||
*/
|
||||
private List<UserMenuVO> buildRouteTree(List<MenuVO> menus) {
|
||||
// 按父ID分组
|
||||
Map<Long, List<MenuVO>> parentMap = menus.stream()
|
||||
.collect(Collectors.groupingBy(menu -> menu.getParentId() != null ? menu.getParentId() : 0L));
|
||||
|
||||
// 递归构建树
|
||||
return buildTreeRecursive(parentMap, 0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归构建树形结构
|
||||
*/
|
||||
private List<UserMenuVO> buildTreeRecursive(Map<Long, List<MenuVO>> parentMap, Long parentId) {
|
||||
List<MenuVO> children = parentMap.get(parentId);
|
||||
if (children == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return children.stream()
|
||||
.map(menu -> {
|
||||
UserMenuVO route = convertMenuToRoute(menu);
|
||||
|
||||
// 递归构建子路由
|
||||
List<UserMenuVO> childRoutes = buildTreeRecursive(parentMap, menu.getId());
|
||||
route.setChildren(childRoutes);
|
||||
|
||||
return route;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将单个MenuVO转换为RouteVO - 符合 vben 动态路由标准
|
||||
*/
|
||||
private UserMenuVO convertMenuToRoute(MenuVO menu) {
|
||||
UserMenuVO route = new UserMenuVO();
|
||||
|
||||
// 设置 vben 标准的路由字段
|
||||
route.setName(menu.getName());
|
||||
route.setPath(menu.getPath());
|
||||
route.setComponent(menu.getComponent());
|
||||
route.setRedirect(menu.getRedirect());
|
||||
|
||||
// 设置路由元信息,包含所有原有数据库字段
|
||||
UserMenuVO.RouteMeta meta = new UserMenuVO.RouteMeta();
|
||||
meta.setId(menu.getId());
|
||||
meta.setParentId(menu.getParentId());
|
||||
meta.setTitle(menu.getTitle());
|
||||
meta.setIcon(menu.getIcon());
|
||||
meta.setOrder(menu.getOrder());
|
||||
meta.setType(menu.getType());
|
||||
meta.setVisible(menu.getVisible());
|
||||
meta.setStatus(menu.getStatus());
|
||||
// 转换 keepAlive: Integer -> Boolean (0=false, 1=true)
|
||||
meta.setKeepAlive(menu.getKeepAlive() != null && menu.getKeepAlive() == 1);
|
||||
meta.setSuperMenu(menu.getSuperMenu());
|
||||
|
||||
// 设置 vben 标准字段的默认值
|
||||
meta.setHideInMenu(menu.getVisible() == 0);
|
||||
meta.setHideInBreadcrumb(false);
|
||||
meta.setHideInTabs(false);
|
||||
meta.setAffixTab(false);
|
||||
meta.setShowChildrenInMenu(true);
|
||||
|
||||
// TODO: 从 sys_menu_permission_map 表获取权限码列表
|
||||
// meta.setAccess(permissionService.getMenuPermissions(menu.getId()));
|
||||
|
||||
route.setMeta(meta);
|
||||
|
||||
// 注意:children 由 buildTreeRecursive 方法处理,这里不设置
|
||||
|
||||
return route;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建测试菜单数据(临时用于调试)
|
||||
*/
|
||||
private List<MenuVO> createTestMenus() {
|
||||
List<MenuVO> testMenus = new ArrayList<>();
|
||||
|
||||
// 测试仪表盘菜单
|
||||
MenuVO dashboard = new MenuVO();
|
||||
dashboard.setId(1L);
|
||||
dashboard.setParentId(null);
|
||||
dashboard.setName("Dashboard");
|
||||
dashboard.setTitle("仪表盘");
|
||||
dashboard.setPath("/dashboard");
|
||||
dashboard.setComponent("BasicLayout");
|
||||
dashboard.setIcon("lucide:layout-dashboard");
|
||||
dashboard.setOrder(-1);
|
||||
dashboard.setType("catalog");
|
||||
dashboard.setVisible(1);
|
||||
dashboard.setStatus(1);
|
||||
dashboard.setKeepAlive(0);
|
||||
dashboard.setSuperMenu(0);
|
||||
|
||||
// 仪表盘子菜单
|
||||
List<MenuVO> dashboardChildren = new ArrayList<>();
|
||||
|
||||
MenuVO analytics = new MenuVO();
|
||||
analytics.setId(2L);
|
||||
analytics.setParentId(1L);
|
||||
analytics.setName("Analytics");
|
||||
analytics.setTitle("分析页");
|
||||
analytics.setPath("/analytics");
|
||||
analytics.setComponent("/dashboard/analytics/index");
|
||||
analytics.setIcon("lucide:area-chart");
|
||||
analytics.setOrder(1);
|
||||
analytics.setType("menu");
|
||||
analytics.setVisible(1);
|
||||
analytics.setStatus(1);
|
||||
analytics.setKeepAlive(0);
|
||||
analytics.setSuperMenu(0);
|
||||
|
||||
dashboardChildren.add(analytics);
|
||||
dashboard.setChildren(dashboardChildren);
|
||||
testMenus.add(dashboard);
|
||||
|
||||
// 测试系统管理菜单
|
||||
MenuVO system = new MenuVO();
|
||||
system.setId(3L);
|
||||
system.setParentId(null);
|
||||
system.setName("System");
|
||||
system.setTitle("系统管理");
|
||||
system.setPath("/system");
|
||||
system.setComponent("BasicLayout");
|
||||
system.setIcon("lucide:settings");
|
||||
system.setOrder(999);
|
||||
system.setType("catalog");
|
||||
system.setVisible(1);
|
||||
system.setStatus(1);
|
||||
system.setKeepAlive(0);
|
||||
system.setSuperMenu(0);
|
||||
|
||||
// 系统管理子菜单
|
||||
List<MenuVO> systemChildren = new ArrayList<>();
|
||||
|
||||
MenuVO menuManagement = new MenuVO();
|
||||
menuManagement.setId(4L);
|
||||
menuManagement.setParentId(3L);
|
||||
menuManagement.setName("MenuManagement");
|
||||
menuManagement.setTitle("菜单管理");
|
||||
menuManagement.setPath("/system/menu-management");
|
||||
menuManagement.setComponent("/system/menu-management/index");
|
||||
menuManagement.setIcon("lucide:menu");
|
||||
menuManagement.setOrder(1);
|
||||
menuManagement.setType("menu");
|
||||
menuManagement.setVisible(1);
|
||||
menuManagement.setStatus(1);
|
||||
menuManagement.setKeepAlive(0);
|
||||
menuManagement.setSuperMenu(0);
|
||||
|
||||
systemChildren.add(menuManagement);
|
||||
system.setChildren(systemChildren);
|
||||
testMenus.add(system);
|
||||
|
||||
return testMenus;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { UserInfo } from '@vben/types';
|
||||
import type { RouteRecordStringComponent } from '@vben/types';
|
||||
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
@@ -8,3 +9,12 @@ import { requestClient } from '#/api/request';
|
||||
export async function getUserInfoApi() {
|
||||
return requestClient.get<UserInfo>('/user/info');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户菜单路由
|
||||
* 调用后端 UserController 的 /user/menu 接口
|
||||
* 根据用户角色返回对应的路由列表
|
||||
*/
|
||||
export async function getUserMenuApi() {
|
||||
return requestClient.get<RouteRecordStringComponent[]>('/user/menu');
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ export const overridesPreferences = defineOverridesPreferences({
|
||||
// overrides
|
||||
app: {
|
||||
name: import.meta.env.VITE_APP_TITLE,
|
||||
accessMode: 'backend', // 启用后端访问控制模式
|
||||
},
|
||||
navigation: {
|
||||
accordion: false, // 关闭手风琴模式,允许多个菜单同时展开
|
||||
|
||||
@@ -6,11 +6,11 @@ import type {
|
||||
import { generateAccessible } from '@vben/access';
|
||||
import { preferences } from '@vben/preferences';
|
||||
|
||||
// import { message } from 'ant-design-vue';
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
// import { getAllMenusApi } from '#/api';
|
||||
import { getUserMenuApi } from '#/api/core/user';
|
||||
import { BasicLayout, IFrameView } from '#/layouts';
|
||||
// import { $t } from '#/locales';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');
|
||||
|
||||
@@ -24,13 +24,21 @@ async function generateAccess(options: GenerateMenuAndRoutesOptions) {
|
||||
|
||||
return await generateAccessible(preferences.app.accessMode, {
|
||||
...options,
|
||||
// fetchMenuListAsync: async () => {
|
||||
// message.loading({
|
||||
// content: `${$t('common.loadingMenu')}...`,
|
||||
// duration: 1.5,
|
||||
// });
|
||||
// return await getAllMenusApi();
|
||||
// },
|
||||
fetchMenuListAsync: async () => {
|
||||
console.log('🚀 开始获取用户菜单路由...');
|
||||
message.loading({
|
||||
content: `${$t('common.loadingMenu')}...`,
|
||||
duration: 1.5,
|
||||
});
|
||||
try {
|
||||
const routes = await getUserMenuApi();
|
||||
console.log('✅ 获取用户菜单路由成功:', routes);
|
||||
return routes;
|
||||
} catch (error) {
|
||||
console.error('❌ 获取用户菜单路由失败:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
// 可以指定没有权限跳转403页面
|
||||
forbiddenComponent,
|
||||
// 如果 route.meta.menuVisibleWithForbidden = true
|
||||
|
||||
@@ -87,14 +87,21 @@ function setupAccessGuard(router: Router) {
|
||||
|
||||
// 是否已经生成过动态路由
|
||||
if (accessStore.isAccessChecked) {
|
||||
console.log('✅ 动态路由已生成,跳过');
|
||||
return true;
|
||||
}
|
||||
|
||||
console.log('🚀 开始生成动态路由...');
|
||||
console.log('📊 当前访问模式:', preferences.app.accessMode);
|
||||
|
||||
// 生成路由表
|
||||
// 当前登录用户拥有的角色标识列表
|
||||
const userInfo = userStore.userInfo || (await authStore.fetchUserInfo());
|
||||
const userRoles = userInfo.roles ?? [];
|
||||
|
||||
console.log('👤 用户信息:', userInfo);
|
||||
console.log('🎭 用户角色:', userRoles);
|
||||
|
||||
// 生成菜单和路由
|
||||
const { accessibleMenus, accessibleRoutes } = await generateAccess({
|
||||
roles: userRoles,
|
||||
@@ -103,6 +110,9 @@ function setupAccessGuard(router: Router) {
|
||||
routes: accessRoutes,
|
||||
});
|
||||
|
||||
console.log('📋 可访问菜单:', accessibleMenus);
|
||||
console.log('🛣️ 可访问路由:', accessibleRoutes);
|
||||
|
||||
// 保存菜单信息和路由信息
|
||||
accessStore.setAccessMenus(accessibleMenus);
|
||||
accessStore.setAccessRoutes(accessibleRoutes);
|
||||
|
||||
@@ -86,11 +86,28 @@ export function useTabbar() {
|
||||
};
|
||||
|
||||
function wrapperTabLocale(tab: RouteLocationNormalizedGeneric) {
|
||||
const title = tab?.meta?.title;
|
||||
let processedTitle = 'Unknown';
|
||||
|
||||
try {
|
||||
if (title && typeof title === 'string' && title.trim()) {
|
||||
// 尝试国际化处理
|
||||
processedTitle = $t(title);
|
||||
} else if (tab?.name) {
|
||||
// 如果没有 title,使用路由名称
|
||||
processedTitle = tab.name as string;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('国际化处理失败,使用原始标题:', title, error);
|
||||
// 如果国际化失败,直接使用原始标题
|
||||
processedTitle = title || (tab?.name as string) || 'Unknown';
|
||||
}
|
||||
|
||||
return {
|
||||
...tab,
|
||||
meta: {
|
||||
...tab?.meta,
|
||||
title: $t(tab?.meta?.title as string),
|
||||
title: processedTitle,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user