From 60788d709005a76e852e652538492a00fc8c83a2 Mon Sep 17 00:00:00 2001 From: wenhua <1084668738@qq.com> Date: Mon, 16 Oct 2023 14:26:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=A9=E5=8D=8E:=20=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E8=AF=B7=E6=B1=82=E5=92=8C=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=AF=B7=E6=B1=82=E5=88=B0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=A4=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/RouteInterception.tsx | 39 ++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/router/RouteInterception.tsx b/src/router/RouteInterception.tsx index 660baf2..49b17e1 100644 --- a/src/router/RouteInterception.tsx +++ b/src/router/RouteInterception.tsx @@ -1,6 +1,9 @@ import { Navigate } from "react-router-dom" import Notfound from "../Notfound" import { getToken } from "../utils/auth" +import { useDispatch, useSelector } from "react-redux" +import { addRoutes, getAsyncRouters } from "../stores/permission" +import { addAuthInfo, getAuthInfo } from "../stores/auth" const Views = import.meta.glob('../view/**/*.tsx') // 判断是否有对应路径的映射文件 @@ -13,17 +16,35 @@ const hasMapFile = (filePath: string): boolean => { * @description 路由拦截组件 * @param {children: ReactDOM, filePath: string(文件路径)} */ -export default function RouteInterception ({children, filePath=""}: any) { - if(getToken()) { - if(filePath!=="") { - if(hasMapFile(filePath)) { - return children - } else { - return +export default function RouteInterception({ children, filePath = "" }: any) { + const routes: [] = useSelector((store: any) => store.permission) + const dispatch = useDispatch() + + const requestAsyncRoutes = async () => { + const { payload } = await dispatch(getAsyncRouters() as any) + dispatch(addRoutes(payload)) + } + + const requestAuthInfo = async () => { + const {payload} = await dispatch(getAuthInfo() as any) + dispatch(addAuthInfo(payload)) + } + + if (getToken()) { + if (!routes.length) { + requestAsyncRoutes() + requestAuthInfo() + } else { + if (filePath !== "") { + if (hasMapFile(filePath)) { + return children + } else { + return + } } + return children } - return children } else { - return + return } } \ No newline at end of file