import React from 'react'; import Redirect from 'umi/redirect'; import { connect } from 'dva'; import pathToRegexp from 'path-to-regexp'; import Authorized from '@/utils/Authorized'; import { ConnectProps, ConnectState, Route, UserModelState } from '@/models/connect'; 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)) { console.log('----------------------'); console.log('route.path:', route.path); console.log('path:', path); console.log('route.authority:', route.authority); authorities = route.authority || authorities; // 官方代码好像有问题https://github.com/ant-design/ant-design-pro/commit/4a10734dcf858c6363af719a5886a24ec1115b33#diff-2041540b332693486a24a543b6ba0cc8 // // exact match // if (route.path === path) { // authorities = route.authority || authorities; // } // get children authority recursively if (route.routes) { authorities = getRouteAuthority(path, route.routes) || authorities; } } }); return authorities; }; const AuthComponent: React.FC = ({ children, route = { routes: [], }, location = { pathname: '', }, user, }) => { const { currentUser } = user; const { routes = [] } = route; const isLogin = currentUser && currentUser.name; console.log('routes:', location.pathname, routes); console.log('authority:', getRouteAuthority(location.pathname, routes)); console.log('isLogin:', isLogin); return ( : } > {children} ); }; export default connect(({ user }: ConnectState) => ({ user, }))(AuthComponent);