12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import React from 'react';
- import Redirect from 'umi/redirect';
- import pathToRegexp from 'path-to-regexp';
- import { connect } from 'dva';
- import Authorized from '@/utils/Authorized';
- const getRouteAuthority = (path, routeData) => {
- let authorities;
- routeData.forEach(route => {
- // if (route.authority) {
- // console.log(1);
- // authorities = route.authority;
- // } // 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;
- }
- }
- });
- return authorities;
- };
- const AuthComponent = ({ children, routerData, location, status }) => {
- const isLogin = status === 'ok';
- return (
- <Authorized
- authority={getRouteAuthority(location.pathname, routerData)}
- // noMatch={<Redirect to="/404" />}
- >
- {children}
- </Authorized>
- );
- };
- export default connect(({ menu: menuModel, login: loginModel }) => ({
- routerData: menuModel.routerData,
- status: loginModel.status,
- }))(AuthComponent);
|