@hzab/data-model 1.8.9-alpha.0 → 2.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/ArrayUtils.ts +668 -0
- package/src/array-data-model.ts +583 -0
- package/src/axios.ts +210 -0
- package/src/{data-model.js → data-model.ts} +354 -106
- package/src/{hooks.js → hooks.ts} +15 -4
- package/src/{index.js → index.ts} +1 -0
- package/src/{public-utils.js → public-utils.ts} +12 -3
- package/src/type.ts +98 -0
- package/src/{utils.js → utils.ts} +25 -19
- package/src/ArrayUtils.js +0 -539
- package/src/array-data-model.js +0 -422
- package/src/axios.js +0 -162
|
@@ -1,10 +1,245 @@
|
|
|
1
1
|
import _ from "lodash";
|
|
2
|
-
import axios from "./axios";
|
|
2
|
+
import { axios, axiosDef } from "./axios";
|
|
3
|
+
import {
|
|
4
|
+
objToFormData,
|
|
5
|
+
formDataToObj,
|
|
6
|
+
_$Temp,
|
|
7
|
+
setDefaultAxios,
|
|
8
|
+
setDefaultErrMsg,
|
|
9
|
+
setNetworkErrMsg,
|
|
10
|
+
checkNetwork,
|
|
11
|
+
} from "./utils";
|
|
12
|
+
import {
|
|
13
|
+
JSONObject,
|
|
14
|
+
QueryParams,
|
|
15
|
+
RequestData,
|
|
16
|
+
ResponseData,
|
|
17
|
+
AxiosConfig,
|
|
18
|
+
MapFunction,
|
|
19
|
+
RequestMapFunction,
|
|
20
|
+
GetListResult,
|
|
21
|
+
GetListFunc,
|
|
22
|
+
HandleResponseFunc,
|
|
23
|
+
ApiError,
|
|
24
|
+
} from "./type";
|
|
3
25
|
|
|
4
|
-
|
|
26
|
+
/**
|
|
27
|
+
* DataModel 构造函数参数接口
|
|
28
|
+
*/
|
|
29
|
+
export interface DataModelOptions<T = any> {
|
|
30
|
+
/** 请求 url 替换的额外参数 */
|
|
31
|
+
ctx?: JSONObject;
|
|
32
|
+
/** GET 请求的参数 */
|
|
33
|
+
query?: QueryParams;
|
|
34
|
+
|
|
35
|
+
/** POST 接口地址 */
|
|
36
|
+
createApi?: string;
|
|
37
|
+
/** POST 接口提交前的数据处理回调 */
|
|
38
|
+
createMap?: MapFunction<T, T>;
|
|
39
|
+
/** GET 详情 接口地址 */
|
|
40
|
+
getApi?: string;
|
|
41
|
+
/** GET 详情 接口返回后的数据处理回调 */
|
|
42
|
+
getMap?: MapFunction<T, T>;
|
|
43
|
+
/** GET 列表 接口地址 */
|
|
44
|
+
getListApi?: string;
|
|
45
|
+
/** GET 列表 接口返回后的数据处理回调 */
|
|
46
|
+
getListMap?: MapFunction<T, T>;
|
|
47
|
+
/** GET 列表 接口回调,用于自定义列表请求接口 */
|
|
48
|
+
getListFunc?: GetListFunc<T>;
|
|
49
|
+
/** PUT 接口地址 */
|
|
50
|
+
updateApi?: string;
|
|
51
|
+
/** PUT 接口提交前的数据处理回调 */
|
|
52
|
+
updateMap?: MapFunction<T, T>;
|
|
53
|
+
/** PATCH 接口地址 */
|
|
54
|
+
patchApi?: string;
|
|
55
|
+
/** PATCH 接口提交前的数据处理回调 */
|
|
56
|
+
patchMap?: MapFunction<T, T>;
|
|
57
|
+
/** DELETE 接口地址 */
|
|
58
|
+
deleteApi?: string;
|
|
59
|
+
/** DELETE 批量删除 接口地址 */
|
|
60
|
+
multipleDeleteApi?: string;
|
|
61
|
+
|
|
62
|
+
/** GET 列表 接口请求前数据处理回调 */
|
|
63
|
+
getListReqMap?: RequestMapFunction<QueryParams>;
|
|
64
|
+
/** GET 列表 接口请求结果数据处理回调 */
|
|
65
|
+
getListResMap?: MapFunction<GetListResult<T>, GetListResult<T>>;
|
|
66
|
+
/** GET 详情 接口请求前数据处理回调 */
|
|
67
|
+
getReqMap?: RequestMapFunction<QueryParams>;
|
|
68
|
+
/** GET 详情 接口请求结果数据处理回调 */
|
|
69
|
+
getResMap?: MapFunction<T, T>;
|
|
70
|
+
/** POST 接口请求前数据处理回调 */
|
|
71
|
+
createReqMap?: RequestMapFunction<RequestData>;
|
|
72
|
+
/** POST 接口请求结果数据处理回调 */
|
|
73
|
+
createResMap?: MapFunction<ResponseData, ResponseData>;
|
|
74
|
+
/** PUT 接口请求前数据处理回调 */
|
|
75
|
+
updateReqMap?: RequestMapFunction<RequestData>;
|
|
76
|
+
/** PUT 接口请求结果数据处理回调 */
|
|
77
|
+
updateResMap?: MapFunction<ResponseData, ResponseData>;
|
|
78
|
+
/** PATCH 接口请求前数据处理回调 */
|
|
79
|
+
patchReqMap?: RequestMapFunction<RequestData>;
|
|
80
|
+
/** PATCH 接口请求结果数据处理回调 */
|
|
81
|
+
patchResMap?: MapFunction<ResponseData, ResponseData>;
|
|
82
|
+
/** DELETE 接口请求前数据处理回调 */
|
|
83
|
+
deleteReqMap?: RequestMapFunction<JSONObject>;
|
|
84
|
+
/** DELETE 接口请求结果数据处理回调 */
|
|
85
|
+
deleteResMap?: MapFunction<ResponseData, ResponseData>;
|
|
86
|
+
/** DELETE 批量删除 接口请求前数据处理回调 */
|
|
87
|
+
multipleDeleteReqMap?: RequestMapFunction<JSONObject>;
|
|
88
|
+
/** DELETE 批量删除 接口请求结果数据处理回调 */
|
|
89
|
+
multipleDeleteResMap?: MapFunction<ResponseData, ResponseData>;
|
|
90
|
+
|
|
91
|
+
/** 统一 response 回调,用于处理定制 response 格式 */
|
|
92
|
+
handleResponse?: HandleResponseFunc;
|
|
93
|
+
|
|
94
|
+
/** 请求的 axios 实例 */
|
|
95
|
+
axios?: typeof axios;
|
|
96
|
+
/** axios 配置 */
|
|
97
|
+
axiosConf?: AxiosConfig;
|
|
98
|
+
|
|
99
|
+
/** GET 列表接口 axios 配置 */
|
|
100
|
+
getListAxiosConf?: AxiosConfig;
|
|
101
|
+
/** GET 详情接口 axios 配置 */
|
|
102
|
+
getAxiosConf?: AxiosConfig;
|
|
103
|
+
/** POST 接口 axios 配置 */
|
|
104
|
+
createAxiosConf?: AxiosConfig;
|
|
105
|
+
/** PUT 接口 axios 配置 */
|
|
106
|
+
updateAxiosConf?: AxiosConfig;
|
|
107
|
+
/** PATCH 接口 axios 配置 */
|
|
108
|
+
patchAxiosConf?: AxiosConfig;
|
|
109
|
+
/** DELETE 接口 axios 配置 */
|
|
110
|
+
deleteAxiosConf?: AxiosConfig;
|
|
111
|
+
/** DELETE 批量删除接口 axios 配置 */
|
|
112
|
+
multipleDeleteAxiosConf?: AxiosConfig;
|
|
113
|
+
|
|
114
|
+
/** 是否返回完整的接口数据(response.data) */
|
|
115
|
+
isResponseData?: boolean;
|
|
116
|
+
/** 是否直接返回 response */
|
|
117
|
+
isResponse?: boolean;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 删除请求配置接口
|
|
122
|
+
*/
|
|
123
|
+
export interface DeleteConfig {
|
|
124
|
+
/** axios delete url 参数 */
|
|
125
|
+
params?: JSONObject;
|
|
126
|
+
/** axios delete data 参数 */
|
|
127
|
+
data?: JSONObject;
|
|
128
|
+
/** 其他 axios delete 配置 */
|
|
129
|
+
[key: string]: any;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 获取 API 地址的选项接口
|
|
134
|
+
*/
|
|
135
|
+
export interface GetApiUrlOptions {
|
|
136
|
+
/** 请求来源标识 */
|
|
137
|
+
from?: string;
|
|
138
|
+
[key: string]: any;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 错误处理选项接口
|
|
143
|
+
*/
|
|
144
|
+
export interface HandleMsgOptions {
|
|
145
|
+
/** 是否使用 response.statusText 作为兜底 msg */
|
|
146
|
+
useStatusText?: boolean;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* DataModel 类
|
|
151
|
+
* 用于封装常用的 CRUD 请求方法
|
|
152
|
+
*/
|
|
153
|
+
class DataModel<T = any> {
|
|
154
|
+
/** 是否返回完整的接口数据(response.data) */
|
|
155
|
+
isResponseData: boolean;
|
|
156
|
+
/** 是否直接返回 response */
|
|
157
|
+
isResponse: boolean;
|
|
158
|
+
/** 统一 response 回调,用于处理定制 response 格式 */
|
|
159
|
+
handleResponse?: HandleResponseFunc;
|
|
160
|
+
|
|
161
|
+
/** 请求 url 替换的额外参数 */
|
|
162
|
+
ctx: JSONObject;
|
|
163
|
+
/** GET 请求的参数 */
|
|
164
|
+
query: QueryParams;
|
|
165
|
+
|
|
166
|
+
/** POST 接口地址 */
|
|
167
|
+
createApi?: string;
|
|
168
|
+
/** POST 接口提交前的数据处理回调 */
|
|
169
|
+
createMap?: MapFunction<T, T>;
|
|
170
|
+
/** GET 详情 接口地址 */
|
|
171
|
+
getApi?: string;
|
|
172
|
+
/** GET 详情 接口返回后的数据处理回调 */
|
|
173
|
+
getMap?: MapFunction<T, T>;
|
|
174
|
+
/** GET 列表 接口地址 */
|
|
175
|
+
getListApi?: string;
|
|
176
|
+
/** GET 列表 接口返回后的数据处理回调 */
|
|
177
|
+
getListMap?: MapFunction<T, T>;
|
|
178
|
+
/** GET 列表 接口回调,用于自定义列表请求接口 */
|
|
179
|
+
getListFunc?: GetListFunc<T>;
|
|
180
|
+
/** PUT 接口地址 */
|
|
181
|
+
updateApi?: string;
|
|
182
|
+
/** PUT 接口提交前的数据处理回调 */
|
|
183
|
+
updateMap?: MapFunction<T, T>;
|
|
184
|
+
/** PATCH 接口地址 */
|
|
185
|
+
patchApi?: string;
|
|
186
|
+
/** PATCH 接口提交前的数据处理回调 */
|
|
187
|
+
patchMap?: MapFunction<T, T>;
|
|
188
|
+
/** DELETE 接口地址 */
|
|
189
|
+
deleteApi?: string;
|
|
190
|
+
/** DELETE 批量删除 接口地址 */
|
|
191
|
+
multipleDeleteApi?: string;
|
|
192
|
+
|
|
193
|
+
/** GET 列表 接口请求前数据处理回调 */
|
|
194
|
+
getListReqMap?: RequestMapFunction<QueryParams>;
|
|
195
|
+
/** GET 列表 接口请求结果数据处理回调 */
|
|
196
|
+
getListResMap?: MapFunction<GetListResult<T>, GetListResult<T>>;
|
|
197
|
+
/** GET 详情 接口请求前数据处理回调 */
|
|
198
|
+
getReqMap?: RequestMapFunction<QueryParams>;
|
|
199
|
+
/** GET 详情 接口请求结果数据处理回调 */
|
|
200
|
+
getResMap?: MapFunction<T, T>;
|
|
201
|
+
/** POST 接口请求前数据处理回调 */
|
|
202
|
+
createReqMap?: RequestMapFunction<RequestData>;
|
|
203
|
+
/** POST 接口请求结果数据处理回调 */
|
|
204
|
+
createResMap?: MapFunction<ResponseData, ResponseData>;
|
|
205
|
+
/** PUT 接口请求前数据处理回调 */
|
|
206
|
+
updateReqMap?: RequestMapFunction<RequestData>;
|
|
207
|
+
/** PUT 接口请求结果数据处理回调 */
|
|
208
|
+
updateResMap?: MapFunction<ResponseData, ResponseData>;
|
|
209
|
+
/** PATCH 接口请求前数据处理回调 */
|
|
210
|
+
patchReqMap?: RequestMapFunction<RequestData>;
|
|
211
|
+
/** PATCH 接口请求结果数据处理回调 */
|
|
212
|
+
patchResMap?: MapFunction<ResponseData, ResponseData>;
|
|
213
|
+
/** DELETE 接口请求前数据处理回调 */
|
|
214
|
+
deleteReqMap?: RequestMapFunction<JSONObject>;
|
|
215
|
+
/** DELETE 接口请求结果数据处理回调 */
|
|
216
|
+
deleteResMap?: MapFunction<ResponseData, ResponseData>;
|
|
217
|
+
/** DELETE 批量删除 接口请求前数据处理回调 */
|
|
218
|
+
multipleDeleteReqMap?: RequestMapFunction<JSONObject>;
|
|
219
|
+
/** DELETE 批量删除 接口请求结果数据处理回调 */
|
|
220
|
+
multipleDeleteResMap?: MapFunction<ResponseData, ResponseData>;
|
|
221
|
+
|
|
222
|
+
/** 请求的 axios 实例 */
|
|
223
|
+
axios: typeof axios;
|
|
224
|
+
/** axios 配置 */
|
|
225
|
+
axiosConf: AxiosConfig;
|
|
226
|
+
|
|
227
|
+
/** GET 列表接口 axios 配置 */
|
|
228
|
+
getListAxiosConf?: AxiosConfig;
|
|
229
|
+
/** GET 详情接口 axios 配置 */
|
|
230
|
+
getAxiosConf?: AxiosConfig;
|
|
231
|
+
/** POST 接口 axios 配置 */
|
|
232
|
+
createAxiosConf?: AxiosConfig;
|
|
233
|
+
/** PUT 接口 axios 配置 */
|
|
234
|
+
updateAxiosConf?: AxiosConfig;
|
|
235
|
+
/** PATCH 接口 axios 配置 */
|
|
236
|
+
patchAxiosConf?: AxiosConfig;
|
|
237
|
+
/** DELETE 接口 axios 配置 */
|
|
238
|
+
deleteAxiosConf?: AxiosConfig;
|
|
239
|
+
/** DELETE 批量删除接口 axios 配置 */
|
|
240
|
+
multipleDeleteAxiosConf?: AxiosConfig;
|
|
5
241
|
|
|
6
|
-
|
|
7
|
-
constructor(params) {
|
|
242
|
+
constructor(params: DataModelOptions<T>) {
|
|
8
243
|
const {
|
|
9
244
|
ctx,
|
|
10
245
|
query,
|
|
@@ -29,9 +264,9 @@ class DataModel {
|
|
|
29
264
|
} = params;
|
|
30
265
|
|
|
31
266
|
/** 是否返回完整的接口数据(response.data) */
|
|
32
|
-
this.isResponseData = isResponseData;
|
|
267
|
+
this.isResponseData = !!isResponseData;
|
|
33
268
|
/** 是否直接返回 response */
|
|
34
|
-
this.isResponse = isResponse;
|
|
269
|
+
this.isResponse = !!isResponse;
|
|
35
270
|
/** 统一 response 回调,用于处理定制 response 格式 */
|
|
36
271
|
this.handleResponse = handleResponse;
|
|
37
272
|
|
|
@@ -42,25 +277,25 @@ class DataModel {
|
|
|
42
277
|
|
|
43
278
|
/** POST 接口地址 */
|
|
44
279
|
this.createApi = createApi;
|
|
45
|
-
/** POST 接口提交前的数据处理回调
|
|
280
|
+
/** POST 接口提交前的数据处理回调 */
|
|
46
281
|
this.createMap = createMap;
|
|
47
282
|
/** GET 详情 接口地址 */
|
|
48
283
|
this.getApi = getApi;
|
|
49
|
-
/** GET 详情 接口返回后的数据处理回调
|
|
284
|
+
/** GET 详情 接口返回后的数据处理回调 */
|
|
50
285
|
this.getMap = getMap;
|
|
51
286
|
/** GET 列表 接口地址 */
|
|
52
287
|
this.getListApi = getListApi;
|
|
53
|
-
/** GET 列表 接口返回后的数据处理回调
|
|
288
|
+
/** GET 列表 接口返回后的数据处理回调 */
|
|
54
289
|
this.getListMap = getListMap;
|
|
55
290
|
/** GET 列表 接口回调,用于自定义列表请求接口。query => ({list: [], pagination: { total: 0 }}) 优先级高于 getListApi */
|
|
56
291
|
this.getListFunc = getListFunc;
|
|
57
292
|
/** PUT 接口地址 */
|
|
58
293
|
this.updateApi = updateApi;
|
|
59
|
-
/** PUT 接口提交前的数据处理回调
|
|
294
|
+
/** PUT 接口提交前的数据处理回调 */
|
|
60
295
|
this.updateMap = updateMap;
|
|
61
296
|
/** PATCH 接口地址 */
|
|
62
297
|
this.patchApi = patchApi;
|
|
63
|
-
/** PATCH 接口提交前的数据处理回调
|
|
298
|
+
/** PATCH 接口提交前的数据处理回调 */
|
|
64
299
|
this.patchMap = patchMap;
|
|
65
300
|
/** DELETE 接口地址 */
|
|
66
301
|
this.deleteApi = deleteApi;
|
|
@@ -149,13 +384,18 @@ class DataModel {
|
|
|
149
384
|
|
|
150
385
|
/**
|
|
151
386
|
* 获取请求 url 地址
|
|
152
|
-
* @param
|
|
153
|
-
* @param
|
|
154
|
-
* @param
|
|
155
|
-
* @param
|
|
156
|
-
* @returns
|
|
387
|
+
* @param api 接口地址 https://a.com/test/:oId/:uId
|
|
388
|
+
* @param record 替换参数 { uId: 81 }
|
|
389
|
+
* @param ctx url 替换的额外参数
|
|
390
|
+
* @param opt 选项
|
|
391
|
+
* @returns 处理后的 api 地址
|
|
157
392
|
*/
|
|
158
|
-
getApiUrl(
|
|
393
|
+
getApiUrl(
|
|
394
|
+
api: string | undefined,
|
|
395
|
+
record: JSONObject = {},
|
|
396
|
+
ctx: JSONObject = this.ctx,
|
|
397
|
+
opt?: GetApiUrlOptions,
|
|
398
|
+
): string {
|
|
159
399
|
const { from } = opt ?? {};
|
|
160
400
|
if (!api) {
|
|
161
401
|
const errMsg = `${from ?? ""} api 不能为空。`;
|
|
@@ -174,12 +414,12 @@ class DataModel {
|
|
|
174
414
|
|
|
175
415
|
/**
|
|
176
416
|
* GET 详情请求
|
|
177
|
-
* @param
|
|
178
|
-
* @param
|
|
179
|
-
* @param
|
|
180
|
-
* @returns
|
|
417
|
+
* @param q query 参数
|
|
418
|
+
* @param ctx url 替换的额外参数
|
|
419
|
+
* @param axiosConf axios 配置
|
|
420
|
+
* @returns Promise<T>
|
|
181
421
|
*/
|
|
182
|
-
get(q = {}, ctx = {}, axiosConf) {
|
|
422
|
+
get(q: QueryParams = {}, ctx: JSONObject = {}, axiosConf?: AxiosConfig): Promise<T> {
|
|
183
423
|
let query = _.merge({}, this.query, q);
|
|
184
424
|
query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
|
|
185
425
|
|
|
@@ -199,7 +439,7 @@ class DataModel {
|
|
|
199
439
|
.then((response) => {
|
|
200
440
|
this.handleRes(
|
|
201
441
|
response,
|
|
202
|
-
(res) => {
|
|
442
|
+
(res: T) => {
|
|
203
443
|
if (this.getMap) {
|
|
204
444
|
res = this.getMap(res);
|
|
205
445
|
}
|
|
@@ -217,12 +457,12 @@ class DataModel {
|
|
|
217
457
|
|
|
218
458
|
/**
|
|
219
459
|
* GET 列表请求
|
|
220
|
-
* @param
|
|
221
|
-
* @param
|
|
222
|
-
* @param
|
|
223
|
-
* @returns
|
|
460
|
+
* @param q query 参数
|
|
461
|
+
* @param ctx url 替换的额外参数
|
|
462
|
+
* @param axiosConf axios 配置
|
|
463
|
+
* @returns Promise<GetListResult<T>>
|
|
224
464
|
*/
|
|
225
|
-
async getList(q, ctx, axiosConf) {
|
|
465
|
+
async getList(q: QueryParams = {}, ctx: JSONObject = {}, axiosConf?: AxiosConfig): Promise<GetListResult<T>> {
|
|
226
466
|
let query = _.merge({}, this.query, q);
|
|
227
467
|
query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
|
|
228
468
|
|
|
@@ -230,11 +470,11 @@ class DataModel {
|
|
|
230
470
|
query = this.getListReqMap(query);
|
|
231
471
|
}
|
|
232
472
|
|
|
233
|
-
let resultList = null;
|
|
473
|
+
let resultList: GetListResult<T> | null = null;
|
|
234
474
|
if (this.getListFunc) {
|
|
235
475
|
resultList = await this.getListFunc(query);
|
|
236
476
|
} else {
|
|
237
|
-
const getPro = new Promise((resolve, reject) => {
|
|
477
|
+
const getPro = new Promise<GetListResult<T>>((resolve, reject) => {
|
|
238
478
|
const apiUrl = this.getApiUrl(this.getListApi, q, ctx, { from: "getList" });
|
|
239
479
|
this.axios
|
|
240
480
|
.get(apiUrl, {
|
|
@@ -246,7 +486,7 @@ class DataModel {
|
|
|
246
486
|
.then((response) => {
|
|
247
487
|
this.handleRes(
|
|
248
488
|
response,
|
|
249
|
-
(res) => {
|
|
489
|
+
(res: GetListResult<T>) => {
|
|
250
490
|
// data: { list: [], pagination: { current: 0, total: 1 } }
|
|
251
491
|
resolve(res);
|
|
252
492
|
},
|
|
@@ -268,14 +508,14 @@ class DataModel {
|
|
|
268
508
|
|
|
269
509
|
/**
|
|
270
510
|
* POST 请求
|
|
271
|
-
* @param
|
|
272
|
-
* @param
|
|
273
|
-
* @param
|
|
274
|
-
* @returns
|
|
511
|
+
* @param params 参数
|
|
512
|
+
* @param ctx url 替换的额外参数
|
|
513
|
+
* @param axiosConf axios 配置
|
|
514
|
+
* @returns Promise<ResponseData>
|
|
275
515
|
*/
|
|
276
|
-
create(params, ctx, axiosConf) {
|
|
516
|
+
create(params: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
|
|
277
517
|
return new Promise((resolve, reject) => {
|
|
278
|
-
const opt = {
|
|
518
|
+
const opt: AxiosConfig = {
|
|
279
519
|
...this.axiosConf,
|
|
280
520
|
...this.createAxiosConf,
|
|
281
521
|
...axiosConf,
|
|
@@ -294,7 +534,7 @@ class DataModel {
|
|
|
294
534
|
.then((response) => {
|
|
295
535
|
this.handleRes(
|
|
296
536
|
response,
|
|
297
|
-
(res) => {
|
|
537
|
+
(res: ResponseData) => {
|
|
298
538
|
if (this.createResMap) {
|
|
299
539
|
res = this.createResMap(res);
|
|
300
540
|
}
|
|
@@ -309,14 +549,14 @@ class DataModel {
|
|
|
309
549
|
|
|
310
550
|
/**
|
|
311
551
|
* PUT 请求
|
|
312
|
-
* @param
|
|
313
|
-
* @param
|
|
314
|
-
* @param
|
|
315
|
-
* @returns
|
|
552
|
+
* @param params 参数
|
|
553
|
+
* @param ctx url 替换的额外参数
|
|
554
|
+
* @param axiosConf axios 配置
|
|
555
|
+
* @returns Promise<ResponseData>
|
|
316
556
|
*/
|
|
317
|
-
update(params, ctx, axiosConf) {
|
|
557
|
+
update(params: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
|
|
318
558
|
return new Promise((resolve, reject) => {
|
|
319
|
-
const opt = { ...this.axiosConf, ...this.updateAxiosConf, ...axiosConf };
|
|
559
|
+
const opt: AxiosConfig = { ...this.axiosConf, ...this.updateAxiosConf, ...axiosConf };
|
|
320
560
|
let _params = _.cloneDeep(formDataToObj(params));
|
|
321
561
|
if (this.updateReqMap) {
|
|
322
562
|
_params = this.updateReqMap(_params, params);
|
|
@@ -331,7 +571,7 @@ class DataModel {
|
|
|
331
571
|
.then((response) => {
|
|
332
572
|
this.handleRes(
|
|
333
573
|
response,
|
|
334
|
-
(res) => {
|
|
574
|
+
(res: ResponseData) => {
|
|
335
575
|
if (this.updateResMap) {
|
|
336
576
|
res = this.updateResMap(res);
|
|
337
577
|
}
|
|
@@ -346,14 +586,14 @@ class DataModel {
|
|
|
346
586
|
|
|
347
587
|
/**
|
|
348
588
|
* PATCH 请求
|
|
349
|
-
* @param
|
|
350
|
-
* @param
|
|
351
|
-
* @param
|
|
352
|
-
* @returns
|
|
589
|
+
* @param params 参数
|
|
590
|
+
* @param ctx url 替换的额外参数
|
|
591
|
+
* @param axiosConf axios 配置
|
|
592
|
+
* @returns Promise<ResponseData>
|
|
353
593
|
*/
|
|
354
|
-
patch(params, ctx, axiosConf) {
|
|
594
|
+
patch(params: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
|
|
355
595
|
return new Promise((resolve, reject) => {
|
|
356
|
-
const opt = { ...this.axiosConf, ...this.patchAxiosConf, ...axiosConf };
|
|
596
|
+
const opt: AxiosConfig = { ...this.axiosConf, ...this.patchAxiosConf, ...axiosConf };
|
|
357
597
|
let _params = _.cloneDeep(formDataToObj(params));
|
|
358
598
|
if (this.patchReqMap) {
|
|
359
599
|
_params = this.patchReqMap(_params, params);
|
|
@@ -368,7 +608,7 @@ class DataModel {
|
|
|
368
608
|
.then((response) => {
|
|
369
609
|
this.handleRes(
|
|
370
610
|
response,
|
|
371
|
-
(res) => {
|
|
611
|
+
(res: ResponseData) => {
|
|
372
612
|
if (this.patchResMap) {
|
|
373
613
|
res = this.patchResMap(res);
|
|
374
614
|
}
|
|
@@ -383,13 +623,13 @@ class DataModel {
|
|
|
383
623
|
|
|
384
624
|
/**
|
|
385
625
|
* 删除接口
|
|
386
|
-
* @param
|
|
387
|
-
* @param
|
|
388
|
-
* @param
|
|
389
|
-
* @param
|
|
390
|
-
* @returns
|
|
626
|
+
* @param config axios.delete config 参数
|
|
627
|
+
* @param config.params axios.delete url 参数
|
|
628
|
+
* @param config.data axios.delete data 参数
|
|
629
|
+
* @param ctx url 替换的额外参数
|
|
630
|
+
* @returns Promise<ResponseData>
|
|
391
631
|
*/
|
|
392
|
-
delete(config, ctx) {
|
|
632
|
+
delete(config?: DeleteConfig, ctx?: JSONObject): Promise<ResponseData> {
|
|
393
633
|
let _config = _.cloneDeep(config) || {};
|
|
394
634
|
if (this.deleteReqMap) {
|
|
395
635
|
_config = this.deleteReqMap(_config);
|
|
@@ -406,7 +646,7 @@ class DataModel {
|
|
|
406
646
|
.then((response) => {
|
|
407
647
|
this.handleRes(
|
|
408
648
|
response,
|
|
409
|
-
(res) => {
|
|
649
|
+
(res: ResponseData) => {
|
|
410
650
|
if (this.deleteResMap) {
|
|
411
651
|
res = this.deleteResMap(res);
|
|
412
652
|
}
|
|
@@ -421,13 +661,13 @@ class DataModel {
|
|
|
421
661
|
|
|
422
662
|
/**
|
|
423
663
|
* 批量删除接口
|
|
424
|
-
* @param
|
|
425
|
-
* @param
|
|
426
|
-
* @param
|
|
427
|
-
* @param
|
|
428
|
-
* @returns
|
|
664
|
+
* @param config axios.delete config 参数
|
|
665
|
+
* @param config.params axios.delete url 参数
|
|
666
|
+
* @param config.data axios.delete data 参数
|
|
667
|
+
* @param ctx url 替换的额外参数
|
|
668
|
+
* @returns Promise<ResponseData>
|
|
429
669
|
*/
|
|
430
|
-
multipleDelete(config, ctx) {
|
|
670
|
+
multipleDelete(config?: DeleteConfig, ctx?: JSONObject): Promise<ResponseData> {
|
|
431
671
|
let _config = _.cloneDeep(config) || {};
|
|
432
672
|
if (this.multipleDeleteReqMap) {
|
|
433
673
|
_config = this.multipleDeleteReqMap(_config);
|
|
@@ -444,7 +684,7 @@ class DataModel {
|
|
|
444
684
|
.then((response) => {
|
|
445
685
|
this.handleRes(
|
|
446
686
|
response,
|
|
447
|
-
(res) => {
|
|
687
|
+
(res: ResponseData) => {
|
|
448
688
|
if (this.multipleDeleteResMap) {
|
|
449
689
|
res = this.multipleDeleteResMap(res);
|
|
450
690
|
}
|
|
@@ -457,9 +697,15 @@ class DataModel {
|
|
|
457
697
|
});
|
|
458
698
|
}
|
|
459
699
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
700
|
+
/**
|
|
701
|
+
* 处理响应结果
|
|
702
|
+
* @param response axios 响应对象
|
|
703
|
+
* @param resolve 成功回调
|
|
704
|
+
* @param reject 失败回调
|
|
705
|
+
*/
|
|
706
|
+
handleRes(response: any, resolve: (res: any) => void, reject: (err: ApiError) => void) {
|
|
707
|
+
if (axiosDef.isCancel(response)) {
|
|
708
|
+
console.info("errorHandler axiosDef.isCancel", response);
|
|
463
709
|
return;
|
|
464
710
|
}
|
|
465
711
|
if (this.isResponse) {
|
|
@@ -471,18 +717,18 @@ class DataModel {
|
|
|
471
717
|
return;
|
|
472
718
|
}
|
|
473
719
|
if (typeof response !== "object") {
|
|
474
|
-
reject(new Error("response not object"));
|
|
720
|
+
reject(new Error("response not object") as ApiError);
|
|
475
721
|
return;
|
|
476
722
|
}
|
|
477
723
|
let _res = this.handleResponse ? this.handleResponse(response) : response;
|
|
478
|
-
//
|
|
724
|
+
// 兼容 传入的是 response.data 的情况
|
|
479
725
|
if (_res.data && _res.headers && _res.request) {
|
|
480
726
|
_res = _res.data;
|
|
481
727
|
}
|
|
482
728
|
const { code, data } = _res;
|
|
483
729
|
const message = this.handleMsg(response);
|
|
484
730
|
if (code == 200) {
|
|
485
|
-
|
|
731
|
+
const _data = data ?? {};
|
|
486
732
|
if (_.isObject(_data)) {
|
|
487
733
|
if (_data.content && _data.pageNumber && _data.total) {
|
|
488
734
|
_data.list = _data.content;
|
|
@@ -494,7 +740,7 @@ class DataModel {
|
|
|
494
740
|
}
|
|
495
741
|
resolve(_data);
|
|
496
742
|
} else {
|
|
497
|
-
const error = new Error(message);
|
|
743
|
+
const error = new Error(message) as ApiError;
|
|
498
744
|
error.code = code;
|
|
499
745
|
error.response = response;
|
|
500
746
|
error._msg = message;
|
|
@@ -503,9 +749,14 @@ class DataModel {
|
|
|
503
749
|
}
|
|
504
750
|
}
|
|
505
751
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
752
|
+
/**
|
|
753
|
+
* 错误处理器
|
|
754
|
+
* @param err 错误对象
|
|
755
|
+
* @param reject Promise reject 回调
|
|
756
|
+
*/
|
|
757
|
+
errorHandler(err: any = {}, reject: (err: ApiError) => void) {
|
|
758
|
+
if (axiosDef.isCancel(err)) {
|
|
759
|
+
console.info("errorHandler axiosDef.isCancel", err);
|
|
509
760
|
return;
|
|
510
761
|
}
|
|
511
762
|
if (this.isResponse) {
|
|
@@ -519,7 +770,7 @@ class DataModel {
|
|
|
519
770
|
const response = err?.response || err;
|
|
520
771
|
if (response) {
|
|
521
772
|
const message = this.handleMsg(response, { useStatusText: true });
|
|
522
|
-
const error = new Error(message);
|
|
773
|
+
const error = new Error(message) as ApiError;
|
|
523
774
|
error.code = response.status;
|
|
524
775
|
error.response = response;
|
|
525
776
|
if (message) {
|
|
@@ -532,17 +783,20 @@ class DataModel {
|
|
|
532
783
|
return reject(err ?? { _msg: _$Temp.networkErrMsg });
|
|
533
784
|
}
|
|
534
785
|
|
|
786
|
+
/**
|
|
787
|
+
* 处理错误消息
|
|
788
|
+
* @param response 响应对象
|
|
789
|
+
* @param opt 选项
|
|
790
|
+
* @returns 错误消息
|
|
791
|
+
*/
|
|
535
792
|
handleMsg(
|
|
536
|
-
response,
|
|
537
|
-
opt = {
|
|
793
|
+
response: any,
|
|
794
|
+
opt: HandleMsgOptions = {
|
|
538
795
|
// 是否使用 response.statusText 作为兜底 msg
|
|
539
796
|
useStatusText: false,
|
|
540
797
|
},
|
|
541
|
-
) {
|
|
542
|
-
const {
|
|
543
|
-
// 是否使用 response.statusText 作为兜底 msg
|
|
544
|
-
useStatusText,
|
|
545
|
-
} = opt || {};
|
|
798
|
+
): string {
|
|
799
|
+
const { useStatusText } = opt || {};
|
|
546
800
|
let message =
|
|
547
801
|
(response.data && (response.data.message || response.data.msg)) ||
|
|
548
802
|
response.msg ||
|
|
@@ -556,7 +810,12 @@ class DataModel {
|
|
|
556
810
|
return message || _$Temp.defaultErrMsg;
|
|
557
811
|
}
|
|
558
812
|
|
|
559
|
-
|
|
813
|
+
/**
|
|
814
|
+
* 获取网络错误消息
|
|
815
|
+
* @param httpCode HTTP 状态码
|
|
816
|
+
* @returns 错误消息
|
|
817
|
+
*/
|
|
818
|
+
getNetworkErrMsg(httpCode: any): string {
|
|
560
819
|
const { networkErrMsg } = _$Temp;
|
|
561
820
|
if (httpCode === "ERR_NETWORK") {
|
|
562
821
|
return networkErrMsg;
|
|
@@ -565,26 +824,15 @@ class DataModel {
|
|
|
565
824
|
}
|
|
566
825
|
}
|
|
567
826
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
return false;
|
|
579
|
-
}
|
|
580
|
-
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
581
|
-
if (!connection) {
|
|
582
|
-
return true;
|
|
583
|
-
}
|
|
584
|
-
const noNetwork = connection?.type === "none" || connection.downlink === 0 || connection.effectiveType === null;
|
|
585
|
-
return !noNetwork;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
export { axios, DataModel, formDataToObj, objToFormData, setDefaultAxios, setDefaultErrMsg, setNetworkErrMsg };
|
|
827
|
+
export {
|
|
828
|
+
axios,
|
|
829
|
+
checkNetwork,
|
|
830
|
+
DataModel,
|
|
831
|
+
formDataToObj,
|
|
832
|
+
objToFormData,
|
|
833
|
+
setDefaultAxios,
|
|
834
|
+
setDefaultErrMsg,
|
|
835
|
+
setNetworkErrMsg,
|
|
836
|
+
};
|
|
589
837
|
|
|
590
838
|
export default DataModel;
|