Przeglądaj źródła

修复2.0权限问题

Tan90Qian 7 lat temu
rodzic
commit
cbf052c6db
2 zmienionych plików z 8 dodań i 7 usunięć
  1. 7 6
      src/pages/Authorized.js
  2. 1 1
      src/utils/authority.js

+ 7 - 6
src/pages/Authorized.js

@@ -1,21 +1,22 @@
 import React from 'react';
 import React from 'react';
 import RenderAuthorized from '@/components/Authorized';
 import RenderAuthorized from '@/components/Authorized';
 import Exception from '@/components/Exception';
 import Exception from '@/components/Exception';
+import { getAuthority } from '@/utils/authority';
 import { matchRoutes } from 'react-router-config';
 import { matchRoutes } from 'react-router-config';
-import uniq from 'lodash/uniq';
+import intersection from 'lodash/intersection';
 import { formatMessage } from 'umi/locale';
 import { formatMessage } from 'umi/locale';
 import Link from 'umi/link';
 import Link from 'umi/link';
 
 
-const Authorized = RenderAuthorized(['admin', 'user']);
+const Authorized = RenderAuthorized(getAuthority());
 
 
 export default ({ children, route, location }) => {
 export default ({ children, route, location }) => {
   const routes = matchRoutes(route.routes, location.pathname);
   const routes = matchRoutes(route.routes, location.pathname);
   let authorities = [];
   let authorities = [];
   routes.forEach(item => {
   routes.forEach(item => {
-    if (Array.isArray(item.route.authority)) {
-      authorities = authorities.concat(item.route.authority);
-    } else if (typeof item.route.authority === 'string') {
+    if (Array.isArray(item.route.authority) && item.route.authority.length) {
       authorities.push(item.route.authority);
       authorities.push(item.route.authority);
+    } else if (typeof item.route.authority === 'string' && item.route.authority) {
+      authorities.push([item.route.authority]);
     }
     }
   });
   });
   const noMatch = (
   const noMatch = (
@@ -28,7 +29,7 @@ export default ({ children, route, location }) => {
   );
   );
   return (
   return (
     <Authorized
     <Authorized
-      authority={authorities.length === 0 ? undefined : uniq(authorities)}
+      authority={authorities.length === 0 ? undefined : intersection(...authorities)}
       noMatch={noMatch}
       noMatch={noMatch}
     >
     >
       {children}
       {children}

+ 1 - 1
src/utils/authority.js

@@ -6,7 +6,7 @@ export function getAuthority() {
     if (authority.includes('[')) {
     if (authority.includes('[')) {
       authority = JSON.parse(authority);
       authority = JSON.parse(authority);
     } else {
     } else {
-      authority = [authority];
+      authority = [JSON.parse(authority)];
     }
     }
   } else {
   } else {
     authority = ['admin'];
     authority = ['admin'];