Bladeren bron

修改权限提示

魏熙美 5 jaren geleden
bovenliggende
commit
1adf342e10
3 gewijzigde bestanden met toevoegingen van 46 en 45 verwijderingen
  1. 3
    2
      config/config.js
  2. 0
    5
      package.json
  3. 43
    38
      src/utils/request.js

+ 3
- 2
config/config.js Bestand weergeven

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

+ 0
- 5
package.json Bestand weergeven

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

+ 43
- 38
src/utils/request.js Bestand weergeven

26
 };
26
 };
27
 
27
 
28
 const replaceURLParams = (url, params = {}) => {
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
 request.interceptors.request.use((url, options) => {
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
   if (login || logout) {
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
 request.interceptors.response.use(async (response, options) => {
65
 request.interceptors.response.use(async (response, options) => {
67
-  
68
   if (response && response.status) {
66
   if (response && response.status) {
69
     if (response.status != 200) {
67
     if (response.status != 200) {
70
       const errorText = codeMessage[response.status] || response.statusText;
68
       const errorText = codeMessage[response.status] || response.statusText;
77
     } else {
75
     } else {
78
       const { code, data, message } = await response.clone().json();
76
       const { code, data, message } = await response.clone().json();
79
       if (code != 1000) {
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
       if (data && data.token) {
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
       return data;
97
       return data;
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
 export default config => {
109
 export default config => {
105
   if (typeof config === 'string') {
110
   if (typeof config === 'string') {
106
     return request(config);
111
     return request(config);
107
   } else {
112
   } else {
108
-    const {url, ...options} = config;
113
+    const { url, ...options } = config;
109
     return request(url, options);
114
     return request(url, options);
110
   }
115
   }
111
 };
116
 };
112
 
117
 
113
-export { fetch, apis }
118
+export { fetch, apis };