Browse Source

修改权限提示

魏熙美 5 years ago
parent
commit
1adf342e10
3 changed files with 46 additions and 45 deletions
  1. 3
    2
      config/config.js
  2. 0
    5
      package.json
  3. 43
    38
      src/utils/request.js

+ 3
- 2
config/config.js View File

@@ -81,6 +81,8 @@ export default {
81 81
   targets: {
82 82
     ie: 11,
83 83
   },
84
+  publicPath: './',
85
+  history: 'hash',
84 86
   devtool: isAntDesignProPreview ? 'source-map' : false,
85 87
   // umi routes: https://umijs.org/zh/guide/router.html
86 88
   routes,
@@ -133,6 +135,5 @@ export default {
133 135
       changeOrigin: true,
134 136
       // pathRewrite: { '^/server': '' },
135 137
     },
136
-  }
137
-  
138
+  },
138 139
 };

+ 0
- 5
package.json View File

@@ -26,11 +26,6 @@
26 26
     "test:component": "umi test ./src/components",
27 27
     "ui": "umi ui"
28 28
   },
29
-  "husky": {
30
-    "hooks": {
31
-      "pre-commit": "npm run lint-staged"
32
-    }
33
-  },
34 29
   "lint-staged": {
35 30
     "**/*.less": "stylelint --syntax less",
36 31
     "**/*.{js,jsx,tsx,ts,less,md,json}": [

+ 43
- 38
src/utils/request.js View File

@@ -26,45 +26,43 @@ const codeMessage = {
26 26
 };
27 27
 
28 28
 const replaceURLParams = (url, params = {}) => {
29
-  return Object.keys(params).reduce((acc, k) => { // 此方法对每个元素进行处理
30
-    const re = new RegExp(`:${k}(?!w)`, 'i')
31
-    return acc.replace(re, params[k])
32
-  }, url)
33
-}
29
+  return Object.keys(params).reduce((acc, k) => {
30
+    // 此方法对每个元素进行处理
31
+    const re = new RegExp(`:${k}(?!w)`, 'i');
32
+    return acc.replace(re, params[k]);
33
+  }, url);
34
+};
34 35
 
35 36
 request.interceptors.request.use((url, options) => {
36
-  const { urlData, headers = {}, logout = false, login = false, action, data, ...opts } = options
37
-  const apiURL = urlData ? replaceURLParams(url, urlData) : url
38
-  const token = mixStr(window.localStorage.getItem('test-foobar'))
37
+  const { urlData, headers = {}, logout = false, login = false, action, data, ...opts } = options;
38
+  const apiURL = urlData ? replaceURLParams(url, urlData) : url;
39
+  const token = mixStr(window.localStorage.getItem('test-foobar'));
39 40
 
40 41
   if (login || logout) {
41
-    window.localStorage.removeItem('test-foobar')
42
+    window.localStorage.removeItem('test-foobar');
42 43
   }
43 44
 
44
-  const authHeader = !login ? { Authorization: `Bearer ${token}` } : {}
45
-  const actionHeader = action ? { 'X-ACTION': action  }: {}
45
+  const authHeader = !login ? { Authorization: `Bearer ${token}` } : {};
46
+  const actionHeader = action ? { 'X-ACTION': action } : {};
46 47
 
47
-  return (
48
-    {
49
-      url: apiURL,
50
-      options: {
51
-        ...opts,
52
-        headers: {
53
-          ...authHeader,
54
-          ...actionHeader,
55
-          ...headers,
56
-        },
57
-        data,
58
-        requestType: data instanceof FormData ? 'form' : 'json',
59
-        credentials: 'include', // 带 cookie
60
-        interceptors: true
48
+  return {
49
+    url: apiURL,
50
+    options: {
51
+      ...opts,
52
+      headers: {
53
+        ...authHeader,
54
+        ...actionHeader,
55
+        ...headers,
61 56
       },
62
-    }
63
-  );
57
+      data,
58
+      requestType: data instanceof FormData ? 'form' : 'json',
59
+      credentials: 'include', // 带 cookie
60
+      interceptors: true,
61
+    },
62
+  };
64 63
 });
65 64
 
66 65
 request.interceptors.response.use(async (response, options) => {
67
-  
68 66
   if (response && response.status) {
69 67
     if (response.status != 200) {
70 68
       const errorText = codeMessage[response.status] || response.statusText;
@@ -77,15 +75,23 @@ request.interceptors.response.use(async (response, options) => {
77 75
     } else {
78 76
       const { code, data, message } = await response.clone().json();
79 77
       if (code != 1000) {
80
-        notification.error({
81
-          message: `请求错误`,
82
-          description: message,
83
-        });
84
-        throw new Error(message);
78
+        if (code === 1001) {
79
+          notification.error({
80
+            message: `请求错误`,
81
+            description: '请登录系统',
82
+          });
83
+          throw new Error('请登录系统');
84
+        } else {
85
+          notification.error({
86
+            message: `请求错误`,
87
+            description: message,
88
+          });
89
+          throw new Error(message);
90
+        }
85 91
       }
86 92
 
87 93
       if (data && data.token) {
88
-        window.localStorage.setItem('test-foobar', mixStr(data.token))
94
+        window.localStorage.setItem('test-foobar', mixStr(data.token));
89 95
       }
90 96
 
91 97
       return data;
@@ -98,16 +104,15 @@ request.interceptors.response.use(async (response, options) => {
98 104
   }
99 105
 });
100 106
 
101
-
102
-const fetch = api => options => request(api.url, {...api, ...options || {}})
107
+const fetch = api => options => request(api.url, { ...api, ...(options || {}) });
103 108
 
104 109
 export default config => {
105 110
   if (typeof config === 'string') {
106 111
     return request(config);
107 112
   } else {
108
-    const {url, ...options} = config;
113
+    const { url, ...options } = config;
109 114
     return request(url, options);
110 115
   }
111 116
 };
112 117
 
113
-export { fetch, apis }
118
+export { fetch, apis };