committed by
GitHub
4 changed files with 43 additions and 42 deletions
@ -1,13 +1,44 @@ |
|||
import React from 'react'; |
|||
import RenderAuthorized from '@/components/Authorized'; |
|||
import { getAuthority } from '@/utils/authority'; |
|||
import Redirect from 'umi/redirect'; |
|||
import pathToRegexp from 'path-to-regexp'; |
|||
import { connect } from 'dva'; |
|||
import Authorized from '@/utils/Authorized'; |
|||
|
|||
const Authority = getAuthority(); |
|||
const Authorized = RenderAuthorized(Authority); |
|||
function AuthComponent({ children, location, routerData, status }) { |
|||
const isLogin = status === 'ok'; |
|||
|
|||
export default ({ children }) => ( |
|||
<Authorized authority={children.props.route.authority} noMatch={<Redirect to="/user/login" />}> |
|||
{children} |
|||
</Authorized> |
|||
); |
|||
const getRouteAuthority = (pathname, routeData) => { |
|||
const routes = routeData.slice(); // clone
|
|||
|
|||
const getAuthority = (routeDatas, path) => { |
|||
let authorities; |
|||
routeDatas.forEach(route => { |
|||
// check partial route
|
|||
if (pathToRegexp(`${route.path}(.*)`).test(path)) { |
|||
if (route.authority) { |
|||
authorities = route.authority; |
|||
} |
|||
// is exact route?
|
|||
if (!pathToRegexp(route.path).test(path) && route.routes) { |
|||
authorities = getAuthority(route.routes, path); |
|||
} |
|||
} |
|||
}); |
|||
return authorities; |
|||
}; |
|||
|
|||
return getAuthority(routes, pathname); |
|||
}; |
|||
return ( |
|||
<Authorized |
|||
authority={getRouteAuthority(location.pathname, routerData)} |
|||
noMatch={isLogin ? <Redirect to="/exception/403" /> : <Redirect to="/user/login" />} |
|||
> |
|||
{children} |
|||
</Authorized> |
|||
); |
|||
} |
|||
export default connect(({ menu: menuModel, login: loginModel }) => ({ |
|||
routerData: menuModel.routerData, |
|||
status: loginModel.status, |
|||
}))(AuthComponent); |
|||
|
|||
Loading…
Reference in new issue