import { ConnectProps, ConnectState, Route, UserModelState } from '@/models/connect'; import Authorized from '@/utils/Authorized'; import React from 'react'; import Redirect from 'umi/redirect'; import { connect } from 'dva'; import pathToRegexp from 'path-to-regexp'; interface AuthComponentProps extends ConnectProps { user: UserModelState; } const getRouteAuthority = (path: string, routeData: Route[]) => { let authorities: string[] | string | undefined; routeData.forEach(route => { // match prefix if (pathToRegexp(`${route.path}(.*)`).test(path)) { // exact match if (route.path === path) { authorities = route.authority || authorities; } // get children authority recursively if (route.routes) { authorities = getRouteAuthority(path, route.routes) || authorities; } } }); console.log('authorities:', authorities); return authorities; }; const AuthComponent: React.FC = ({ children, route = { routes: [], }, location = { pathname: '', }, user, }) => { const { currentUser } = user; const { routes = [] } = route; const isLogin = currentUser && currentUser.name; return ( : } > {children} ); }; export default connect(({ user }: ConnectState) => ({ user, }))(AuthComponent);