|
@@ -1,113 +1,115 @@
|
1
|
|
-import { request } from 'umi';
|
2
|
|
-import { downloadBlob } from './download';
|
3
|
|
-
|
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) {
|
22
|
|
- const headers = options.headers || {};
|
23
|
|
- const token = localStorage.getItem('token')
|
24
|
|
- ? { Authorization: localStorage.getItem('token') }
|
25
|
|
- : {};
|
26
|
|
-
|
27
|
|
- console.log('------------->', API_BASE)
|
28
|
|
-
|
29
|
|
- const prefix = `${API_BASE}/api/admin`;
|
30
|
|
- let requestType='json'
|
31
|
|
- if (options.data instanceof FormData) {
|
32
|
|
- requestType='form'
|
33
|
|
- }
|
34
|
|
- return {
|
35
|
|
- url: `${prefix}${url}`,
|
36
|
|
- options: {
|
37
|
|
- ...options,
|
38
|
|
- requestType,
|
39
|
|
- headers: {
|
40
|
|
- ...headers,
|
41
|
|
- ...token,
|
42
|
|
- },
|
43
|
|
- },
|
44
|
|
- };
|
45
|
|
-}
|
46
|
|
-
|
47
|
|
-async function responseInterceptor(response, ctx) {
|
48
|
|
- const contextType = response.headers.get('content-type');
|
49
|
|
- if (contextType.indexOf('json') > -1) {
|
50
|
|
- const result = await response.clone().json();
|
51
|
|
- if (result?.token || result?.data?.token) {
|
52
|
|
- localStorage.setItem('token', result?.token || result?.data?.token);
|
53
|
|
- }
|
54
|
|
- }
|
55
|
|
-
|
56
|
|
- if (contextType.indexOf('application/vnd.ms-excel') > -1) {
|
57
|
|
- const data = await response.clone().blob();
|
58
|
|
- console.info(response)
|
59
|
|
- const content = response.headers.get('content-disposition');
|
60
|
|
- const fileName = content.replace('attachment;filename=', '');
|
61
|
|
- downloadBlob(data, decodeURIComponent(fileName));
|
62
|
|
- }
|
63
|
|
-
|
64
|
|
- return response;
|
65
|
|
-}
|
66
|
|
-
|
67
|
|
-// https://umijs.org/plugins/plugin-request
|
68
|
|
-export const requestConfig = {
|
69
|
|
- errorConfig: {
|
70
|
|
- adaptor: (resData, ctx) => {
|
71
|
|
- if (typeof resData === 'string') {
|
72
|
|
- // 可能是 excel 导出
|
73
|
|
- return {
|
74
|
|
- success: true,
|
75
|
|
- };
|
76
|
|
- }
|
77
|
|
-
|
78
|
|
- const showError = ctx.req.options?.showType;
|
79
|
|
-
|
80
|
|
- return {
|
81
|
|
- ...resData,
|
82
|
|
- success: resData.code === 1000,
|
83
|
|
- errorMessage: resData.message,
|
84
|
|
- showType: isNaN(showError) ? 1 : showError, // 默认通过 message 组件报错
|
85
|
|
- };
|
86
|
|
- },
|
87
|
|
- },
|
88
|
|
- requestInterceptors: [requestInterceptor],
|
89
|
|
- responseInterceptors: [responseInterceptor],
|
90
|
|
-};
|
91
|
|
-
|
92
|
|
-export default (...args) => request(...args).then((r) => r.data);
|
93
|
|
-
|
94
|
|
-export function queryTable(apiRequest) {
|
95
|
|
- return function (params) {
|
96
|
|
- return apiRequest({
|
97
|
|
- ...params,
|
98
|
|
- pageNum: params.current,
|
99
|
|
- })
|
100
|
|
- .then((res) => {
|
101
|
|
- return {
|
102
|
|
- data: res.records,
|
103
|
|
- success: true,
|
104
|
|
- total: res.total,
|
105
|
|
- };
|
106
|
|
- })
|
107
|
|
- .catch((err) => {
|
108
|
|
- return {
|
109
|
|
- success: false,
|
110
|
|
- };
|
111
|
|
- });
|
112
|
|
- };
|
113
|
|
-}
|
|
1
|
+import { request } from 'umi';
|
|
2
|
+import { downloadBlob } from './download';
|
|
3
|
+
|
|
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) {
|
|
22
|
+ const headers = options.headers || {};
|
|
23
|
+ const token = localStorage.getItem('token')
|
|
24
|
+ ? { Authorization: localStorage.getItem('token') }
|
|
25
|
+ : {};
|
|
26
|
+
|
|
27
|
+ console.log('------------->', API_BASE)
|
|
28
|
+
|
|
29
|
+ const prefix = `${API_BASE}/api/admin`;
|
|
30
|
+ let requestType = 'json'
|
|
31
|
+ if (options.data instanceof FormData) {
|
|
32
|
+ requestType = 'form'
|
|
33
|
+ }
|
|
34
|
+ return {
|
|
35
|
+ url: `${prefix}${url}`,
|
|
36
|
+ options: {
|
|
37
|
+ ...options,
|
|
38
|
+ requestType,
|
|
39
|
+ headers: {
|
|
40
|
+ ...headers,
|
|
41
|
+ ...token,
|
|
42
|
+ },
|
|
43
|
+ },
|
|
44
|
+ };
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+async function responseInterceptor (response, ctx) {
|
|
48
|
+ const contextType = response.headers.get('content-type');
|
|
49
|
+ if (contextType.indexOf('json') > -1) {
|
|
50
|
+ const result = await response.clone().json();
|
|
51
|
+ if (result?.token || result?.data?.token) {
|
|
52
|
+ localStorage.setItem('token', result?.token || result?.data?.token);
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ if (contextType.indexOf('application/vnd.ms-excel') > -1) {
|
|
57
|
+ const data = await response.clone().blob();
|
|
58
|
+ console.info(response)
|
|
59
|
+ const content = response.headers.get('content-disposition');
|
|
60
|
+ const fileName = content.replace('attachment;filename=', '');
|
|
61
|
+ downloadBlob(data, decodeURIComponent(fileName));
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ return response;
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+// https://umijs.org/plugins/plugin-request
|
|
68
|
+export const requestConfig = {
|
|
69
|
+ errorConfig: {
|
|
70
|
+ adaptor: (resData, ctx) => {
|
|
71
|
+ if (typeof resData === 'string') {
|
|
72
|
+ // 可能是 excel 导出
|
|
73
|
+ return {
|
|
74
|
+ success: true,
|
|
75
|
+ };
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ const showError = ctx.req.options?.showType;
|
|
79
|
+
|
|
80
|
+ return {
|
|
81
|
+ ...resData,
|
|
82
|
+ success: resData.code === 1000,
|
|
83
|
+ errorMessage: resData.message,
|
|
84
|
+ showType: isNaN(showError) ? 1 : showError, // 默认通过 message 组件报错
|
|
85
|
+ };
|
|
86
|
+ },
|
|
87
|
+ },
|
|
88
|
+ requestInterceptors: [requestInterceptor],
|
|
89
|
+ responseInterceptors: [responseInterceptor],
|
|
90
|
+};
|
|
91
|
+
|
|
92
|
+export default (...args) => request(...args).then((r) => r.data);
|
|
93
|
+
|
|
94
|
+export function queryTable (apiRequest) {
|
|
95
|
+ return function (params) {
|
|
96
|
+ const { current, pageSize, ...leftParams } = params;
|
|
97
|
+ return apiRequest({
|
|
98
|
+ ...params,
|
|
99
|
+ pageNum: params.current,
|
|
100
|
+ ...(leftParams || {}),
|
|
101
|
+ })
|
|
102
|
+ .then((res) => {
|
|
103
|
+ return {
|
|
104
|
+ data: res.records,
|
|
105
|
+ success: true,
|
|
106
|
+ total: res.total,
|
|
107
|
+ };
|
|
108
|
+ })
|
|
109
|
+ .catch((err) => {
|
|
110
|
+ return {
|
|
111
|
+ success: false,
|
|
112
|
+ };
|
|
113
|
+ });
|
|
114
|
+ };
|
|
115
|
+}
|