feat: 添加路由拦截器/修复打包侧边栏图标失效问题
This commit is contained in:
8
src/router/getRouter.tsx
Normal file
8
src/router/getRouter.tsx
Normal file
@@ -0,0 +1,8 @@
|
||||
/** @format */
|
||||
|
||||
import { useRoutes } from "react-router-dom";
|
||||
import routes from "./routes";
|
||||
|
||||
export default function GetRouter() {
|
||||
return useRoutes(routes);
|
||||
}
|
||||
8
src/router/matchRouter.ts
Normal file
8
src/router/matchRouter.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/** @format */
|
||||
|
||||
import routes from "./routes.tsx";
|
||||
import { matchRoutes } from "react-router-dom";
|
||||
function matchRoute(path: string) {
|
||||
return matchRoutes(routes, path);
|
||||
}
|
||||
export default matchRoute;
|
||||
11
src/router/modules/main/share/modules/shareList.tsx
Normal file
11
src/router/modules/main/share/modules/shareList.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
/** @format */
|
||||
|
||||
import { lazy } from "react";
|
||||
|
||||
const ShareList = lazy(
|
||||
() =>
|
||||
new Promise((resolve: any) => {
|
||||
resolve(import("@/components/Main/Share/components/ShareList"));
|
||||
}),
|
||||
);
|
||||
export default ShareList;
|
||||
@@ -31,6 +31,7 @@ import Up from "@/router/modules/main/settings/up/up.ts";
|
||||
import Wangyi from "@/router/modules/main/settings/wangyi/wangyi.ts";
|
||||
import Jinshan from "@/router/modules/main/settings/jinshan/jinshan.ts";
|
||||
import Qiniu from "@/router/modules/main/settings/qiniu/qiniu.ts";
|
||||
import ShareList from "@/router/modules/main/share/modules/shareList.tsx";
|
||||
|
||||
const routes: RouteObject[] = [
|
||||
{
|
||||
@@ -42,7 +43,7 @@ const routes: RouteObject[] = [
|
||||
Component: (props) => ComponentLoading(Register, props),
|
||||
},
|
||||
{
|
||||
path: "*",
|
||||
path: "/404",
|
||||
Component: (props) => ComponentLoading(NoFound, props),
|
||||
},
|
||||
{
|
||||
@@ -86,6 +87,10 @@ const routes: RouteObject[] = [
|
||||
path: "/main/share",
|
||||
Component: MainShare,
|
||||
},
|
||||
{
|
||||
path: "/main/share/list/:id",
|
||||
Component: ShareList,
|
||||
},
|
||||
{
|
||||
path: "/main/setting",
|
||||
Component: MainSetting,
|
||||
|
||||
45
src/router/useAuth.tsx
Normal file
45
src/router/useAuth.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
/** @format */
|
||||
import matchAuth from "./matchRouter.ts";
|
||||
import { Navigate, useLocation } from "react-router-dom";
|
||||
import { message } from "antd";
|
||||
import { getStorageFromKey } from "@/utils/localStorage/config.ts";
|
||||
import React from "react";
|
||||
export default function AuthRoute(props: { children: React.ReactNode }) {
|
||||
const location: any = useLocation();
|
||||
const isLogin = getStorageFromKey("token");
|
||||
const currentPath: string = location.pathname;
|
||||
// 未登录情况访问login
|
||||
const condition: boolean =
|
||||
(currentPath === "/login" && !isLogin) ||
|
||||
currentPath === "/404" ||
|
||||
(currentPath === "register" && !isLogin) ||
|
||||
(currentPath === "forget" && !isLogin);
|
||||
// 登录后访问login
|
||||
const conditionWithLogin: boolean =
|
||||
(currentPath === "/login" && isLogin) ||
|
||||
(currentPath === "register" && isLogin) ||
|
||||
(currentPath === "forget" && isLogin);
|
||||
const findPath: any = matchAuth(currentPath);
|
||||
|
||||
if (condition) {
|
||||
return props.children;
|
||||
}
|
||||
if (conditionWithLogin && findPath) {
|
||||
return <Navigate to="/main/home" />;
|
||||
}
|
||||
|
||||
if (!findPath) {
|
||||
return <Navigate to="/404" />;
|
||||
}
|
||||
|
||||
if (!isLogin) {
|
||||
message
|
||||
.open({
|
||||
content: "请先登录!",
|
||||
type: "warning",
|
||||
})
|
||||
.then();
|
||||
return <Navigate to="/login" />;
|
||||
}
|
||||
return props.children;
|
||||
}
|
||||
Reference in New Issue
Block a user