@tmsfe/tms-core 0.0.203 → 0.0.206
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 +1 -1
- package/src/index.js +0 -15
- package/src/navbarUtils.js +1 -17
- package/src/report/helper.ts +2 -2
- package/src/request.js +22 -3
- package/src/syncfnmanager.js +4 -7
- package/src/utils/canvas/canvasDrawer.js +3 -1
- package/src/wxApi.ts +35 -1
- package/src/regCheck.ts +0 -40
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -56,14 +56,6 @@ import * as storage from './storage';
|
|
|
56
56
|
import * as uiUtil from './tmsuiUtils';
|
|
57
57
|
import { throttle, debounce } from './funcUtils';
|
|
58
58
|
import { formatAmount } from './priceUtils';
|
|
59
|
-
import {
|
|
60
|
-
validTaxId,
|
|
61
|
-
validPhone,
|
|
62
|
-
validName,
|
|
63
|
-
validEmail,
|
|
64
|
-
validIdNumber,
|
|
65
|
-
} from './regCheck';
|
|
66
|
-
|
|
67
59
|
|
|
68
60
|
/**
|
|
69
61
|
* @public
|
|
@@ -249,13 +241,6 @@ const api = {
|
|
|
249
241
|
/* 金额方法 */
|
|
250
242
|
formatAmount,
|
|
251
243
|
|
|
252
|
-
/* 常见正则校验 */
|
|
253
|
-
validTaxId,
|
|
254
|
-
validPhone,
|
|
255
|
-
validName,
|
|
256
|
-
validEmail,
|
|
257
|
-
validIdNumber,
|
|
258
|
-
|
|
259
244
|
/* 时间方法 */
|
|
260
245
|
groupTimeDuration,
|
|
261
246
|
formatTimeDuration,
|
package/src/navbarUtils.js
CHANGED
|
@@ -2,23 +2,7 @@
|
|
|
2
2
|
* @desc: 获取导航高度的相关函数
|
|
3
3
|
*/
|
|
4
4
|
import { compareVersion } from './compareVersion';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 获取系统信息,有缓存, 返回数据同wx.getSystemInfoSync {@link https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfoSync.html}
|
|
9
|
-
* @returns {Object} 系统信息
|
|
10
|
-
*/
|
|
11
|
-
const getSystemInfoSync = () => {
|
|
12
|
-
if (systemInfo) return systemInfo;
|
|
13
|
-
let res;
|
|
14
|
-
try {
|
|
15
|
-
systemInfo = wx.getSystemInfoSync();
|
|
16
|
-
res = systemInfo;
|
|
17
|
-
} catch (_) {
|
|
18
|
-
res = {};
|
|
19
|
-
}
|
|
20
|
-
return res;
|
|
21
|
-
};
|
|
5
|
+
import { getSystemInfoSync } from './wxApi';
|
|
22
6
|
|
|
23
7
|
/**
|
|
24
8
|
* 获取系统信息,有缓存, 返回数据同wx.getSystemInfoSync {@link https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfoSync.html}
|
package/src/report/helper.ts
CHANGED
|
@@ -44,8 +44,8 @@ function getSystemInfo(): ISystemInfo {
|
|
|
44
44
|
if (!systemInfo) {
|
|
45
45
|
const currentSystemInfo = wx.getSystemInfoSync() as any;
|
|
46
46
|
// eslint-disable-next-line
|
|
47
|
-
const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host
|
|
48
|
-
systemInfo = { model, wxVersion, platform, SDKVersion, host
|
|
47
|
+
const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host } = currentSystemInfo;
|
|
48
|
+
systemInfo = { model, wxVersion, platform, SDKVersion, host };
|
|
49
49
|
}
|
|
50
50
|
return systemInfo;
|
|
51
51
|
}
|
package/src/request.js
CHANGED
|
@@ -407,7 +407,7 @@ export default class Request {
|
|
|
407
407
|
const res = await this.invokeRequest(path, param, method, header);
|
|
408
408
|
// format 业务errcode,用于判断是否做重试
|
|
409
409
|
const { errCode } = formatResErrCode(path, res.data);
|
|
410
|
-
//
|
|
410
|
+
// 重试case1:处理token失效情况(errcode=35)
|
|
411
411
|
const needRetryLogin = errCode === 35 && isPathNeedRetryLogin(path);
|
|
412
412
|
if (needRetryLogin) {
|
|
413
413
|
// 调用runtime的login方法刷新token
|
|
@@ -415,9 +415,24 @@ export default class Request {
|
|
|
415
415
|
// 刷新后重试一次请求
|
|
416
416
|
return await this.invokeRequest(path, param, method, header);
|
|
417
417
|
}
|
|
418
|
+
|
|
419
|
+
// 重试case3:处理网关框架码重试 requestRetryFlag?.gatewayCode由七彩石配置
|
|
420
|
+
const { requestRetryFlag } = wx.tmsFlagMap || {};
|
|
421
|
+
// X-Gateway-Code必须首字母大写,否则真机取不到
|
|
422
|
+
const gatewayCode = res?.header?.['X-Gateway-Code'];
|
|
423
|
+
if (requestRetryFlag?.gatewayCode && isGatewayCodeNeedRetry(gatewayCode)) {
|
|
424
|
+
return await this.handleReTry(path, param, method, header, {
|
|
425
|
+
reportMsg: {
|
|
426
|
+
gatewayCode,
|
|
427
|
+
errMsg: `home gateway code ${gatewayCode} requires retry`,
|
|
428
|
+
},
|
|
429
|
+
waitTime: 10, // 网关框架码错误10ms后再重试
|
|
430
|
+
isSuccess: retryRes => !isGatewayCodeNeedRetry(retryRes?.header?.gatewayCode),
|
|
431
|
+
});
|
|
432
|
+
}
|
|
418
433
|
return res;
|
|
419
434
|
} catch (err) {
|
|
420
|
-
//
|
|
435
|
+
// 重试case2:微信系统errno:在重试码白名单内,重试
|
|
421
436
|
const { retryFlag, retryApiWhiteMap, requestRetryCount } = wx.tmsFlagMap || {};
|
|
422
437
|
const isWhiteApi = retryApiWhiteMap?.home === 'all' ? true : (retryApiWhiteMap?.home || []).includes(path);
|
|
423
438
|
const canRetry = errno => RETRY_WX_ERR_NOS.includes(errno) && isWhiteApi;
|
|
@@ -574,7 +589,7 @@ export default class Request {
|
|
|
574
589
|
* @returns {Promise} 返回 wx.request 的 requestTask
|
|
575
590
|
*/
|
|
576
591
|
async createStreamRequestTask(path, params = {}) {
|
|
577
|
-
const { param = {}, method = 'POST', header = {}, callbacks = {} } = params;
|
|
592
|
+
const { param = {}, method = 'POST', header = {}, callbacks = {}, timeout = 60000 } = params;
|
|
578
593
|
const requestParam = await composeParam(param, this.withAuth, this.baseParam);
|
|
579
594
|
const data = sign(requestParam, this.secretKey);
|
|
580
595
|
|
|
@@ -586,6 +601,7 @@ export default class Request {
|
|
|
586
601
|
header,
|
|
587
602
|
method,
|
|
588
603
|
data,
|
|
604
|
+
timeout,
|
|
589
605
|
enableHttp2: this.enableHttp2,
|
|
590
606
|
enableChunked: this.enableChunked,
|
|
591
607
|
responseType: this.responseType,
|
|
@@ -619,6 +635,9 @@ const isPathNeedRetryLogin = (path) => {
|
|
|
619
635
|
return noRetryPaths.every(noRetryPath => !path.includes(noRetryPath));
|
|
620
636
|
};
|
|
621
637
|
|
|
638
|
+
// 判断网关框架码是否需要重试
|
|
639
|
+
const isGatewayCodeNeedRetry = gatewayCode => (gatewayCode > 0 && gatewayCode <= 999) || +gatewayCode === 5000;
|
|
640
|
+
|
|
622
641
|
let refreshPromise;
|
|
623
642
|
async function loginRefresh() {
|
|
624
643
|
if (!refreshPromise) {
|
package/src/syncfnmanager.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getSystemInfoSync as getSystemInfoSyncFunc } from './wxApi';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @desc: 本文件负责对小程序调用wx同步方法的管理
|
|
3
5
|
*/
|
|
@@ -14,13 +16,8 @@ const getSystemInfoSync = () => {
|
|
|
14
16
|
return systemInfo;
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return systemInfo;
|
|
21
|
-
} catch (_) {
|
|
22
|
-
return {};
|
|
23
|
-
}
|
|
19
|
+
systemInfo = getSystemInfoSyncFunc();
|
|
20
|
+
return systemInfo;
|
|
24
21
|
};
|
|
25
22
|
|
|
26
23
|
/**
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getSystemInfoSync } from '../../wxApi';
|
|
2
|
+
|
|
1
3
|
class CanvasDrawer {
|
|
2
4
|
constructor(canvasId, container, instance = '', designWindowWidth = 750) {
|
|
3
5
|
this.canvasId = canvasId; // canvasId
|
|
@@ -16,7 +18,7 @@ class CanvasDrawer {
|
|
|
16
18
|
const canvas = res[0].node;
|
|
17
19
|
this.canvas = canvas;
|
|
18
20
|
this.ctx = canvas.getContext('2d');
|
|
19
|
-
const { pixelRatio: dpr, windowWidth } =
|
|
21
|
+
const { pixelRatio: dpr, windowWidth } = getSystemInfoSync();
|
|
20
22
|
canvas.width = res[0].width * dpr;
|
|
21
23
|
canvas.height = res[0].height * dpr;
|
|
22
24
|
this.ratio = windowWidth / this.designWindowWidth;
|
package/src/wxApi.ts
CHANGED
|
@@ -35,4 +35,38 @@ async function getWxSetting(forceRefresh = false): Promise<{
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* 获取系统信息
|
|
40
|
+
* 文档:https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfoSync.html
|
|
41
|
+
* !!! 注意事项 !!!
|
|
42
|
+
* 1. 小程序常规打开和被半屏时屏幕尺寸不一样,获取屏幕高度、顶部栏胶囊位置的不要使用该缓存,会导致样式异常,如:navBar组件
|
|
43
|
+
* 2. 获取系统授权信息的不要使用该缓存,会导致授权信息更新不及时,如:系统位置定位开关打开 | 允许微信使用位置定位能力开关打开
|
|
44
|
+
*/
|
|
45
|
+
function getSystemInfoSync(): WechatMiniprogram.SystemInfo {
|
|
46
|
+
if (wx.tmsSystemInfo) {
|
|
47
|
+
return wx.tmsSystemInfo;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
// 可以安全缓存的key,其它key缓存可能会因为更新不及时出现异常
|
|
52
|
+
const canCacheKeys = [
|
|
53
|
+
'brand', 'model', 'pixelRatio', 'screenWidth', 'windowWidth', 'language',
|
|
54
|
+
'version', 'system', 'platform', 'SDKVersion', 'benchmarkLevel', 'host',
|
|
55
|
+
];
|
|
56
|
+
const systemInfo = wx.getSystemInfoSync();
|
|
57
|
+
const safeSystemInfo = {};
|
|
58
|
+
canCacheKeys.forEach((key) => {
|
|
59
|
+
safeSystemInfo[key] = systemInfo[key];
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
wx.tmsSystemInfo = safeSystemInfo;
|
|
63
|
+
return wx.tmsSystemInfo;
|
|
64
|
+
} catch (_) {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
getWxSetting,
|
|
71
|
+
getSystemInfoSync,
|
|
72
|
+
};
|
package/src/regCheck.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 常见正则校验
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// 校验税号
|
|
6
|
-
function validTaxId(taxId) {
|
|
7
|
-
return /^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/.test(taxId);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// 校验电话、手机号
|
|
11
|
-
function validPhone(phone) {
|
|
12
|
-
const reg = /^(1[3456789]\d{9}|(\d{3,4}-\d{7,8}))$/;
|
|
13
|
-
return reg.test(phone);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// 校验姓名只允许中英文
|
|
17
|
-
function validName(name) {
|
|
18
|
-
const reg = /^[\u4e00-\u9fa5a-zA-Z]+$/;
|
|
19
|
-
return reg.test(name);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// 验证邮箱地址
|
|
23
|
-
function validEmail(email) {
|
|
24
|
-
const regExp = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/;
|
|
25
|
-
return regExp.test(email);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// 匹配身份证号
|
|
29
|
-
function validIdNumber(idNumber: string): boolean {
|
|
30
|
-
const regExp = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
|
|
31
|
-
return regExp.test(idNumber);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export {
|
|
35
|
-
validTaxId,
|
|
36
|
-
validPhone,
|
|
37
|
-
validName,
|
|
38
|
-
validEmail,
|
|
39
|
-
validIdNumber,
|
|
40
|
-
};
|