张延森 hace 4 años
padre
commit
e215ed6187

+ 13
- 0
config/routes.js Ver fichero

@@ -45,6 +45,19 @@ export default [
45 45
               },
46 46
             ],
47 47
           },
48
+          {
49
+            path: '/building',
50
+            name: '项目管理',
51
+            icon: 'control',
52
+            component: '../layouts/BlankLayout',
53
+            routes: [
54
+              {
55
+                path: '/building/list',
56
+                name: '项目列表',
57
+                component: './building/List',
58
+              },
59
+            ],
60
+          },
48 61
           {
49 62
             component: './404',
50 63
           },

+ 30
- 12
src/layouts/BasicLayout.jsx Ver fichero

@@ -4,7 +4,7 @@
4 4
  * https://github.com/ant-design/ant-design-pro-layout
5 5
  */
6 6
 import ProLayout, { DefaultFooter } from '@ant-design/pro-layout';
7
-import React, { useEffect } from 'react';
7
+import React, { useEffect, useMemo } from 'react';
8 8
 import { Link } from 'umi';
9 9
 import { connect } from 'dva';
10 10
 import { Icon, Result, Button } from 'antd';
@@ -26,13 +26,16 @@ const noMatch = (
26 26
   />
27 27
 );
28 28
 
29
-/**
30
- * use Authorized check all menu item
31
- */
32
-const menuDataRender = (menuList) =>
29
+/** Use Authorized check all menu item */
30
+const menuDataRender = (menuRoles) => (menuList) =>
33 31
   menuList.map((item) => {
34
-    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
35
-    return Authorized.check(item.authority, localItem, null);
32
+    const needAuth = !!item.menuCode;
33
+
34
+    const authority =
35
+      (menuRoles.filter((x) => x.menuCode === item.menuCode)[0] || {}).roles || [];
36
+
37
+    const localItem = { ...item, children: item.children ? menuDataRender(menuRoles)(item.children) : [] };
38
+    return Authorized.check(needAuth ? authority : undefined, localItem, null);
36 39
   });
37 40
 
38 41
 const footerRender = () => {
@@ -55,6 +58,8 @@ const footerRender = () => {
55 58
 
56 59
 const BasicLayout = (props) => {
57 60
   const {
61
+    // user,
62
+    menus,
58 63
     dispatch,
59 64
     children,
60 65
     settings,
@@ -72,9 +77,20 @@ const BasicLayout = (props) => {
72 77
     }
73 78
   }; // get children authority
74 79
 
75
-  const authorized = getAuthorityFromRouter(props.route.routes, location.pathname || '/') || {
76
-    authority: undefined,
77
-  };
80
+  const authorized = useMemo(() => {
81
+    const routeItem = getAuthorityFromRouter(props.route.routes, location.pathname || '/')
82
+    if (!routeItem || !routeItem.menuCode) {
83
+      return {
84
+        authority: undefined,
85
+      };
86
+    }
87
+
88
+    const authority = (menus.filter((x) => x.menuCode === routeItem.menuCode)[0] || {}).roles;
89
+    return {
90
+      authority: authority && authority.length ? authority : ['any-string-for-no-right'],
91
+    };
92
+  })
93
+  
78 94
 
79 95
   return (
80 96
     <ProLayout
@@ -112,7 +128,7 @@ const BasicLayout = (props) => {
112 128
         );
113 129
       }}
114 130
       footerRender={footerRender}
115
-      menuDataRender={menuDataRender}
131
+      menuDataRender={menuDataRender(menus)}
116 132
       formatMessage={formatMessage}
117 133
       rightContentRender={() => <RightContent />}
118 134
       {...props}
@@ -125,7 +141,9 @@ const BasicLayout = (props) => {
125 141
   );
126 142
 };
127 143
 
128
-export default connect(({ global, settings }) => ({
144
+export default connect(({ global, settings, user }) => ({
129 145
   collapsed: global.collapsed,
130 146
   settings,
147
+  user: user.currentUser,
148
+  menus: user.menuList,
131 149
 }))(BasicLayout);

+ 3
- 0
src/models/user.js Ver fichero

@@ -1,4 +1,5 @@
1 1
 import { fetch, apis } from '@/utils/request';
2
+import { setAuthority } from '@/utils/authority'
2 3
 // import { setAllBtnAuth, setUserBtnAuth } from '@/components/AuthButton';
3 4
 
4 5
 const getCurrentUser = fetch(apis.user.current)
@@ -28,6 +29,8 @@ const UserModel = {
28 29
       // setAllBtnAuth(buttonList)
29 30
       // setUserBtnAuth(currentUser.buttons)
30 31
 
32
+      setAuthority(currentUser.roles)
33
+
31 34
       return { ...state, currentUser, menuList, buttonList };
32 35
     },
33 36
     changeNotifyCount(

+ 4
- 4
src/pages/Welcome.jsx Ver fichero

@@ -13,10 +13,10 @@ const CodePreview = ({ children }) => (
13 13
 );
14 14
 
15 15
 export default (props) => {
16
-  console.log('--------welcome-----', JSON.stringify(props))
17
-  useEffect(() => {
18
-    console.log('------useEffect-------')
19
-  }, [])
16
+  // console.log('--------welcome-----', JSON.stringify(props))
17
+  // useEffect(() => {
18
+  //   console.log('------useEffect-------')
19
+  // }, [])
20 20
   
21 21
   return (
22 22
     <PageHeaderWrapper>

+ 7
- 0
src/pages/building/List/index.jsx Ver fichero

@@ -0,0 +1,7 @@
1
+import React from 'react'
2
+
3
+export default (props) => {
4
+  return (
5
+    <div></div>
6
+  )
7
+}

+ 6
- 27
src/utils/authority.js Ver fichero

@@ -1,33 +1,12 @@
1 1
 import { reloadAuthorized } from './Authorized'; // use localStorage to store the authority info, which might be sent from server in actual project.
2 2
 
3
-export function getAuthority(str) {
4
-  const authorityString =
5
-    typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-authority') : str; // authorityString could be admin, "admin", ["admin"]
3
+let roles = []
6 4
 
7
-  let authority;
8
-
9
-  try {
10
-    if (authorityString) {
11
-      authority = JSON.parse(authorityString);
12
-    }
13
-  } catch (e) {
14
-    authority = authorityString;
15
-  }
16
-
17
-  if (typeof authority === 'string') {
18
-    return [authority];
19
-  } // preview.pro.ant.design only do not use in your production.
20
-  // preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
21
-
22
-  if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
23
-    return ['admin'];
24
-  }
25
-
26
-  return authority;
5
+export function getAuthority() {
6
+  return roles || [];
27 7
 }
28
-export function setAuthority(authority) {
29
-  const proAuthority = typeof authority === 'string' ? [authority] : authority;
30
-  localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority)); // auto reload
31
-
8
+export function setAuthority(authority = []) {
9
+  roles = typeof authority === 'string' ? [authority] : authority;
10
+  
32 11
   reloadAuthorized();
33 12
 }