import DefaultErrorPage from '@/pages/ErrorPage' import { frameState, userState } from '@/store/mobx' import { NavigationGuardsProps, RouteModel } from '@/types/router' import { combinationPath } from '@/utils/util' import React, { Component } from 'react' import KeepAlive from 'react-activation' import { Redirect, Route } from "react-router-dom" class NavigationGuards extends Component { // constructor(props: NavigationGuardsProps) { // super(props) // userState.check() // } /** * 判断pathTarget是否包涵pathConfig/ * 即,pathConfig是否为pathTarget的子路径 * @param pathConfig 配置文件中的路径的全路径 * @param pathTarget 用户请求的路径 */ static switchRoute(pathConfig: string, pathTarget: string) { if (pathConfig === pathTarget) return true const reg = new RegExp(`(^${pathConfig})(?=/)`) return reg.test(pathTarget) } static permissionAuthentication(authArray: string[], myPermis: string) { return !!authArray.find((item) => item === myPermis) } shouldComponentUpdate(nextProps: any) { //与上次请求的路径相同时不重新渲染 if (this.props.location.pathname === nextProps.location.pathname) return false userState.check() return true } /** * 在路由配置信息中查找用户希望访问的组件 * @param parentPath 父路由的路径 * @param targetPath 用户当前希望访问的路径 * @param routeConfig 路由配置信息 * @param ErrorPage 错误页面 */ static findTargetRoute(parentPath: string, targetPath: string, routeConfig: RouteModel[]): { targetRoute: RouteModel | null | Error, realPath: string } { for (let i = 0; i < routeConfig.length; i++) { const item = routeConfig[i] const path = combinationPath(parentPath, item.path) if (targetPath && NavigationGuards.switchRoute(path, targetPath)) { return { targetRoute: { ...item, path }, realPath: path } } } return { targetRoute: null, realPath: "" } } render() { console.log("路由守卫渲染") const { location, routeConfig, match } = this.props //如果没有提供routeConfig则不做任何事情 if (!routeConfig || routeConfig.length <= 0) { return null } const ErrorPage = DefaultErrorPage //父路由的路径 const parentPath = match && match.path //用户当前希望访问的路径 const targetPath: string | undefined = location?.pathname //如果访问子菜单,则跳转到子菜单的默认路由 if (targetPath && frameState.defaultRouteMapping.has(targetPath)) { const targetDefaultRoute: string = frameState.defaultRouteMapping.get(targetPath) as string return } const findRes = targetPath && NavigationGuards.findTargetRoute(parentPath, targetPath, routeConfig) const targetRoute: RouteModel | null | "" | undefined | Error = findRes && findRes.targetRoute const isLogin = userState.isLogin if (targetRoute instanceof Error) return if (findRes) { const path: string = findRes.realPath ? findRes.realPath : "" targetRoute && frameState.setCurrentRoutePath(path) } if (!targetRoute) { return } if (targetRoute.redirect && !targetRoute.component) { return } //以下部分可提出去作为用户自定义部分 if (isLogin) { return } else { return } } } //已经登陆的状态下,处理路由 function LoginHandler(props: { targetRoute: RouteModel, ErrorPage: any }): any { const { targetRoute, ErrorPage } = props const { path, auth } = targetRoute const noCache = targetRoute.meta?.noCache ? targetRoute.meta.noCache : false if (path === '/login') { return } else if (!auth || NavigationGuards.permissionAuthentication(auth, userState.role)) { return ( ( noCache ? : ( ) ) }> ) } else { return } } //未登录状态下,处理路由 function NotLoginHandler(props: { targetRoute: RouteModel}): any { const { targetRoute } = props const { path, auth } = targetRoute if (auth && auth.length > 0) { return } else { return ( ) }> } } export default NavigationGuards