@tmsfe/tms-core 0.0.196 → 0.0.198

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.196",
3
+ "version": "0.0.198",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -61,7 +61,8 @@ function proxyWxRequest(): void {
61
61
  // 如果用户自定义了dataType或者responseType,则不做处理
62
62
  if (dataType || responseType) {
63
63
  util.reportFunc(url, traceparent, '用户自定义了dataType和responseType');
64
- originalRequestApi.call(this, {
64
+ // 出行业务请求流式接口时要求必须传responseType,所以这里返回 wx.request 的 requestTask,用于监听流式数据
65
+ return originalRequestApi.call(this, {
65
66
  ...originalOptions,
66
67
  success: (res) => {
67
68
  encryptUtil.dealEncryptionSwitch(url, traceId, res.header).then((msg) => {
@@ -71,7 +72,6 @@ function proxyWxRequest(): void {
71
72
  },
72
73
  header: { ...header, Traceparent: traceparent },
73
74
  });
74
- return;
75
75
  }
76
76
  // 加密请求数据
77
77
  const { data: formatData, header: formatHeader, cryptoKeyInfo } = util
package/src/request.js CHANGED
@@ -16,17 +16,7 @@ import reporter from './report/index';
16
16
  import logger from './logger';
17
17
 
18
18
  // 微信底层错误重试码 600001-微信cronet组件错误 600003-网络中断 5-接口超时
19
- export const RETRY_WX_ERRNO_MAP = {
20
- 600001: {
21
- waitTime: 500,
22
- },
23
- 600003: {
24
- waitTime: 300,
25
- },
26
- 5: {
27
- waitTime: 500,
28
- },
29
- };
19
+ export const RETRY_WX_ERR_NOS = [600001, 600003, 5];
30
20
 
31
21
  /**
32
22
  * 用于序列化需要签名的参数
@@ -201,6 +191,9 @@ export default class Request {
201
191
  secretKey = '';
202
192
  withAuth = true;
203
193
  baseParam = {};
194
+ enableChunked = false;
195
+ responseType = '';
196
+ enableHttp2 = false;
204
197
 
205
198
  /**
206
199
  * Request 构造函数
@@ -218,6 +211,15 @@ export default class Request {
218
211
  this.withAuth = !!config.withAuth;
219
212
  }
220
213
  this.baseParam = config.baseParam || {};
214
+ if (config.enableChunked) {
215
+ this.enableChunked = !!config.enableChunked;
216
+ }
217
+ if (config.responseType) {
218
+ this.responseType = config.responseType;
219
+ }
220
+ if (config.enableHttp2) {
221
+ this.enableHttp2 = !!config.enableHttp2;
222
+ }
221
223
  }
222
224
 
223
225
  /**
@@ -309,6 +311,18 @@ export default class Request {
309
311
  return this.createRequestTask(path, param, 'POST', header);
310
312
  }
311
313
 
314
+ /**
315
+ * 发送post方式的请求(返回 wx.request 的 requestTask,用于监听流式数据)
316
+ * @memberof Request
317
+ * @param {string} path 请求接口路径
318
+ * @param {object} param 业务参数
319
+ * @param {object} header 自定义请求头
320
+ * @returns {Promise} Promise<WechatMiniprogram.RequestTask>
321
+ */
322
+ execPostTask(path, param, header) {
323
+ return this.createStreamRequestTask(path, param, 'POST', header);
324
+ }
325
+
312
326
  /**
313
327
  * @memberof Request
314
328
  * @param {String} path 请求接口路径
@@ -384,6 +398,7 @@ export default class Request {
384
398
  * @param {object} header 自定义的请求头
385
399
  * @returns {Promise} 接口返回结果
386
400
  */
401
+ // eslint-disable-next-line complexity
387
402
  async createRequestTask(path, param = {}, method = 'POST', header = {}) {
388
403
  try {
389
404
  const res = await this.invokeRequest(path, param, method, header);
@@ -400,18 +415,17 @@ export default class Request {
400
415
  return res;
401
416
  } catch (err) {
402
417
  // 微信系统errno:在重试码白名单内,重试
403
- const { retryFlag, retryApiWhiteMap } = wx.tmsFlagMap || {};
418
+ const { retryFlag, retryApiWhiteMap, requestRetryCount } = wx.tmsFlagMap || {};
404
419
  const isWhiteApi = retryApiWhiteMap?.home === 'all' ? true : (retryApiWhiteMap?.home || []).includes(path);
405
- const canRetry = errno => RETRY_WX_ERRNO_MAP[errno] && isWhiteApi;
420
+ const canRetry = errno => RETRY_WX_ERR_NOS.includes(errno) && isWhiteApi;
406
421
  if (retryFlag && canRetry(err?.errno)) {
407
- const waitTime = RETRY_WX_ERRNO_MAP[err.errno]?.waitTime || 300;
408
422
  return await this.handleReTry(path, param, method, header, {
409
423
  reportMsg: {
410
424
  errno: err.errno,
411
425
  errMsg: err.errMsg,
412
426
  },
413
- // maxRetryCount: 2, // 先立即重试一次,失败再延时重试一次
414
- waitTime, // 微信底层错误300ms后重试一次
427
+ maxRetryCount: requestRetryCount, // 重试次数
428
+ waitTime: 300, // 微信底层错误300ms后重试一次
415
429
  isSuccess: retryRes => !canRetry(retryRes?.errno),
416
430
  });
417
431
  }
@@ -540,6 +554,33 @@ export default class Request {
540
554
  });
541
555
  });
542
556
  }
557
+
558
+ /**
559
+ * 创建发送请求任务(返回 requestTask,用于流式场景)
560
+ * 不封装为 Promise,直接返回 wx.request 的 requestTask,便于调用方绑定 onChunkReceived
561
+ * @memberof Request
562
+ * @param {string} path 请求接口路径
563
+ * @param {object} param 业务参数
564
+ * @param {string} method 请求方法 get/post
565
+ * @param {object} header 自定义的请求头
566
+ * @returns {Promise} 返回 wx.request 的 requestTask
567
+ */
568
+ async createStreamRequestTask(path, param = {}, method = 'POST', header = {}) {
569
+ const requestParam = await composeParam(param, this.withAuth, this.baseParam);
570
+ const data = sign(requestParam, this.secretKey);
571
+
572
+ // 返回原始 requestTask,供外部绑定 onChunkReceived
573
+ const requestTask = wx.request({
574
+ url: this.makeUrl(path),
575
+ header,
576
+ method,
577
+ data,
578
+ enableHttp2: this.enableHttp2,
579
+ enableChunked: this.enableChunked,
580
+ responseType: this.responseType,
581
+ });
582
+ return requestTask;
583
+ }
543
584
  }
544
585
 
545
586
  /**