温华: 迁移路由请求和用户信息请求到路由拦截处
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
import { Navigate } from "react-router-dom"
|
import { Navigate } from "react-router-dom"
|
||||||
import Notfound from "../Notfound"
|
import Notfound from "../Notfound"
|
||||||
import { getToken } from "../utils/auth"
|
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')
|
const Views = import.meta.glob('../view/**/*.tsx')
|
||||||
// 判断是否有对应路径的映射文件
|
// 判断是否有对应路径的映射文件
|
||||||
@@ -13,17 +16,35 @@ const hasMapFile = (filePath: string): boolean => {
|
|||||||
* @description 路由拦截组件
|
* @description 路由拦截组件
|
||||||
* @param {children: ReactDOM, filePath: string(文件路径)}
|
* @param {children: ReactDOM, filePath: string(文件路径)}
|
||||||
*/
|
*/
|
||||||
export default function RouteInterception ({children, filePath=""}: any) {
|
export default function RouteInterception({ children, filePath = "" }: any) {
|
||||||
if(getToken()) {
|
const routes: [] = useSelector((store: any) => store.permission)
|
||||||
if(filePath!=="") {
|
const dispatch = useDispatch()
|
||||||
if(hasMapFile(filePath)) {
|
|
||||||
return children
|
const requestAsyncRoutes = async () => {
|
||||||
} else {
|
const { payload } = await dispatch(getAsyncRouters() as any)
|
||||||
return <Notfound />
|
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 <Navigate to='/404' />
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return children
|
||||||
}
|
}
|
||||||
return children
|
|
||||||
} else {
|
} else {
|
||||||
return <Navigate to='/login' />
|
return <Navigate to='/login' replace={true} />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user