张延森 3 years ago
parent
commit
47dc3ac851
4 changed files with 15 additions and 8 deletions
  1. 1
    0
      config/config.dev.js
  2. 2
    0
      config/config.js
  3. 8
    4
      src/access.js
  4. 4
    4
      src/app.jsx

+ 1
- 0
config/config.dev.js View File

1
 // https://umijs.org/config/
1
 // https://umijs.org/config/
2
 import { defineConfig } from 'umi';
2
 import { defineConfig } from 'umi';
3
 export default defineConfig({
3
 export default defineConfig({
4
+  //
4
   plugins: [
5
   plugins: [
5
     // https://github.com/zthxxx/react-dev-inspector
6
     // https://github.com/zthxxx/react-dev-inspector
6
     'react-dev-inspector/plugins/umi/react-inspector',
7
     'react-dev-inspector/plugins/umi/react-inspector',

+ 2
- 0
config/config.js View File

5
 import proxy from './proxy';
5
 import proxy from './proxy';
6
 import routes from './routes';
6
 import routes from './routes';
7
 const { REACT_APP_ENV } = process.env;
7
 const { REACT_APP_ENV } = process.env;
8
+
8
 export default defineConfig({
9
 export default defineConfig({
10
+  // 以下是 umi 参数
9
   hash: true,
11
   hash: true,
10
   antd: {},
12
   antd: {},
11
   dva: {
13
   dva: {

+ 8
- 4
src/access.js View File

2
  * @see https://umijs.org/zh-CN/plugins/plugin-access
2
  * @see https://umijs.org/zh-CN/plugins/plugin-access
3
  * */
3
  * */
4
 export default function access(initialState) {
4
 export default function access(initialState) {
5
-  const { currentUser } = initialState || {};
6
-  return {
7
-    canAdmin: currentUser && currentUser.access === 'admin',
8
-  };
5
+  const { menuAccess } = initialState || {};
6
+  return (menuAccess || []).reduce((acc, item) => {
7
+    const { permission: canAccess } = item;
8
+    return {
9
+      ...acc,
10
+      [permission]: canAccess,
11
+    };
12
+  }, {});
9
 }
13
 }

+ 4
- 4
src/app.jsx View File

18
 export async function getInitialState() {
18
 export async function getInitialState() {
19
   const fetchUserInfo = async () => {
19
   const fetchUserInfo = async () => {
20
     try {
20
     try {
21
-      const { user } = await queryCurrentUser();
22
-      return user;
21
+      return await queryCurrentUser();
23
     } catch (error) {
22
     } catch (error) {
24
       history.push(loginPath);
23
       history.push(loginPath);
25
     }
24
     }
28
   }; // 如果是登录页面,不执行
27
   }; // 如果是登录页面,不执行
29
 
28
 
30
   if (history.location.pathname !== loginPath) {
29
   if (history.location.pathname !== loginPath) {
31
-    const currentUser = await fetchUserInfo();
30
+    const res = await fetchUserInfo();
32
     return {
31
     return {
33
       fetchUserInfo,
32
       fetchUserInfo,
34
-      currentUser,
33
+      currentUser: res.user,
34
+      menuAccess: res.menu,
35
       settings: {},
35
       settings: {},
36
     };
36
     };
37
   }
37
   }