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 , routeData ) => { let authorities ; routeData . forEach ( route => { if ( pathToRegexp ( `${ route . path }(.*)` ) . test ( path ) ) { if ( route . path === path ) { authorities = route . authority || authorities ; } if ( route . routes ) { authorities = getRouteAuthority ( path , route . routes ) || authorities ; } } } ) ; return authorities ; } ; const AuthComponent = ( { children , route = { routes : [ ] , } , location = { pathname : '' , } , user , } ) => { const { currentUser } = user ; const { routes = [ ] } = route ; const isLogin = currentUser && currentUser . name ; return ( < Authorized authority = { getRouteAuthority ( location . pathname , routes ) || '' } noMatch = { isLogin ? < Redirect to = "/exception/403" / > : < Redirect to = "/user/login" / > } > { children } < /Authorized>); } ; export default connect ( ( { user } ) => ( { user , } ) ) ( AuthComponent ) ;