@tmsfe/tms-core 0.0.188 → 0.0.189

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.188",
3
+ "version": "0.0.189",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
package/src/logger.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @desc: 日志输出
3
+ */
4
+
5
+ const logger = wx.getLogManager({});
6
+
7
+ // 上报自定义事件
8
+ function reportEvent(eventName, eventData: { ext1?: unknown; ext2?: unknown; ext3?: unknown; } = {}) {
9
+ // 避免tmsAegisInst内部报错
10
+ try {
11
+ wx.tmsAegisInst?.reportEvent({
12
+ name: eventName,
13
+ ...eventData,
14
+ });
15
+ } catch (e) {}
16
+ }
17
+
18
+ // eslint-disable-next-line import/no-anonymous-default-export
19
+ export default {
20
+ ...logger,
21
+ reportEvent,
22
+ };
package/src/request.js CHANGED
@@ -13,8 +13,16 @@ import { getEnvInfo, getAuthInfo } from './env';
13
13
  import { safeJsonParse } from './objUtils';
14
14
  import { encryptObjInit } from './encrypt/index';
15
15
  import reporter from './report/index';
16
+ import logger from './logger';
16
17
 
17
- const logger = wx.getLogManager({});
18
+ const RETRY_WX_ERR_NOS = [600001, 600003, 5]; // 微信底层错误重试码 600001-微信cronet组件错误 600003-网络中断 5-接口超时
19
+
20
+ // 重试api白名单
21
+ const RETRY_API_WHITE_LIST = [
22
+ 'marketing/apollo/config',
23
+ 'user/pubsubscribe/get',
24
+ 'marketing/config',
25
+ ];
18
26
 
19
27
  /**
20
28
  * 用于序列化需要签名的参数
@@ -372,15 +380,108 @@ export default class Request {
372
380
  * @param {object} header 自定义的请求头
373
381
  * @returns {Promise} 接口返回结果
374
382
  */
