feat: 删除路由拦截器,添加二级路由
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
/** @format */
|
||||
|
||||
import { useEffect } from "react";
|
||||
import {
|
||||
Location,
|
||||
NavigateFunction,
|
||||
RouteObject,
|
||||
useLocation,
|
||||
useNavigate,
|
||||
useRoutes,
|
||||
} from "react-router-dom";
|
||||
|
||||
/**
|
||||
* 递归查询对应的路由
|
||||
* @param path
|
||||
* @param routes
|
||||
*/
|
||||
export function searchRouteDetail(path: string, routes: RouteObject[]): RouteObject | null {
|
||||
for (const item of routes) {
|
||||
if (item.path === path) return item;
|
||||
if (item.children) {
|
||||
return searchRouteDetail(path, item.children);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局路由守卫
|
||||
* @param location
|
||||
* @param navigate
|
||||
* @param routes
|
||||
*/
|
||||
async function guard(location: Location, navigate: NavigateFunction, routes: RouteObject[]) {
|
||||
const { pathname } = location;
|
||||
|
||||
//找到对应的路由信息
|
||||
const routerDetail: RouteObject | null = searchRouteDetail(pathname, routes);
|
||||
|
||||
//没有找到路由,跳转404
|
||||
if (!routerDetail) {
|
||||
navigate("/404");
|
||||
return false;
|
||||
}
|
||||
//如果需要权限验证
|
||||
// if (
|
||||
// routerDetail.path !== "/login" &&
|
||||
// routerDetail.path !== "/register" &&
|
||||
// routerDetail.path !== "/" &&
|
||||
// routerDetail.path !== "/404" &&
|
||||
// routerDetail.path !== "/forget"
|
||||
// ) {
|
||||
// const token: string | null = getStorageFromKey("token");
|
||||
// if (!token) {
|
||||
// message.warning("请先登录!").then();
|
||||
// navigate("/login");
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由守卫
|
||||
* @param routes
|
||||
* @constructor
|
||||
*/
|
||||
export const RouterGuard = (routes: RouteObject[]) => {
|
||||
const location: Location<any> = useLocation();
|
||||
const navigate: NavigateFunction = useNavigate();
|
||||
useEffect(() => {
|
||||
guard(location, navigate, routes).then();
|
||||
}, [location, navigate, routes]);
|
||||
return useRoutes(routes);
|
||||
};
|
11
src/router/modules/main/bucket/index.ts
Normal file
11
src/router/modules/main/bucket/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/** @format */
|
||||
|
||||
import { lazy } from "react";
|
||||
|
||||
const MainBucket = lazy(
|
||||
() =>
|
||||
new Promise((resolve: any) => {
|
||||
setTimeout(() => resolve(import("@/components/Main/Bucket")), 500);
|
||||
}),
|
||||
);
|
||||
export default MainBucket;
|
11
src/router/modules/main/file/index.ts
Normal file
11
src/router/modules/main/file/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/** @format */
|
||||
|
||||
import { lazy } from "react";
|
||||
|
||||
const MainFile = lazy(
|
||||
() =>
|
||||
new Promise((resolve: any) => {
|
||||
setTimeout(() => resolve(import("@/components/Main/File")), 500);
|
||||
}),
|
||||
);
|
||||
export default MainFile;
|
11
src/router/modules/main/home/index.ts
Normal file
11
src/router/modules/main/home/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/** @format */
|
||||
|
||||
import { lazy } from "react";
|
||||
|
||||
const MainHome = lazy(
|
||||
() =>
|
||||
new Promise((resolve: any) => {
|
||||
setTimeout(() => resolve(import("@/components/Main/Home")), 500);
|
||||
}),
|
||||
);
|
||||
export default MainHome;
|
11
src/router/modules/main/user/index.ts
Normal file
11
src/router/modules/main/user/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/** @format */
|
||||
|
||||
import { lazy } from "react";
|
||||
|
||||
const MainUser = lazy(
|
||||
() =>
|
||||
new Promise((resolve: any) => {
|
||||
setTimeout(() => resolve(import("@/components/Main/User")), 500);
|
||||
}),
|
||||
);
|
||||
export default MainUser;
|
@@ -10,6 +10,10 @@ import ComponentLoading from "@/components/ComponentLoading";
|
||||
import Loading from "./modules/loading";
|
||||
import Forget from "@/router/modules/forget";
|
||||
import Home from "@/router/modules/home";
|
||||
import MainHome from "@/router/modules/main/home";
|
||||
import MainBucket from "@/router/modules/main/bucket";
|
||||
import MainFile from "@/router/modules/main/file";
|
||||
import MainUser from "@/router/modules/main/user";
|
||||
|
||||
const routes: RouteObject[] = [
|
||||
{
|
||||
@@ -35,6 +39,25 @@ const routes: RouteObject[] = [
|
||||
{
|
||||
path: "/main",
|
||||
Component: (props) => ComponentLoading(Main, props),
|
||||
children: [
|
||||
{
|
||||
index: true,
|
||||
path: "/main/home",
|
||||
Component: MainHome,
|
||||
},
|
||||
{
|
||||
path: "/main/bucket",
|
||||
Component: MainBucket,
|
||||
},
|
||||
{
|
||||
path: "/main/file",
|
||||
Component: MainFile,
|
||||
},
|
||||
{
|
||||
path: "/main/user",
|
||||
Component: MainUser,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: "/home",
|
||||
|
Reference in New Issue
Block a user