@hzab/data-model 1.2.1 → 1.3.1
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/CHANGELOG.md +9 -0
- package/README.md +8 -0
- package/package.json +1 -1
- package/src/data-model.js +85 -16
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
# @hzab/data-model@1.3.0
|
|
2
|
+
|
|
3
|
+
- fix: useStatusText: true 问题修复
|
|
4
|
+
|
|
5
|
+
# @hzab/data-model@1.3.0
|
|
6
|
+
|
|
7
|
+
- feat: 网络情况判断
|
|
8
|
+
- feat: 新增参数 isResponseData 是否返回完整的接口数据(response.data); isResponse 是否直接返回 response
|
|
9
|
+
|
|
1
10
|
# @hzab/data-model@1.2.1
|
|
2
11
|
|
|
3
12
|
- fix: setAxTimeout default timeout 0
|
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@ import DataModel, {
|
|
|
16
16
|
getToken,
|
|
17
17
|
// 设置默认兜底的错误信息
|
|
18
18
|
setDefaultErrMsg,
|
|
19
|
+
// 设置默认网络异常的错误信息
|
|
20
|
+
setNetworkErrMsg,
|
|
19
21
|
// 设置 DataModel 中默认的 axios
|
|
20
22
|
setDefaultAxios,
|
|
21
23
|
} from "@hzab/data-model";
|
|
@@ -85,6 +87,10 @@ const listDM = new DataModel({
|
|
|
85
87
|
multipleDeleteReqMap,
|
|
86
88
|
// 批量删除接口 delete 出参枚举函数
|
|
87
89
|
multipleDeleteResMap,
|
|
90
|
+
// 是否返回完整的接口数据(response.data)
|
|
91
|
+
isResponseData,
|
|
92
|
+
// 是否直接返回 response
|
|
93
|
+
isResponse,
|
|
88
94
|
});
|
|
89
95
|
|
|
90
96
|
// useDataModel,解决 hooks 写法 model 重复实例化导致 query 丢失的问题
|
|
@@ -145,6 +151,8 @@ function Demo({ orgId }) {
|
|
|
145
151
|
| deleteResMap | Function | 否 | - | 删除接口 get 出参枚举函数 |
|
|
146
152
|
| multipleDeleteReqMap | Function | 否 | - | 批量删除接口 get 入参枚举函数 |
|
|
147
153
|
| multipleDeleteResMap | Function | 否 | - | 批量删除接口 get 出参枚举函数 |
|
|
154
|
+
| isResponseData | boolean | 否 | - | 是否返回完整的接口数据(response.data) |
|
|
155
|
+
| response | boolean | 否 | - | 是否直接返回 response |
|
|
148
156
|
|
|
149
157
|
## useDataModel
|
|
150
158
|
|
package/package.json
CHANGED
package/src/data-model.js
CHANGED
|
@@ -2,8 +2,11 @@ import _ from "lodash";
|
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
|
|
4
4
|
const DEFAULT_MSG = "未知错误";
|
|
5
|
+
const NETWORK_MSG = "网络异常,请检查网络是否开启";
|
|
6
|
+
|
|
5
7
|
const _$Temp = {
|
|
6
|
-
|
|
8
|
+
defaultErrMsg: DEFAULT_MSG,
|
|
9
|
+
networkErrMsg: NETWORK_MSG,
|
|
7
10
|
};
|
|
8
11
|
|
|
9
12
|
class DataModel {
|
|
@@ -24,8 +27,15 @@ class DataModel {
|
|
|
24
27
|
multipleDeleteApi,
|
|
25
28
|
axios: as,
|
|
26
29
|
axiosConf,
|
|
30
|
+
// 是否返回完整的接口数据(response.data)
|
|
31
|
+
isResponseData,
|
|
32
|
+
// 是否直接返回 response
|
|
33
|
+
isResponse,
|
|
27
34
|
} = params;
|
|
28
35
|
|
|
36
|
+
this.isResponseData = isResponseData;
|
|
37
|
+
this.isResponse = isResponse;
|
|
38
|
+
|
|
29
39
|
this.ctx = ctx || {};
|
|
30
40
|
this.query = query || {};
|
|
31
41
|
this.axios = as || _$Temp.axios || axios;
|
|
@@ -324,6 +334,14 @@ class DataModel {
|
|
|
324
334
|
}
|
|
325
335
|
|
|
326
336
|
handleRes(response, resolve, reject) {
|
|
337
|
+
if (this.isResponse) {
|
|
338
|
+
resolve(response);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (this.isResponseData) {
|
|
342
|
+
resolve(response?.data ?? response);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
327
345
|
if (typeof response !== "object") {
|
|
328
346
|
reject(new Error("response not object"));
|
|
329
347
|
return;
|
|
@@ -333,34 +351,38 @@ class DataModel {
|
|
|
333
351
|
if (_res.data && _res.headers && _res.request && typeof _res.data?.code === "number") {
|
|
334
352
|
_res = _res.data;
|
|
335
353
|
}
|
|
336
|
-
const { code,
|
|
354
|
+
const { code, data } = _res;
|
|
355
|
+
const message = this.handleMsg(response);
|
|
337
356
|
if (code === 200) {
|
|
338
357
|
let _data = data ?? {};
|
|
339
|
-
if (_.isObject(_data)
|
|
358
|
+
if (_.isObject(_data)) {
|
|
340
359
|
// 前缀 _ 避免与 data 里已有的 message 冲突
|
|
341
|
-
_data._msg = message
|
|
342
|
-
_data._message =
|
|
360
|
+
_data._msg = message;
|
|
361
|
+
_data._message = message;
|
|
343
362
|
}
|
|
344
363
|
resolve(_data);
|
|
345
364
|
} else {
|
|
346
|
-
const
|
|
347
|
-
const error = new Error(_msg);
|
|
365
|
+
const error = new Error(message);
|
|
348
366
|
error.code = code;
|
|
349
367
|
error.response = response;
|
|
350
|
-
error._msg =
|
|
351
|
-
error._message =
|
|
368
|
+
error._msg = message;
|
|
369
|
+
error._message = message;
|
|
352
370
|
reject(error);
|
|
353
371
|
}
|
|
354
372
|
}
|
|
355
373
|
|
|
356
374
|
errorHandler(err, reject) {
|
|
375
|
+
if (this.isResponse) {
|
|
376
|
+
reject(err);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (this.isResponseData) {
|
|
380
|
+
reject(err?.data ?? err);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
357
383
|
const response = err.response || err;
|
|
358
384
|
if (response) {
|
|
359
|
-
const message =
|
|
360
|
-
(response.data && (response.data.message || response.data.msg)) ||
|
|
361
|
-
response.msg ||
|
|
362
|
-
response.statusText ||
|
|
363
|
-
_$Temp.defaultMsg;
|
|
385
|
+
const message = this.handleMsg(response, { useStatusText: true });
|
|
364
386
|
const error = new Error(message);
|
|
365
387
|
error.code = response.status;
|
|
366
388
|
error.response = response;
|
|
@@ -371,8 +393,49 @@ class DataModel {
|
|
|
371
393
|
}
|
|
372
394
|
return reject(error);
|
|
373
395
|
}
|
|
374
|
-
return reject(err || { _msg: _$Temp.
|
|
396
|
+
return reject(err || { _msg: _$Temp.defaultErrMsg });
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
handleMsg(response, { useStatusText }) {
|
|
400
|
+
let message =
|
|
401
|
+
(response.data && (response.data.message || response.data.msg)) ||
|
|
402
|
+
response.msg ||
|
|
403
|
+
this.getNetworkErrMsg(response);
|
|
404
|
+
|
|
405
|
+
if (useStatusText) {
|
|
406
|
+
message = response.statusText;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return message || _$Temp.defaultErrMsg;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
getNetworkErrMsg(httpCode) {
|
|
413
|
+
const { networkErrMsg } = _$Temp;
|
|
414
|
+
if (httpCode === "ERR_NETWORK") {
|
|
415
|
+
return networkErrMsg;
|
|
416
|
+
}
|
|
417
|
+
return checkNetwork() ? "" : networkErrMsg;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* 检测网络状态
|
|
423
|
+
* @param {*} errMsg 网络异常消息
|
|
424
|
+
* @returns
|
|
425
|
+
*/
|
|
426
|
+
export function checkNetwork() {
|
|
427
|
+
if (!navigator) {
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
if (navigator.onLine === false) {
|
|
431
|
+
return false;
|
|
375
432
|
}
|
|
433
|
+
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
434
|
+
if (!connection) {
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
const noNetwork = connection?.type === "none" || connection.downlink === 0 || connection.effectiveType === null;
|
|
438
|
+
return !noNetwork;
|
|
376
439
|
}
|
|
377
440
|
|
|
378
441
|
function setDefaultAxios(ax) {
|
|
@@ -383,7 +446,13 @@ function setDefaultAxios(ax) {
|
|
|
383
446
|
|
|
384
447
|
export function setDefaultErrMsg(msg) {
|
|
385
448
|
if (msg) {
|
|
386
|
-
_$Temp.
|
|
449
|
+
_$Temp.defaultErrMsg = msg;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export function setNetworkErrMsg(msg) {
|
|
454
|
+
if (msg) {
|
|
455
|
+
_$Temp.networkErrMsg = msg;
|
|
387
456
|
}
|
|
388
457
|
}
|
|
389
458
|
|