-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Umi Permission Routing
- Loading branch information
Showing
4 changed files
with
43 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
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); |
如果用户刷新页面后,login模型的status属性并不能判断出用户的登录状态