张延森 4 年 前
コミット
e215ed6187
共有6 個のファイルを変更した63 個の追加43 個の削除を含む
  1. 13
    0
      config/routes.js
  2. 30
    12
      src/layouts/BasicLayout.jsx
  3. 3
    0
      src/models/user.js
  4. 4
    4
      src/pages/Welcome.jsx
  5. 7
    0
      src/pages/building/List/index.jsx
  6. 6
    27
      src/utils/authority.js

+ 13
- 0
config/routes.js ファイルの表示

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
             component: './404',
62
             component: './404',
50
           },
63
           },

+ 30
- 12
src/layouts/BasicLayout.jsx ファイルの表示

4
  * https://github.com/ant-design/ant-design-pro-layout
4
  * https://github.com/ant-design/ant-design-pro-layout
5
  */
5
  */
6
 import ProLayout, { DefaultFooter } from '@ant-design/pro-layout';
6
 import ProLayout, { DefaultFooter } from '@ant-design/pro-layout';
7
-import React, { useEffect } from 'react';
7
+import React, { useEffect, useMemo } from 'react';
8
 import { Link } from 'umi';
8
 import { Link } from 'umi';
9
 import { connect } from 'dva';
9
 import { connect } from 'dva';
10
 import { Icon, Result, Button } from 'antd';
10
 import { Icon, Result, Button } from 'antd';
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
   menuList.map((item) => {
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
 const footerRender = () => {
41
 const footerRender = () => {
55
 
58
 
56
 const BasicLayout = (props) => {
59
 const BasicLayout = (props) => {
57
   const {
60
   const {
61
+    // user,
62
+    menus,
58
     dispatch,
63
     dispatch,
59
     children,
64
     children,
60
     settings,
65
     settings,
72
     }
77
     }
73
   }; // get children authority
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
   return (
95
   return (
80
     <ProLayout
96
     <ProLayout
112
         );
128
         );
113
       }}
129
       }}
114
       footerRender={footerRender}
130
       footerRender={footerRender}
115
-      menuDataRender={menuDataRender}
131
+      menuDataRender={menuDataRender(menus)}
116
       formatMessage={formatMessage}
132
       formatMessage={formatMessage}
117
       rightContentRender={() => <RightContent />}
133
       rightContentRender={() => <RightContent />}
118
       {...props}
134
       {...props}
125
   );
141
   );
126
 };
142
 };
127
 
143
 
128
-export default connect(({ global, settings }) => ({
144
+export default connect(({ global, settings, user }) => ({
129
   collapsed: global.collapsed,
145
   collapsed: global.collapsed,
130
   settings,
146
   settings,
147
+  user: user.currentUser,
148
+  menus: user.menuList,
131
 }))(BasicLayout);
149
 }))(BasicLayout);

+ 3
- 0
src/models/user.js ファイルの表示

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

+ 4
- 4
src/pages/Welcome.jsx ファイルの表示

13
 );
13
 );
14
 
14
 
15
 export default (props) => {
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
   return (
21
   return (
22
     <PageHeaderWrapper>
22
     <PageHeaderWrapper>

+ 7
- 0
src/pages/building/List/index.jsx ファイルの表示

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

+ 6
- 27
src/utils/authority.js ファイルの表示

1
 import { reloadAuthorized } from './Authorized'; // use localStorage to store the authority info, which might be sent from server in actual project.
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
   reloadAuthorized();
11
   reloadAuthorized();
33
 }
12
 }