张延森 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,6 +1,7 @@
1 1
 // https://umijs.org/config/
2 2
 import { defineConfig } from 'umi';
3 3
 export default defineConfig({
4
+  //
4 5
   plugins: [
5 6
     // https://github.com/zthxxx/react-dev-inspector
6 7
     'react-dev-inspector/plugins/umi/react-inspector',

+ 2
- 0
config/config.js View File

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

+ 8
- 4
src/access.js View File

@@ -2,8 +2,12 @@
2 2
  * @see https://umijs.org/zh-CN/plugins/plugin-access
3 3
  * */
4 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,8 +18,7 @@ export const initialStateConfig = {
18 18
 export async function getInitialState() {
19 19
   const fetchUserInfo = async () => {
20 20
     try {
21
-      const { user } = await queryCurrentUser();
22
-      return user;
21
+      return await queryCurrentUser();
23 22
     } catch (error) {
24 23
       history.push(loginPath);
25 24
     }
@@ -28,10 +27,11 @@ export async function getInitialState() {
28 27
   }; // 如果是登录页面,不执行
29 28
 
30 29
   if (history.location.pathname !== loginPath) {
31
-    const currentUser = await fetchUserInfo();
30
+    const res = await fetchUserInfo();
32 31
     return {
33 32
       fetchUserInfo,
34
-      currentUser,
33
+      currentUser: res.user,
34
+      menuAccess: res.menu,
35 35
       settings: {},
36 36
     };
37 37
   }