|
@@ -1,13 +1,29 @@
|
1
|
|
-
|
2
|
1
|
import { request } from 'umi';
|
3
|
2
|
import { downloadBlob } from './download';
|
4
|
3
|
|
5
|
|
-function requestInterceptor (url, options) {
|
|
4
|
+/**
|
|
5
|
+ *
|
|
6
|
+ * 自定义修改 umi-request 行为
|
|
7
|
+ *
|
|
8
|
+ * 默认错误处理行为是通过 message 组件报错
|
|
9
|
+ * 也可以设置 option.showType 来修改默认行为
|
|
10
|
+ *
|
|
11
|
+ * export enum ErrorShowType {
|
|
12
|
+ * SILENT = 0, // 不提示错误
|
|
13
|
+ * WARN_MESSAGE = 1, // 警告信息提示
|
|
14
|
+ * ERROR_MESSAGE = 2, // 错误信息提示
|
|
15
|
+ * NOTIFICATION = 4, // 通知提示
|
|
16
|
+ * REDIRECT = 9, // 页面跳转,会跳转到 /exception 页面
|
|
17
|
+ * }
|
|
18
|
+ *
|
|
19
|
+ */
|
|
20
|
+
|
|
21
|
+function requestInterceptor(url, options) {
|
6
|
22
|
const headers = options.headers || {};
|
7
|
23
|
const token = localStorage.getItem('token')
|
8
|
|
- ? { 'X-Authorization-JWT': localStorage.getItem('token') }
|
9
|
|
- : // ? { 'Authorization': localStorage.getItem('token') }
|
10
|
|
- {};
|
|
24
|
+ ? { Authorization: localStorage.getItem('token') }
|
|
25
|
+ : {};
|
|
26
|
+
|
11
|
27
|
const prefix =
|
12
|
28
|
process.env.NODE_ENV === 'production' ? 'http://sgl-v2.njyunzhi.com/api/admin' : '/api/admin';
|
13
|
29
|
|
|
@@ -23,18 +39,14 @@ function requestInterceptor (url, options) {
|
23
|
39
|
};
|
24
|
40
|
}
|
25
|
41
|
|
26
|
|
-async function responseInterceptor (response) {
|
|
42
|
+async function responseInterceptor(response, ctx) {
|
|
43
|
+ console.log('-----response--------->', response, ctx);
|
27
|
44
|
const contextType = response.headers.get('content-type');
|
28
|
45
|
if (contextType.indexOf('json') > -1) {
|
29
|
46
|
const result = await response.clone().json();
|
30
|
47
|
if (result?.token || result?.data?.token) {
|
31
|
48
|
localStorage.setItem('token', result?.token || result?.data?.token);
|
32
|
49
|
}
|
33
|
|
-
|
34
|
|
- if (result.code === 1000) {
|
35
|
|
- return result;
|
36
|
|
- }
|
37
|
|
- return Promise.reject(result);
|
38
|
50
|
}
|
39
|
51
|
|
40
|
52
|
if (contextType.indexOf('application/vnd.ms-excel') > -1) {
|
|
@@ -49,13 +61,16 @@ async function responseInterceptor (response) {
|
49
|
61
|
|
50
|
62
|
// https://umijs.org/plugins/plugin-request
|
51
|
63
|
export const requestConfig = {
|
|
64
|
+ abc: 1,
|
52
|
65
|
errorConfig: {
|
53
|
|
- adaptor: (resData) => {
|
|
66
|
+ adaptor: (resData, ctx) => {
|
|
67
|
+ const showError = ctx.req.options?.showType;
|
|
68
|
+
|
54
|
69
|
return {
|
55
|
70
|
...resData,
|
56
|
71
|
success: resData.code === 1000,
|
57
|
72
|
errorMessage: resData.message,
|
58
|
|
- showType: 0, // 静默处理错误
|
|
73
|
+ showType: isNaN(showError) ? 1 : showError, // 默认通过 message 组件报错
|
59
|
74
|
};
|
60
|
75
|
},
|
61
|
76
|
},
|
|
@@ -65,7 +80,7 @@ export const requestConfig = {
|
65
|
80
|
|
66
|
81
|
export default (...args) => request(...args).then((r) => r.data);
|
67
|
82
|
|
68
|
|
-export function queryTable (apiRequest) {
|
|
83
|
+export function queryTable(apiRequest) {
|
69
|
84
|
return function (params) {
|
70
|
85
|
return apiRequest({
|
71
|
86
|
...params,
|