初始化fateverse
This commit is contained in:
71
src/router/index.tsx
Normal file
71
src/router/index.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
import { getToken } from '../utils/auth'
|
||||
import LayOut from '../layout/index'
|
||||
import Home from '../view/home'
|
||||
import { Suspense, lazy } from 'react'
|
||||
import { Navigate } from 'react-router-dom'
|
||||
import Loading from '../Loading'
|
||||
import Notfound from '../Notfound'
|
||||
const Views = import.meta.glob('../view/**/*.tsx')
|
||||
interface Meta {
|
||||
title: string,
|
||||
redirect?: boolean
|
||||
}
|
||||
|
||||
type route = {
|
||||
path: string,
|
||||
element: any,
|
||||
meta?: any,
|
||||
children?: Array<route>
|
||||
}
|
||||
|
||||
const lazyLoad = (viewName: string) => {
|
||||
if(viewName === 'login') {
|
||||
const LoginView = lazy(()=>import('../Login'))
|
||||
return <LoginView />
|
||||
}
|
||||
const View = lazy(()=>import(`../view/${viewName}.tsx`))
|
||||
return <View/>
|
||||
}
|
||||
|
||||
// 判断是否有对应路径的映射文件
|
||||
const hasMapFile = (filePath: string): boolean => {
|
||||
let flag: boolean = false
|
||||
Views[`../view/${filePath}.tsx`] ? flag = true : flag = false
|
||||
return flag
|
||||
}
|
||||
|
||||
// 路由拦截
|
||||
export const RouteInterception = ({children, filePath=""}: any) => {
|
||||
if(getToken()) {
|
||||
if(filePath!=="") {
|
||||
if(hasMapFile(filePath)) {
|
||||
return children
|
||||
} else {
|
||||
return <Notfound />
|
||||
}
|
||||
}
|
||||
return children
|
||||
} else {
|
||||
return <Navigate to='/login' />
|
||||
}
|
||||
}
|
||||
|
||||
// 常驻路由
|
||||
const currentRouter: Array<route> = [
|
||||
{
|
||||
path: '/',
|
||||
element: <RouteInterception><Suspense fallback={<Loading />}><LayOut /></Suspense></RouteInterception>,
|
||||
children: [
|
||||
{
|
||||
path: "", //登录后默认跳转路径
|
||||
element : <Home/>
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
element: lazyLoad('login')
|
||||
}
|
||||
]
|
||||
|
||||
export default currentRouter
|
||||
Reference in New Issue
Block a user