feat: 本次提交更新内容如下
基础框架完成,测试打包
This commit is contained in:
@@ -1,10 +1,23 @@
|
||||
import React from "react";
|
||||
import { BrowserRouter, useRoutes } from "react-router-dom";
|
||||
import homeRoutes from "./module/home";
|
||||
import aboutRoutes from "./module/about";
|
||||
import { BrowserRouter, useRoutes, RouteObject } from "react-router-dom";
|
||||
import PermissionRoute from "./permissionRoute";
|
||||
|
||||
function wrapWithPermission(route) {
|
||||
// 动态导入所有 module 下的 ts/tsx 路由模块
|
||||
const modules = import.meta.glob("./module/*.{ts,tsx}", { eager: true });
|
||||
|
||||
// 合并所有模块的默认导出(假设每个模块都是 export default 路由数组)
|
||||
const allRoutes: (RouteObject & { auth?: boolean; requiredRole?: string })[] =
|
||||
[];
|
||||
Object.values(modules).forEach((mod: any) => {
|
||||
if (Array.isArray(mod.default)) {
|
||||
allRoutes.push(...mod.default);
|
||||
}
|
||||
});
|
||||
|
||||
// 权限包装
|
||||
function wrapWithPermission(
|
||||
route: RouteObject & { auth?: boolean; requiredRole?: string }
|
||||
) {
|
||||
if (route.auth) {
|
||||
return {
|
||||
...route,
|
||||
@@ -18,10 +31,7 @@ function wrapWithPermission(route) {
|
||||
return route;
|
||||
}
|
||||
|
||||
const routes = [
|
||||
...homeRoutes.map(wrapWithPermission),
|
||||
...aboutRoutes.map(wrapWithPermission),
|
||||
];
|
||||
const routes = allRoutes.map(wrapWithPermission);
|
||||
|
||||
const AppRoutes = () => useRoutes(routes);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user