Authorized.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React from 'react';
  2. import Redirect from 'umi/redirect';
  3. import pathToRegexp from 'path-to-regexp';
  4. import { connect } from 'dva';
  5. import Authorized from '@/utils/Authorized';
  6. const getRouteAuthority = (path, routeData) => {
  7. let authorities;
  8. routeData.forEach(route => {
  9. // if (route.authority) {
  10. // console.log(1);
  11. // authorities = route.authority;
  12. // } // match prefix
  13. if (pathToRegexp(`${route.path}(.*)`).test(path)) {
  14. // exact match
  15. if (route.path === path) {
  16. authorities = route.authority || authorities;
  17. } // get children authority recursively
  18. if (route.routes) {
  19. authorities = getRouteAuthority(path, route.routes) || authorities;
  20. }
  21. }
  22. });
  23. return authorities;
  24. };
  25. const AuthComponent = ({ children, routerData, location, status }) => {
  26. const isLogin = status === 'ok';
  27. return (
  28. <Authorized
  29. authority={getRouteAuthority(location.pathname, routerData)}
  30. // noMatch={<Redirect to="/404" />}
  31. >
  32. {children}
  33. </Authorized>
  34. );
  35. };
  36. export default connect(({ menu: menuModel, login: loginModel }) => ({
  37. routerData: menuModel.routerData,
  38. status: loginModel.status,
  39. }))(AuthComponent);