diff --git a/src/App.tsx b/src/App.tsx
new file mode 100644
index 0000000..c350d53
--- /dev/null
+++ b/src/App.tsx
@@ -0,0 +1,10 @@
+/** @format */
+
+import routes from "@/router/routes.ts";
+import { RouterGuard } from "@/router/guard.ts";
+
+function App() {
+ return
{RouterGuard(routes)}
;
+}
+
+export default App;
diff --git a/src/main.tsx b/src/main.tsx
index 06327d4..57c471f 100644
--- a/src/main.tsx
+++ b/src/main.tsx
@@ -2,19 +2,19 @@
import React from "react";
import ReactDOM from "react-dom/client";
-import { RouterProvider, createBrowserRouter } from "react-router-dom";
import "./assets/styles/index.less";
-import routeConfig from "./router";
import "virtual:svg-icons-register";
import { Provider as MobxProvider } from "mobx-react";
import { RootStore } from "@/store";
+import { BrowserRouter } from "react-router-dom";
+import App from "@/App.tsx";
-const router = createBrowserRouter(routeConfig);
ReactDOM.createRoot(document.getElementById("root")!).render(
-
+
+
+
- ,
,
);
diff --git a/src/router/guard.ts b/src/router/guard.ts
new file mode 100644
index 0000000..1d2fac6
--- /dev/null
+++ b/src/router/guard.ts
@@ -0,0 +1,62 @@
+/** @format */
+
+import { message } from "antd";
+import { useEffect } from "react";
+import {
+ Location,
+ NavigateFunction,
+ RouteObject,
+ useLocation,
+ useNavigate,
+ useRoutes,
+} from "react-router-dom";
+import localforage from "localforage";
+
+//递归查询对应的路由
+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;
+}
+
+//全局路由守卫
+async function guard(location: Location, navigate: NavigateFunction, routes: RouteObject[]) {
+ const { pathname } = location;
+
+ //找到对应的路由信息
+ const routedetail: RouteObject | null = searchRouteDetail(pathname, routes);
+
+ //没有找到路由,跳转404
+ if (!routedetail) {
+ navigate("/404");
+ return false;
+ }
+ //如果需要权限验证
+ if (
+ routedetail.path !== "/login" &&
+ routedetail.path !== "/register" &&
+ routedetail.path !== "/" &&
+ routedetail.path !== "/404"
+ ) {
+ const token: string | null = await localforage.getItem("token");
+ if (!token) {
+ message.warning("请先登录!").then();
+ navigate("/login");
+ return false;
+ }
+ }
+ return true;
+}
+
+export const RouterGuard = (routes: RouteObject[]) => {
+ const location: Location = useLocation();
+ const navigate: NavigateFunction = useNavigate();
+ useEffect(() => {
+ guard(location, navigate, routes).then();
+ }, [location, navigate, routes]);
+ return useRoutes(routes);
+};
diff --git a/src/router/index.ts b/src/router/routes.ts
similarity index 91%
rename from src/router/index.ts
rename to src/router/routes.ts
index bd09ac0..c8f0463 100644
--- a/src/router/index.ts
+++ b/src/router/routes.ts
@@ -15,14 +15,14 @@ const routes: RouteObject[] = [
path: "/",
Component: (props) => ComponentLoading(home, props),
},
- {
- path: "*",
- Component: NoFound,
- },
{
path: "/register",
Component: (props) => ComponentLoading(Register, props),
},
+ {
+ path: "/404",
+ Component: (props) => ComponentLoading(NoFound, props),
+ },
{
path: "/login",
Component: (props) => ComponentLoading(Login, props),
diff --git a/src/views/404/404.tsx b/src/views/404/404.tsx
index c7ba3eb..a155e45 100644
--- a/src/views/404/404.tsx
+++ b/src/views/404/404.tsx
@@ -7,7 +7,7 @@ import { useEffect } from "react";
export default () => {
const navigate = useNavigate();
const goBack = () => {
- navigate(-1);
+ navigate(-2);
};
useEffect(() => {
document.body.classList.add("not-fount-body");