375
- async createRequestTask(path, param = {}, method = 'POST', header = {}, hasRetry = false) {
376
- // maplbs接口不需要token
383
+ async createRequestTask(path, param = {}, method = 'POST', header = {}) {
384
+ try {
385
+ const res = await this.invokeRequest(path, param, method, header);
386
+ // format 业务errcode,用于判断是否做重试
387
+ const { errCode } = formatResErrCode(path, res.data);
388
+ // 处理token失效情况(errcode=35)
389
+ const needRetryLogin = errCode === 35 && isPathNeedRetryLogin(path);
390
+ if (needRetryLogin) {
391
+ // 调用runtime的login方法刷新token
392
+ await loginRefresh();
393
+ // 刷新后重试一次请求
394
+ return await this.invokeRequest(path, param, method, header);
395
+ }
396
+ return res;
397
+ } catch (err) {
398
+ // 微信系统errno:在重试码白名单内,重试
399
+ const isWhiteApi = RETRY_API_WHITE_LIST.includes(path);
400
+ const canRetry = errno => RETRY_WX_ERR_NOS.includes(errno) && isWhiteApi;
401
+ if (canRetry(err?.errno) && wx.tmsFlagMap?.retryFlag) {
402
+ return await this.handleReTry(path, param, method, header, {
403
+ reportMsg: {
404
+ errno: err.errno,
405
+ errMsg: err.errMsg,
406
+ },
407
+ // maxRetryCount: 2, // 先立即重试一次,失败再延时重试一次
408
+ waitTime: 300, // 微信底层错误300ms后重试一次
409
+ isSuccess: retryRes => !canRetry(retryRes?.errno),
410
+ });
411
+ }
412
+ return Promise.reject(err);
413
+ }
414
+ }
415
+
416
+ // 执行重试
417
+ async handleReTry(path, param, method, header, options) {
418
+ const currentRetryCount = options.currentRetryCount ?? 0;
419
+ const maxRetry = options.maxRetryCount ?? 1;
420
+ const genReportParams = (others = {}) => ({
421
+ ext1: 'home',
422
+ ext2: path,
423
+ ext3: JSON.stringify({
424
+ ...options.reportMsg,
425
+ retryCount: `${currentRetryCount + 1}/${maxRetry}`,
426
+ ...others,
427
+ }),
428
+ });
429
+
430
+ // 执行单次重试
431
+ const executeRetry = async () => {
432
+ reporter.reportEvent('接口发起重试', genReportParams());
433
+ // 如果有等待时间则延迟
434
+ if (options.waitTime) {
435
+ await new Promise(resolve => setTimeout(resolve, options.waitTime));
436
+ }
437
+ try {
438
+ const retryRes = await this.invokeRequest(path, param, method, header);
439
+ const nextReportParams = genReportParams({
440
+ retryResErrno: retryRes?.errno,
441
+ retryResErrMsg: retryRes?.errMsg,
442
+ });
443
+ // 检查是否成功
444
+ const isSuccess = options.isSuccess?.(retryRes) ?? true;
445
+ if (isSuccess) {
446
+ reporter.reportEvent('接口重试成功', nextReportParams);
447
+ return retryRes;
448
+ }
449
+ reporter.reportEvent('接口重试失败', nextReportParams);
450
+ throw new Error('业务逻辑判断失败');
451
+ } catch (error) {
452
+ reporter.reportEvent('接口重试失败', genReportParams({
453
+ retryResErrMsg: error.errMsg,
454
+ retryResType: 'catch',
455
+ }));
456
+ throw error;
457
+ }
458
+ };
459
+
460
+ try {
461
+ return await executeRetry();
462
+ } catch (err) {
463
+ // 检查是否还可以重试
464
+ if (currentRetryCount < maxRetry - 1) {
465
+ return await this.handleReTry(path, param, method, header, {
466
+ ...options,
467
+ currentRetryCount: currentRetryCount + 1,
468
+ waitTime: 300, // 后续重试默认300ms间隔
469
+ });
470
+ }
471
+ return Promise.reject(err);
472
+ }
473
+ }
474
+
475
+ // 执行请求
476
+ async invokeRequest(path, param = {}, method = 'POST', header = {}) {
477
+ // maplbs接口不需要token
377
478
  const withAuth = path.includes('maplbs/ws') ? false : this.withAuth;
378
479
  const requestParam = await composeParam(param, withAuth, this.baseParam);
379
480
  const data = sign(requestParam, this.secretKey);
380
481
  return new Promise((resolve, reject) => {
381
482
  const requestTime = Date.now();
382
483
  const printLog = (isSuccess, res) => {
383
- // 埋点已经单独打日志了,接口请求数据日志太长影响分析
484
+ // 埋点已经单独打日志了,接口请求数据日志太长影响分析
384
485
  if (path.indexOf('basic/event/upload') !== -1) {
385
486
  return;
386
487
  }
@@ -416,24 +517,6 @@ export default class Request {
416
517
 
417
518
  const success = async (res) => {
418
519
  printLog(true, res.data);
419
- // format 业务errcode,用于判断是否做重试
420
- const { errCode } = formatResErrCode(path, res.data);
421
- // 处理token失效情况(errcode=35)
422
- const needRetryLogin = !hasRetry && errCode === 35 && isPathNeedRetryLogin(path);
423
- if (needRetryLogin) {
424
- try {
425
- // 调用runtime的login方法刷新token
426
- await loginRefresh();
427
- // 刷新后重试请求
428
- const retryRes = await this.createRequestTask(path, param, method, header, true);
429
- resolve(retryRes);
430
- return;
431
- } catch (e) {
432
- // 刷新token失败,直接返回原错误
433
- resolve(res);
434
- return;
435
- }
436
- }
437
520
  resolve(res);
438
521
  };
439
522
  const fail = (err) => {