q-koa 13.8.54 → 13.8.55

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.
@@ -5,7 +5,8 @@
5
5
  * 1. 删除 Singleton.instance + lodash.isEqual 的假单例
6
6
  * 2. 新增 static _instanceMap = new Map(),key 为 config.appid
7
7
  * 3. 构造函数按 appid 直接命中缓存,不再做深比较
8
- * 4. 其余逻辑(url 常量 / cache LRU / 所有方法)保持不变
8
+ * 4. 新增内部 _withTokenRetry:遇 40001/40014/42001 强刷当前 appid 的
9
+ * access_token 并重试一次,公共方法签名不暴露任何重试参数
9
10
  */
10
11
 
11
12
  const util = require('util')
@@ -198,16 +199,23 @@ module.exports = class Singleton {
198
199
  return access_token
199
200
  }
200
201
 
201
- async getExpress(_retried = false) {
202
- const access_token = await this.getAccessToken()
203
- const url = util.format(getExpressUrl, access_token)
204
- const result = await wxAxios.post(url).then((res) => res.data)
202
+ // access_token 失效/过期统一重试:强刷当前 appid 的 token 后重试一次
203
+ async _withTokenRetry(request) {
204
+ let access_token = await this.getAccessToken()
205
+ let result = await request(access_token)
206
+ if (result.errcode && [40001, 40014, 42001].includes(result.errcode)) {
207
+ access_token = await this.getAccessToken(true)
208
+ result = await request(access_token)
209
+ }
210
+ return result
211
+ }
212
+
213
+ async getExpress() {
214
+ const result = await this._withTokenRetry(async (access_token) => {
215
+ const url = util.format(getExpressUrl, access_token)
216
+ return await wxAxios.post(url).then((res) => res.data)
217
+ })
205
218
  if (result.errcode) {
206
- if ([40001, 40014, 42001].includes(result.errcode)) {
207
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
208
- await this.getAccessToken(true)
209
- return await this.getExpress(true)
210
- }
211
219
  throw new Error(`${result.errcode};${result.errmsg}`)
212
220
  }
213
221
  return result
@@ -225,10 +233,7 @@ module.exports = class Singleton {
225
233
  shop,
226
234
  insured,
227
235
  service,
228
- }, _retried = false) {
229
- const access_token = await this.getAccessToken()
230
- const url = util.format(addOrderUrl, access_token)
231
-
236
+ }) {
232
237
  const formatOptions = {
233
238
  order_id,
234
239
  openid,
@@ -242,72 +247,42 @@ module.exports = class Singleton {
242
247
  service,
243
248
  }
244
249
 
245
- const result = await wxAxios.post(url, formatOptions).then((res) => res.data)
250
+ const result = await this._withTokenRetry(async (access_token) => {
251
+ const url = util.format(addOrderUrl, access_token)
252
+ return await wxAxios.post(url, formatOptions).then((res) => res.data)
253
+ })
246
254
  if (result.errcode) {
247
- if ([40001, 40014, 42001].includes(result.errcode)) {
248
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
249
- await this.getAccessToken(true)
250
- return await this.addOrder({
251
- add_source,
252
- order_id,
253
- openid,
254
- delivery_id,
255
- biz_id,
256
- sender,
257
- receiver,
258
- cargo,
259
- shop,
260
- insured,
261
- service,
262
- }, true)
263
- }
264
255
  throw new Error(`${result.errcode};${result.errmsg}`)
265
256
  }
266
257
  return result
267
258
  }
268
259
 
269
- async addSubanchor({ roomId, username }, _retried = false) {
270
- const access_token = await this.getAccessToken()
271
- const url = util.format(addSubanchorUrl, access_token)
272
-
260
+ async addSubanchor({ roomId, username }) {
273
261
  const formatOptions = {
274
262
  roomId,
275
263
  username,
276
264
  }
277
265
 
278
- const result = await wxAxios.post(url, formatOptions).then((res) => res.data)
266
+ const result = await this._withTokenRetry(async (access_token) => {
267
+ const url = util.format(addSubanchorUrl, access_token)
268
+ return await wxAxios.post(url, formatOptions).then((res) => res.data)
269
+ })
279
270
  if (result.errcode) {
280
- if ([40001, 40014, 42001].includes(result.errcode)) {
281
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
282
- await this.getAccessToken(true)
283
- return await this.addSubanchor({
284
- roomId,
285
- username,
286
- }, true)
287
- }
288
271
  throw new Error(`${result.errcode};${result.errmsg}`)
289
272
  }
290
273
  return result
291
274
  }
292
275
 
293
- async addRole({ username, role = 2 }, _retried = false) {
294
- const access_token = await this.getAccessToken()
295
- const url = util.format(addRoleUrl, access_token)
296
-
276
+ async addRole({ username, role = 2 }) {
297
277
  const formatOptions = {
298
278
  username,
299
279
  role,
300
280
  }
301
- const result = await wxAxios.post(url, formatOptions).then((res) => res.data)
281
+ const result = await this._withTokenRetry(async (access_token) => {
282
+ const url = util.format(addRoleUrl, access_token)
283
+ return await wxAxios.post(url, formatOptions).then((res) => res.data)
284
+ })
302
285
  if (result.errcode) {
303
- if ([40001, 40014, 42001].includes(result.errcode)) {
304
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
305
- await this.getAccessToken(true)
306
- return await this.addRole({
307
- username,
308
- role,
309
- }, true)
310
- }
311
286
  throw new Error(`${result.errcode};${result.errmsg}`)
312
287
  }
313
288
  return result
@@ -325,10 +300,7 @@ module.exports = class Singleton {
325
300
  anchorWechat = '',
326
301
  anchorName = '',
327
302
  closeReplay = 1,
328
- }, _retried = false) {
329
- const access_token = await this.getAccessToken()
330
- const url = util.format(createLiveUrl, access_token)
331
-
303
+ }) {
332
304
  const formatOptions = {
333
305
  name,
334
306
  coverImg,
@@ -349,69 +321,44 @@ module.exports = class Singleton {
349
321
  closeShare: 1,
350
322
  closeKf: 1,
351
323
  }
352
- const result = await wxAxios.post(url, formatOptions).then((res) => res.data)
324
+ const result = await this._withTokenRetry(async (access_token) => {
325
+ const url = util.format(createLiveUrl, access_token)
326
+ return await wxAxios.post(url, formatOptions).then((res) => res.data)
327
+ })
353
328
  if (result.errcode) {
354
- if ([40001, 40014, 42001].includes(result.errcode)) {
355
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
356
- await this.getAccessToken(true)
357
- return await this.createLive({
358
- name,
359
- createrWechat,
360
- coverImg,
361
- startTime,
362
- endTime,
363
- shareImg,
364
- feedsImg,
365
- subAnchorWechat,
366
- anchorWechat,
367
- anchorName,
368
- closeReplay,
369
- }, true)
370
- }
371
329
  throw new Error(`${result.errcode};${result.errmsg}`)
372
330
  }
373
331
  return result
374
332
  }
375
333
 
376
- async getLive(start = 0, limit = 10, _retried = false) {
377
- const access_token = await this.getAccessToken()
378
- const url = util.format(getLiveUrl, access_token)
379
-
334
+ async getLive(start = 0, limit = 10) {
380
335
  const formatOptions = {
381
336
  start,
382
337
  limit,
383
338
  }
384
- const result = await wxAxios.post(url, formatOptions).then((res) => res.data)
339
+ const result = await this._withTokenRetry(async (access_token) => {
340
+ const url = util.format(getLiveUrl, access_token)
341
+ return await wxAxios.post(url, formatOptions).then((res) => res.data)
342
+ })
385
343
  if (result.errcode) {
386
- if ([40001, 40014, 42001].includes(result.errcode)) {
387
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
388
- await this.getAccessToken(true)
389
- return await this.getLive(start, limit, true)
390
- }
391
344
  throw new Error(`${result.errcode};${result.errmsg}`)
392
345
  }
393
346
  return result.room_info
394
347
  }
395
348
 
396
- async getPhoneNumber(code, _retried = false) {
397
- const access_token = await this.getAccessToken()
398
- const url = util.format(getPhoneNumberUrl, access_token)
399
- const result = await wxAxios.post(url, { code }).then((res) => res.data)
349
+ async getPhoneNumber(code) {
350
+ const result = await this._withTokenRetry(async (access_token) => {
351
+ const url = util.format(getPhoneNumberUrl, access_token)
352
+ return await wxAxios.post(url, { code }).then((res) => res.data)
353
+ })
400
354
  if (result.errcode) {
401
- if ([40001, 40014, 42001].includes(result.errcode)) {
402
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
403
- await this.getAccessToken(true)
404
- return await this.getPhoneNumber(code, true)
405
- }
406
355
  throw new Error(`${result.errcode};${result.errmsg}`)
407
356
  }
408
357
  return result.phone_info
409
358
  }
410
359
 
411
- async createUrlLink(options, _retried = false) {
360
+ async createUrlLink(options) {
412
361
  const { page, scene, ...rest } = options
413
- const access_token = await this.getAccessToken()
414
- const url = util.format(createUrlLinkUrl, access_token)
415
362
  const payLoad = {
416
363
  ...rest,
417
364
  path: options.page,
@@ -422,35 +369,28 @@ module.exports = class Singleton {
422
369
  : '',
423
370
  }
424
371
 
425
- const result = await wxAxios.post(url, payLoad).then((res) => res.data)
372
+ const result = await this._withTokenRetry(async (access_token) => {
373
+ const url = util.format(createUrlLinkUrl, access_token)
374
+ return await wxAxios.post(url, payLoad).then((res) => res.data)
375
+ })
426
376
 
427
377
  if (result.errcode) {
428
- if ([40001, 40014, 42001].includes(result.errcode)) {
429
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
430
- await this.getAccessToken(true)
431
- return await this.createUrlLink(options, true)
432
- }
433
378
  throw new Error(`${result.errcode};${result.errmsg}`)
434
379
  }
435
380
  return result.url_link
436
381
  }
437
382
 
438
- async queryUrlLink(url_link, _retried = false) {
439
- const access_token = await this.getAccessToken()
440
- const url = util.format(queryUrlLinkUrl, access_token)
441
-
442
- const result = await wxAxios
443
- .post(url, {
444
- url_link,
445
- })
446
- .then((res) => res.data)
383
+ async queryUrlLink(url_link) {
384
+ const result = await this._withTokenRetry(async (access_token) => {
385
+ const url = util.format(queryUrlLinkUrl, access_token)
386
+ return await wxAxios
387
+ .post(url, {
388
+ url_link,
389
+ })
390
+ .then((res) => res.data)
391
+ })
447
392
 
448
393
  if (result.errcode) {
449
- if ([40001, 40014, 42001].includes(result.errcode)) {
450
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
451
- await this.getAccessToken(true)
452
- return await this.queryUrlLink(url_link, true)
453
- }
454
394
  if (result.errcode === 85403) {
455
395
  return {
456
396
  expire_time: 0,
@@ -461,10 +401,8 @@ module.exports = class Singleton {
461
401
  return result.url_link_info
462
402
  }
463
403
 
464
- async createShortLink(options, _retried = false) {
404
+ async createShortLink(options) {
465
405
  const { page, scene, title, ...rest } = options
466
- const access_token = await this.getAccessToken()
467
- const url = util.format(createShortLinkUrl, access_token)
468
406
  const query = options.scene
469
407
  ? Object.keys(options.scene)
470
408
  .map((item) => `${item}=${options.scene[item]}`)
@@ -477,23 +415,19 @@ module.exports = class Singleton {
477
415
  is_permanent: true,
478
416
  }
479
417
 
480
- const result = await wxAxios.post(url, payLoad).then((res) => res.data)
418
+ const result = await this._withTokenRetry(async (access_token) => {
419
+ const url = util.format(createShortLinkUrl, access_token)
420
+ return await wxAxios.post(url, payLoad).then((res) => res.data)
421
+ })
481
422
 
482
423
  if (result.errcode) {
483
- if ([40001, 40014, 42001].includes(result.errcode)) {
484
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
485
- await this.getAccessToken(true)
486
- return await this.createShortLink(options, true)
487
- }
488
424
  throw new Error(`${result.errcode};${result.errmsg}`)
489
425
  }
490
426
  return result.link
491
427
  }
492
428
 
493
- async createUrlscheme(options, _retried = false) {
429
+ async createUrlscheme(options) {
494
430
  const { page, scene, ...rest } = options
495
- const access_token = await this.getAccessToken()
496
- const url = util.format(createUrlschemeUrl, access_token)
497
431
  const payLoad = {
498
432
  ...rest,
499
433
  jump_wxa: {
@@ -506,23 +440,19 @@ module.exports = class Singleton {
506
440
  },
507
441
  }
508
442
 
509
- const result = await wxAxios.post(url, payLoad).then((res) => res.data)
443
+ const result = await this._withTokenRetry(async (access_token) => {
444
+ const url = util.format(createUrlschemeUrl, access_token)
445
+ return await wxAxios.post(url, payLoad).then((res) => res.data)
446
+ })
510
447
 
511
448
  if (result.errcode) {
512
- if ([40001, 40014, 42001].includes(result.errcode)) {
513
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
514
- await this.getAccessToken(true)
515
- return await this.createUrlscheme(options, true)
516
- }
517
449
  throw new Error(`${result.errcode};${result.errmsg}`)
518
450
  }
519
451
  return result.openlink
520
452
  }
521
453
 
522
- async createQR(options, _retried = false) {
454
+ async createQR(options) {
523
455
  if (!this.config.targetPath) throw new Error('没有配置文件夹')
524
- const access_token = await this.getAccessToken()
525
- const url = util.format(createQRCodeUrl, access_token)
526
456
  const payLoad = {
527
457
  ...options,
528
458
  scene: options.scene
@@ -533,22 +463,20 @@ module.exports = class Singleton {
533
463
  }
534
464
  const creatuuid = uuid.v1()
535
465
 
536
- const bufferResult = await wxAxios
537
- .post(url, payLoad, {
538
- responseType: 'arraybuffer',
539
- })
540
- .then((res) => {
541
- if (res.headers['content-type'].includes('json')) {
542
- return JSON.parse(Buffer.from(res.data).toString('utf8'))
543
- }
544
- return res.data
545
- })
466
+ const bufferResult = await this._withTokenRetry(async (access_token) => {
467
+ const url = util.format(createQRCodeUrl, access_token)
468
+ return await wxAxios
469
+ .post(url, payLoad, {
470
+ responseType: 'arraybuffer',
471
+ })
472
+ .then((res) => {
473
+ if (res.headers['content-type'].includes('json')) {
474
+ return JSON.parse(Buffer.from(res.data).toString('utf8'))
475
+ }
476
+ return res.data
477
+ })
478
+ })
546
479
  if (bufferResult.errcode) {
547
- if ([40001, 40014, 42001].includes(bufferResult.errcode)) {
548
- if (_retried) throw new Error(`${bufferResult.errcode};${bufferResult.errmsg}`)
549
- await this.getAccessToken(true)
550
- return await this.createQR(options, true)
551
- }
552
480
  throw new Error(bufferResult.errmsg)
553
481
  }
554
482
  await fsPromise.writeFile(
@@ -558,9 +486,7 @@ module.exports = class Singleton {
558
486
  return `${creatuuid}.png`
559
487
  }
560
488
 
561
- async createQRBuffer(options, _retried = false) {
562
- const access_token = await this.getAccessToken()
563
- const url = util.format(createQRCodeUrl, access_token)
489
+ async createQRBuffer(options) {
564
490
  const payLoad = {
565
491
  ...options,
566
492
  scene: options.scene
@@ -570,30 +496,26 @@ module.exports = class Singleton {
570
496
  : '',
571
497
  }
572
498
 
573
- const bufferResult = await wxAxios
574
- .post(url, payLoad, {
575
- responseType: 'arraybuffer',
576
- })
577
- .then((res) => {
578
- if (res.headers['content-type'].includes('json')) {
579
- return JSON.parse(Buffer.from(res.data).toString('utf8'))
580
- }
581
- return res.data
582
- })
499
+ const bufferResult = await this._withTokenRetry(async (access_token) => {
500
+ const url = util.format(createQRCodeUrl, access_token)
501
+ return await wxAxios
502
+ .post(url, payLoad, {
503
+ responseType: 'arraybuffer',
504
+ })
505
+ .then((res) => {
506
+ if (res.headers['content-type'].includes('json')) {
507
+ return JSON.parse(Buffer.from(res.data).toString('utf8'))
508
+ }
509
+ return res.data
510
+ })
511
+ })
583
512
  if (bufferResult.errcode) {
584
- if ([40001, 40014, 42001].includes(bufferResult.errcode)) {
585
- if (_retried) throw new Error(`${bufferResult.errcode};${bufferResult.errmsg}`)
586
- await this.getAccessToken(true)
587
- return await this.createQRBuffer(options, true)
588
- }
589
513
  throw new Error(bufferResult.errmsg)
590
514
  }
591
515
  return bufferResult
592
516
  }
593
517
 
594
- async sendMessage(options, _retried = false) {
595
- const access_token = await this.getAccessToken()
596
- const url = util.format(sendMessageUrl, access_token)
518
+ async sendMessage(options) {
597
519
  const formatOptions = {
598
520
  ...options,
599
521
  data: lodash.mapValues(options.data, (item) => {
@@ -602,411 +524,323 @@ module.exports = class Singleton {
602
524
  }
603
525
  }),
604
526
  }
605
- const result = await wxAxios.post(url, formatOptions).then((res) => res.data)
527
+ const result = await this._withTokenRetry(async (access_token) => {
528
+ const url = util.format(sendMessageUrl, access_token)
529
+ return await wxAxios.post(url, formatOptions).then((res) => res.data)
530
+ })
606
531
  if (result.errcode) {
607
- if ([40001, 40014, 42001].includes(result.errcode)) {
608
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
609
- await this.getAccessToken(true)
610
- return await this.sendMessage(options, true)
611
- }
612
532
  throw new Error(`${result.errcode};${result.errmsg}`)
613
533
  }
614
534
  return result
615
535
  }
616
536
 
617
- async handleMessage(options, _retried = false) {
618
- const access_token = await this.getAccessToken()
619
- const url = util.format(handleMessageUrl, access_token)
620
- const result = await wxAxios.post(url, options).then((res) => res.data)
537
+ async handleMessage(options) {
538
+ const result = await this._withTokenRetry(async (access_token) => {
539
+ const url = util.format(handleMessageUrl, access_token)
540
+ return await wxAxios.post(url, options).then((res) => res.data)
541
+ })
621
542
  if (result.errcode) {
622
- if ([40001, 40014, 42001].includes(result.errcode)) {
623
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
624
- await this.getAccessToken(true)
625
- return await this.handleMessage(options, true)
626
- }
627
543
  throw new Error(`${result.errcode};${result.errmsg}`)
628
544
  }
629
545
  return result
630
546
  }
631
547
 
632
- async handleTyping({ touser, is_typing = true }, _retried = false) {
548
+ async handleTyping({ touser, is_typing = true }) {
633
549
  const options = {
634
550
  touser,
635
551
  command: is_typing ? 'Typing' : 'CancelTyping',
636
552
  }
637
- const access_token = await this.getAccessToken()
638
- const url = util.format(handleTypingUrl, access_token)
639
- const result = await wxAxios.post(url, options).then((res) => res.data)
553
+ const result = await this._withTokenRetry(async (access_token) => {
554
+ const url = util.format(handleTypingUrl, access_token)
555
+ return await wxAxios.post(url, options).then((res) => res.data)
556
+ })
640
557
  if (result.errcode) {
641
- if ([40001, 40014, 42001].includes(result.errcode)) {
642
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
643
- await this.getAccessToken(true)
644
- return await this.handleTyping({ touser, is_typing }, true)
645
- }
646
558
  throw new Error(`${result.errcode};${result.errmsg}`)
647
559
  }
648
560
  return result
649
561
  }
650
562
 
651
- async uploadFile(imgUrl, _retried = false) {
563
+ async uploadFile(imgUrl) {
652
564
  if (!imgUrl || !imgUrl.includes('//')) throw new Error('检查imgUrl是否链接')
653
565
 
654
- const access_token = await this.getAccessToken()
655
- const url = util.format(uploadFile, access_token, 'image')
656
566
  const fullUrl = imgUrl.startsWith('//') ? `http:${imgUrl}` : imgUrl
657
- const stream = (await wxAxios.get(fullUrl, { responseType: 'stream' })).data
658
- const formData = new FormData()
659
- formData.append('media', stream)
660
- const uploadResult = await uploadPromise(url, formData)
567
+ const uploadResult = await this._withTokenRetry(async (access_token) => {
568
+ const url = util.format(uploadFile, access_token, 'image')
569
+ const stream = (await wxAxios.get(fullUrl, { responseType: 'stream' }))
570
+ .data
571
+ const formData = new FormData()
572
+ formData.append('media', stream)
573
+ return await uploadPromise(url, formData)
574
+ })
661
575
  if (uploadResult.errcode) {
662
- if ([40001, 40014, 42001].includes(uploadResult.errcode)) {
663
- if (_retried) throw new Error(`${uploadResult.errcode};${uploadResult.errmsg}`)
664
- await this.getAccessToken(true)
665
- return await this.uploadFile(imgUrl, true)
666
- }
667
576
  throw new Error(`${uploadResult.errcode};${uploadResult.errmsg}`)
668
577
  }
669
578
  return uploadResult.media_id
670
579
  }
671
580
 
672
- async invokeService(payLoad, _retried = false) {
673
- const access_token = await this.getAccessToken()
674
- const url = util.format(serviceMarketUrl, access_token)
675
- const result = await wxAxios
676
- .post(url, {
677
- ...payLoad,
678
- })
679
- .then((res) => res.data)
581
+ async invokeService(payLoad) {
582
+ const result = await this._withTokenRetry(async (access_token) => {
583
+ const url = util.format(serviceMarketUrl, access_token)
584
+ return await wxAxios
585
+ .post(url, {
586
+ ...payLoad,
587
+ })
588
+ .then((res) => res.data)
589
+ })
680
590
  if (result.errcode) {
681
- if ([40001, 40014, 42001].includes(result.errcode)) {
682
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
683
- await this.getAccessToken(true)
684
- return await this.invokeService(payLoad, true)
685
- }
686
591
  throw new Error(`${result.errcode};${result.errmsg}`)
687
592
  }
688
593
  return result.data
689
594
  }
690
595
 
691
- async createOrder(payLoad, _retried = false) {
692
- const access_token = await this.getAccessToken()
693
- const url = util.format(createOrderUrl, access_token)
694
- const result = await wxAxios
695
- .post(url, {
696
- ...payLoad,
697
- })
698
- .then((res) => res.data)
596
+ async createOrder(payLoad) {
597
+ const result = await this._withTokenRetry(async (access_token) => {
598
+ const url = util.format(createOrderUrl, access_token)
599
+ return await wxAxios
600
+ .post(url, {
601
+ ...payLoad,
602
+ })
603
+ .then((res) => res.data)
604
+ })
699
605
 
700
606
  if (result.errcode) {
701
- if ([40001, 40014, 42001].includes(result.errcode)) {
702
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
703
- await this.getAccessToken(true)
704
- return await this.createOrder(payLoad, true)
705
- }
706
607
  throw new Error(`${result.errcode};${result.errmsg}`)
707
608
  }
708
609
  return result.payment_params
709
610
  }
710
611
 
711
- async getOrder(payLoad, _retried = false) {
712
- const access_token = await this.getAccessToken()
713
- const url = util.format(getOrderUrl, access_token)
714
- const result = await wxAxios
715
- .post(url, {
716
- ...payLoad,
717
- })
718
- .then((res) => res.data)
612
+ async getOrder(payLoad) {
613
+ const result = await this._withTokenRetry(async (access_token) => {
614
+ const url = util.format(getOrderUrl, access_token)
615
+ return await wxAxios
616
+ .post(url, {
617
+ ...payLoad,
618
+ })
619
+ .then((res) => res.data)
620
+ })
719
621
 
720
622
  if (result.errcode) {
721
- if ([40001, 40014, 42001].includes(result.errcode)) {
722
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
723
- await this.getAccessToken(true)
724
- return await this.getOrder(payLoad, true)
725
- }
726
623
  throw new Error(`${result.errcode};${result.errmsg}`)
727
624
  }
728
625
  return result.order
729
626
  }
730
627
 
731
- async refundOrder(payLoad, _retried = false) {
732
- const access_token = await this.getAccessToken()
733
- const url = util.format(refundOrderUrl, access_token)
734
- const result = await wxAxios
735
- .post(url, {
736
- ...payLoad,
737
- })
738
- .then((res) => res.data)
628
+ async refundOrder(payLoad) {
629
+ const result = await this._withTokenRetry(async (access_token) => {
630
+ const url = util.format(refundOrderUrl, access_token)
631
+ return await wxAxios
632
+ .post(url, {
633
+ ...payLoad,
634
+ })
635
+ .then((res) => res.data)
636
+ })
739
637
 
740
638
  if (result.errcode) {
741
- if ([40001, 40014, 42001].includes(result.errcode)) {
742
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
743
- await this.getAccessToken(true)
744
- return await this.refundOrder(payLoad, true)
745
- }
746
639
  throw new Error(`${result.errcode};${result.errmsg}`)
747
640
  }
748
641
  return result.data
749
642
  }
750
643
 
751
- async addGood(payLoad, _retried = false) {
752
- const access_token = await this.getAccessToken()
753
- const url = util.format(addGoodUrl, access_token)
754
- const result = await wxAxios
755
- .post(url, {
756
- ...payLoad,
757
- })
758
- .then((res) => res.data)
644
+ async addGood(payLoad) {
645
+ const result = await this._withTokenRetry(async (access_token) => {
646
+ const url = util.format(addGoodUrl, access_token)
647
+ return await wxAxios
648
+ .post(url, {
649
+ ...payLoad,
650
+ })
651
+ .then((res) => res.data)
652
+ })
759
653
  if (result.errcode) {
760
- if ([40001, 40014, 42001].includes(result.errcode)) {
761
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
762
- await this.getAccessToken(true)
763
- return await this.addGood(payLoad, true)
764
- }
765
654
  // return payLoad
766
655
  throw new Error(`${result.errcode};${result.errmsg}`)
767
656
  }
768
657
  return result.data
769
658
  }
770
659
 
771
- async getCategory(payLoad, _retried = false) {
772
- const access_token = await this.getAccessToken()
773
- const url = util.format(getCategoryUrl, access_token)
774
-
775
- const result = await wxAxios
776
- .post(url, {
777
- ...payLoad,
778
- })
779
- .then((res) => res.data)
660
+ async getCategory(payLoad) {
661
+ const result = await this._withTokenRetry(async (access_token) => {
662
+ const url = util.format(getCategoryUrl, access_token)
663
+ return await wxAxios
664
+ .post(url, {
665
+ ...payLoad,
666
+ })
667
+ .then((res) => res.data)
668
+ })
780
669
  if (result.errcode) {
781
- if ([40001, 40014, 42001].includes(result.errcode)) {
782
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
783
- await this.getAccessToken(true)
784
- return await this.getCategory(payLoad, true)
785
- }
786
670
  throw new Error(`${result.errcode};${result.errmsg}`)
787
671
  }
788
672
  return result
789
673
  }
790
674
 
791
- async getBrandList(payLoad, _retried = false) {
792
- const access_token = await this.getAccessToken()
793
- const url = util.format(getBrandListUrl, access_token)
794
-
795
- const result = await wxAxios
796
- .post(url, {
797
- ...payLoad,
798
- })
799
- .then((res) => res.data)
675
+ async getBrandList(payLoad) {
676
+ const result = await this._withTokenRetry(async (access_token) => {
677
+ const url = util.format(getBrandListUrl, access_token)
678
+ return await wxAxios
679
+ .post(url, {
680
+ ...payLoad,
681
+ })
682
+ .then((res) => res.data)
683
+ })
800
684
  if (result.errcode) {
801
- if ([40001, 40014, 42001].includes(result.errcode)) {
802
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
803
- await this.getAccessToken(true)
804
- return await this.getBrandList(payLoad, true)
805
- }
806
685
  throw new Error(`${result.errcode};${result.errmsg}`)
807
686
  }
808
687
  return result.data
809
688
  }
810
689
 
811
- async getShipTemplate(payLoad, _retried = false) {
812
- const access_token = await this.getAccessToken()
813
- const url = util.format(getShipTemplateUrl, access_token)
814
-
815
- const result = await wxAxios
816
- .post(url, {
817
- ...payLoad,
818
- })
819
- .then((res) => res.data)
690
+ async getShipTemplate(payLoad) {
691
+ const result = await this._withTokenRetry(async (access_token) => {
692
+ const url = util.format(getShipTemplateUrl, access_token)
693
+ return await wxAxios
694
+ .post(url, {
695
+ ...payLoad,
696
+ })
697
+ .then((res) => res.data)
698
+ })
820
699
  if (result.errcode) {
821
- if ([40001, 40014, 42001].includes(result.errcode)) {
822
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
823
- await this.getAccessToken(true)
824
- return await this.getShipTemplate(payLoad, true)
825
- }
826
700
  throw new Error(`${result.errcode};${result.errmsg}`)
827
701
  }
828
702
  return result
829
703
  }
830
704
 
831
- async uploadShip(payLoad, _retried = false) {
832
- const access_token = await this.getAccessToken()
833
- const url = util.format(uploadShipUrl, access_token)
834
-
835
- const result = await wxAxios
836
- .post(url, {
837
- ...payLoad,
838
- })
839
- .then((res) => res.data)
705
+ async uploadShip(payLoad) {
706
+ const result = await this._withTokenRetry(async (access_token) => {
707
+ const url = util.format(uploadShipUrl, access_token)
708
+ return await wxAxios
709
+ .post(url, {
710
+ ...payLoad,
711
+ })
712
+ .then((res) => res.data)
713
+ })
840
714
  if (result.errcode) {
841
- if ([40001, 40014, 42001].includes(result.errcode)) {
842
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
843
- await this.getAccessToken(true)
844
- return await this.uploadShip(payLoad, true)
845
- }
846
715
  throw new Error(`${result.errcode};${result.errmsg}`)
847
716
  }
848
717
  return result
849
718
  }
850
719
 
851
- async uploadPreShip(payLoad, _retried = false) {
852
- const access_token = await this.getAccessToken()
853
- const url = util.format(uploadPreShipUrl, access_token)
854
-
855
- const result = await wxAxios
856
- .post(url, {
857
- ...payLoad,
858
- })
859
- .then((res) => res.data)
720
+ async uploadPreShip(payLoad) {
721
+ const result = await this._withTokenRetry(async (access_token) => {
722
+ const url = util.format(uploadPreShipUrl, access_token)
723
+ return await wxAxios
724
+ .post(url, {
725
+ ...payLoad,
726
+ })
727
+ .then((res) => res.data)
728
+ })
860
729
  if (result.errcode) {
861
- if ([40001, 40014, 42001].includes(result.errcode)) {
862
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
863
- await this.getAccessToken(true)
864
- return await this.uploadPreShip(payLoad, true)
865
- }
866
730
  throw new Error(`${result.errcode};${result.errmsg}`)
867
731
  }
868
732
  return result
869
733
  }
870
734
 
871
- async getShipOrderList(payLoad, _retried = false) {
872
- const access_token = await this.getAccessToken()
873
- const url = util.format(getShipOrderListUrl, access_token)
874
-
875
- const result = await wxAxios
876
- .post(url, {
877
- ...payLoad,
878
- })
879
- .then((res) => res.data)
735
+ async getShipOrderList(payLoad) {
736
+ const result = await this._withTokenRetry(async (access_token) => {
737
+ const url = util.format(getShipOrderListUrl, access_token)
738
+ return await wxAxios
739
+ .post(url, {
740
+ ...payLoad,
741
+ })
742
+ .then((res) => res.data)
743
+ })
880
744
  if (result.errcode) {
881
- if ([40001, 40014, 42001].includes(result.errcode)) {
882
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
883
- await this.getAccessToken(true)
884
- return await this.getShipOrderList(payLoad, true)
885
- }
886
745
  throw new Error(`${result.errcode};${result.errmsg}`)
887
746
  }
888
747
  return result
889
748
  }
890
749
 
891
- async uploadImage(payLoad, _retried = false) {
892
- const access_token = await this.getAccessToken()
750
+ async uploadImage(payLoad) {
893
751
  const { upload_type, resp_type, ...rest } = payLoad
894
752
 
895
- const url = util.format(
896
- uploadImageUrl,
897
- access_token,
898
- upload_type,
899
- resp_type
900
- )
901
-
902
- const result = await wxAxios
903
- .post(url, {
904
- ...rest,
905
- })
906
- .then((res) => res.data)
753
+ const result = await this._withTokenRetry(async (access_token) => {
754
+ const url = util.format(
755
+ uploadImageUrl,
756
+ access_token,
757
+ upload_type,
758
+ resp_type
759
+ )
760
+ return await wxAxios
761
+ .post(url, {
762
+ ...rest,
763
+ })
764
+ .then((res) => res.data)
765
+ })
907
766
 
908
767
  if (result.errcode) {
909
- if ([40001, 40014, 42001].includes(result.errcode)) {
910
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
911
- await this.getAccessToken(true)
912
- return await this.uploadImage(payLoad, true)
913
- }
914
768
  throw new Error(`${result.errcode};${result.errmsg}`)
915
769
  }
916
770
  return result.pic_file
917
771
  }
918
772
 
919
- async setMsgJumpPath(payLoad, _retried = false) {
920
- const access_token = await this.getAccessToken()
921
- const url = util.format(setMsgJumpPathUrl, access_token)
922
-
923
- const result = await wxAxios
924
- .post(url, {
925
- ...payLoad,
926
- })
927
- .then((res) => res.data)
773
+ async setMsgJumpPath(payLoad) {
774
+ const result = await this._withTokenRetry(async (access_token) => {
775
+ const url = util.format(setMsgJumpPathUrl, access_token)
776
+ return await wxAxios
777
+ .post(url, {
778
+ ...payLoad,
779
+ })
780
+ .then((res) => res.data)
781
+ })
928
782
  if (result.errcode) {
929
- if ([40001, 40014, 42001].includes(result.errcode)) {
930
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
931
- await this.getAccessToken(true)
932
- return await this.setMsgJumpPath(payLoad, true)
933
- }
934
783
  throw new Error(`${result.errcode};${result.errmsg}`)
935
784
  }
936
785
  return result
937
786
  }
938
787
 
939
- async getDeliveryList(payLoad, _retried = false) {
940
- const access_token = await this.getAccessToken()
941
- const url = util.format(getDeliveryListUrl, access_token)
942
-
943
- const result = await wxAxios
944
- .post(url, {
945
- ...payLoad,
946
- })
947
- .then((res) => res.data)
788
+ async getDeliveryList(payLoad) {
789
+ const result = await this._withTokenRetry(async (access_token) => {
790
+ const url = util.format(getDeliveryListUrl, access_token)
791
+ return await wxAxios
792
+ .post(url, {
793
+ ...payLoad,
794
+ })
795
+ .then((res) => res.data)
796
+ })
948
797
  if (result.errcode) {
949
- if ([40001, 40014, 42001].includes(result.errcode)) {
950
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
951
- await this.getAccessToken(true)
952
- return await this.getDeliveryList(payLoad, true)
953
- }
954
798
  throw new Error(`${result.errcode};${result.errmsg}`)
955
799
  }
956
800
  return result
957
801
  }
958
802
 
959
- async getUserPortrait(payLoad, _retried = false) {
960
- const access_token = await this.getAccessToken()
961
- const url = util.format(getUserPortraitUrl, access_token)
962
-
963
- const result = await wxAxios
964
- .post(url, {
965
- ...payLoad,
966
- })
967
- .then((res) => res.data)
803
+ async getUserPortrait(payLoad) {
804
+ const result = await this._withTokenRetry(async (access_token) => {
805
+ const url = util.format(getUserPortraitUrl, access_token)
806
+ return await wxAxios
807
+ .post(url, {
808
+ ...payLoad,
809
+ })
810
+ .then((res) => res.data)
811
+ })
968
812
  if (result.errcode) {
969
- if ([40001, 40014, 42001].includes(result.errcode)) {
970
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
971
- await this.getAccessToken(true)
972
- return await this.getUserPortrait(payLoad, true)
973
- }
974
813
  throw new Error(`${result.errcode};${result.errmsg}`)
975
814
  }
976
815
  return result
977
816
  }
978
817
 
979
- async getVisitTrend(payLoad, _retried = false) {
980
- const access_token = await this.getAccessToken()
981
- let url = ''
982
- if (payLoad.type === 'month') {
983
- url = util.format(
984
- 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=%s',
985
- access_token
986
- )
987
- } else if (payLoad.type === 'week') {
988
- url = util.format(
989
- 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=%s',
990
- access_token
991
- )
992
- } else if (payLoad.type === 'day') {
993
- url = util.format(
994
- 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=%s',
995
- access_token
996
- )
997
- }
998
-
999
- const result = await wxAxios
1000
- .post(url, {
1001
- ...payLoad,
1002
- })
1003
- .then((res) => res.data)
1004
- if (result.errcode) {
1005
- if ([40001, 40014, 42001].includes(result.errcode)) {
1006
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1007
- await this.getAccessToken(true)
1008
- return await this.getVisitTrend(payLoad, true)
818
+ async getVisitTrend(payLoad) {
819
+ const result = await this._withTokenRetry(async (access_token) => {
820
+ let url = ''
821
+ if (payLoad.type === 'month') {
822
+ url = util.format(
823
+ 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=%s',
824
+ access_token
825
+ )
826
+ } else if (payLoad.type === 'week') {
827
+ url = util.format(
828
+ 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=%s',
829
+ access_token
830
+ )
831
+ } else if (payLoad.type === 'day') {
832
+ url = util.format(
833
+ 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=%s',
834
+ access_token
835
+ )
1009
836
  }
837
+ return await wxAxios
838
+ .post(url, {
839
+ ...payLoad,
840
+ })
841
+ .then((res) => res.data)
842
+ })
843
+ if (result.errcode) {
1010
844
  if (result.errcode === 61503) {
1011
845
  return null
1012
846
  }
@@ -1015,17 +849,12 @@ module.exports = class Singleton {
1015
849
  return result
1016
850
  }
1017
851
 
1018
- async getCommentInfo(commentId, _retried = false) {
1019
- const access_token = await this.getAccessToken()
1020
- const url = util.format(getCommentInfoUrl, commentId, access_token)
1021
-
1022
- const result = await wxAxios.get(url).then((res) => res.data)
852
+ async getCommentInfo(commentId) {
853
+ const result = await this._withTokenRetry(async (access_token) => {
854
+ const url = util.format(getCommentInfoUrl, commentId, access_token)
855
+ return await wxAxios.get(url).then((res) => res.data)
856
+ })
1023
857
  if (result.errcode) {
1024
- if ([40001, 40014, 42001].includes(result.errcode)) {
1025
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1026
- await this.getAccessToken(true)
1027
- return await this.getCommentInfo(commentId, true)
1028
- }
1029
858
  if (result.errcode === 61503) {
1030
859
  return null
1031
860
  }
@@ -1034,23 +863,18 @@ module.exports = class Singleton {
1034
863
  return result
1035
864
  }
1036
865
 
1037
- async createActivity(_retried = false) {
1038
- const access_token = await this.getAccessToken()
1039
- const url = util.format(createActivityUrl, access_token)
1040
-
1041
- const result = await wxAxios.get(url).then((res) => res.data)
866
+ async createActivity() {
867
+ const result = await this._withTokenRetry(async (access_token) => {
868
+ const url = util.format(createActivityUrl, access_token)
869
+ return await wxAxios.get(url).then((res) => res.data)
870
+ })
1042
871
  if (result.errcode) {
1043
- if ([40001, 40014, 42001].includes(result.errcode)) {
1044
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1045
- await this.getAccessToken(true)
1046
- return await this.createActivity(true)
1047
- }
1048
872
  throw new Error(`${result.errcode};${result.errmsg}`)
1049
873
  }
1050
874
  return result
1051
875
  }
1052
876
 
1053
- async setUpdatableMsg(target, _retried = false) {
877
+ async setUpdatableMsg(target) {
1054
878
  let {
1055
879
  activity_id,
1056
880
  target_state,
@@ -1069,8 +893,6 @@ module.exports = class Singleton {
1069
893
  version_type = ''
1070
894
  path = ''
1071
895
  }
1072
- const access_token = await this.getAccessToken()
1073
- const url = util.format(setUpdatableMsgUrl, access_token)
1074
896
  const postData = {
1075
897
  activity_id,
1076
898
  target_state,
@@ -1106,13 +928,11 @@ module.exports = class Singleton {
1106
928
  if (!postData.template_info.parameter_list.length) {
1107
929
  throw new ServiceError('?parameter_list length 0')
1108
930
  }
1109
- const result = await wxAxios.post(url, postData).then((res) => res.data)
931
+ const result = await this._withTokenRetry(async (access_token) => {
932
+ const url = util.format(setUpdatableMsgUrl, access_token)
933
+ return await wxAxios.post(url, postData).then((res) => res.data)
934
+ })
1110
935
  if (result.errcode) {
1111
- if ([40001, 40014, 42001].includes(result.errcode)) {
1112
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1113
- await this.getAccessToken(true)
1114
- return await this.setUpdatableMsg(target, true)
1115
- }
1116
936
  throw new Error(`${result.errcode};${result.errmsg}`)
1117
937
  }
1118
938
  return result
@@ -1149,11 +969,9 @@ module.exports = class Singleton {
1149
969
  }
1150
970
  }
1151
971
 
1152
- async b2bRefund({ mchid, app_key, data }, _retried = false) {
972
+ async b2bRefund({ mchid, app_key, data }) {
1153
973
  const requestUrl = b2bRefundUrl
1154
974
 
1155
- const access_token = await this.getAccessToken()
1156
-
1157
975
  const uri = requestUrl.split('api.weixin.qq.com')[1].split('?')[0]
1158
976
 
1159
977
  const signData = {
@@ -1163,24 +981,19 @@ module.exports = class Singleton {
1163
981
 
1164
982
  const pay_sig = hmacSha256(`${uri}&${JSON.stringify(signData)}`, app_key)
1165
983
 
1166
- const url = util.format(requestUrl, access_token, pay_sig)
1167
- const result = await wxAxios.post(url, signData).then((res) => res.data)
984
+ const result = await this._withTokenRetry(async (access_token) => {
985
+ const url = util.format(requestUrl, access_token, pay_sig)
986
+ return await wxAxios.post(url, signData).then((res) => res.data)
987
+ })
1168
988
  if (result.errcode) {
1169
- if ([40001, 40014, 42001].includes(result.errcode)) {
1170
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1171
- await this.getAccessToken(true)
1172
- return await this.b2bRefund({ mchid, app_key, data }, true)
1173
- }
1174
989
  throw new Error(`${result.errcode};${result.errmsg}`)
1175
990
  }
1176
991
  return result
1177
992
  }
1178
993
 
1179
- async b2bCheckRefund({ mchid, app_key, out_refund_no }, _retried = false) {
994
+ async b2bCheckRefund({ mchid, app_key, out_refund_no }) {
1180
995
  const requestUrl = b2bCheckRefundUrl
1181
996
 
1182
- const access_token = await this.getAccessToken()
1183
-
1184
997
  const uri = requestUrl.split('api.weixin.qq.com')[1].split('?')[0]
1185
998
 
1186
999
  const signData = {
@@ -1190,24 +1003,19 @@ module.exports = class Singleton {
1190
1003
 
1191
1004
  const pay_sig = hmacSha256(`${uri}&${JSON.stringify(signData)}`, app_key)
1192
1005
 
1193
- const url = util.format(requestUrl, access_token, pay_sig)
1194
- const result = await wxAxios.post(url, signData).then((res) => res.data)
1006
+ const result = await this._withTokenRetry(async (access_token) => {
1007
+ const url = util.format(requestUrl, access_token, pay_sig)
1008
+ return await wxAxios.post(url, signData).then((res) => res.data)
1009
+ })
1195
1010
  if (result.errcode) {
1196
- if ([40001, 40014, 42001].includes(result.errcode)) {
1197
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1198
- await this.getAccessToken(true)
1199
- return await this.b2bCheckRefund({ mchid, app_key, out_refund_no }, true)
1200
- }
1201
1011
  throw new Error(`${result.errcode};${result.errmsg}`)
1202
1012
  }
1203
1013
  return result
1204
1014
  }
1205
1015
 
1206
- async b2bCheckOrder({ mchid, app_key, out_trade_no }, _retried = false) {
1016
+ async b2bCheckOrder({ mchid, app_key, out_trade_no }) {
1207
1017
  const requestUrl = b2bCheckOrderUrl
1208
1018
 
1209
- const access_token = await this.getAccessToken()
1210
-
1211
1019
  const uri = requestUrl.split('api.weixin.qq.com')[1].split('?')[0]
1212
1020
 
1213
1021
  const signData = {
@@ -1217,44 +1025,34 @@ module.exports = class Singleton {
1217
1025
 
1218
1026
  const pay_sig = hmacSha256(`${uri}&${JSON.stringify(signData)}`, app_key)
1219
1027
 
1220
- const url = util.format(requestUrl, access_token, pay_sig)
1221
- const result = await wxAxios.post(url, signData).then((res) => res.data)
1028
+ const result = await this._withTokenRetry(async (access_token) => {
1029
+ const url = util.format(requestUrl, access_token, pay_sig)
1030
+ return await wxAxios.post(url, signData).then((res) => res.data)
1031
+ })
1222
1032
  if (result.errcode) {
1223
- if ([40001, 40014, 42001].includes(result.errcode)) {
1224
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1225
- await this.getAccessToken(true)
1226
- return await this.b2bCheckOrder({ mchid, app_key, out_trade_no }, true)
1227
- }
1228
1033
  throw new Error(`${result.errcode};${result.errmsg}`)
1229
1034
  }
1230
1035
  return result
1231
1036
  }
1232
1037
 
1233
- async b2bPreStore(list, _retried = false) {
1234
- const access_token = await this.getAccessToken()
1235
- const url = util.format(b2bPreStoreUrl, access_token)
1236
-
1237
- const result = await wxAxios
1238
- .post(url, {
1239
- retail_info_list: list,
1240
- })
1241
- .then((res) => res.data)
1038
+ async b2bPreStore(list) {
1039
+ const result = await this._withTokenRetry(async (access_token) => {
1040
+ const url = util.format(b2bPreStoreUrl, access_token)
1041
+ return await wxAxios
1042
+ .post(url, {
1043
+ retail_info_list: list,
1044
+ })
1045
+ .then((res) => res.data)
1046
+ })
1242
1047
  if (result.errcode) {
1243
- if ([40001, 40014, 42001].includes(result.errcode)) {
1244
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1245
- await this.getAccessToken(true)
1246
- return await this.b2bPreStore(list, true)
1247
- }
1248
1048
  throw new Error(`${result.errcode};${result.errmsg}`)
1249
1049
  }
1250
1050
  return result
1251
1051
  }
1252
1052
 
1253
- async b2bFinish({ mchid, app_key, out_trade_no }, _retried = false) {
1053
+ async b2bFinish({ mchid, app_key, out_trade_no }) {
1254
1054
  const requestUrl = b2bFinishUrl
1255
1055
 
1256
- const access_token = await this.getAccessToken()
1257
-
1258
1056
  const uri = requestUrl.split('api.weixin.qq.com')[1].split('?')[0]
1259
1057
 
1260
1058
  const signData = {
@@ -1264,36 +1062,28 @@ module.exports = class Singleton {
1264
1062
 
1265
1063
  const pay_sig = hmacSha256(`${uri}&${JSON.stringify(signData)}`, app_key)
1266
1064
 
1267
- const url = util.format(requestUrl, access_token, pay_sig)
1268
- const result = await wxAxios.post(url, signData).then((res) => res.data)
1065
+ const result = await this._withTokenRetry(async (access_token) => {
1066
+ const url = util.format(requestUrl, access_token, pay_sig)
1067
+ return await wxAxios.post(url, signData).then((res) => res.data)
1068
+ })
1269
1069
  if (result.errcode) {
1270
- if ([40001, 40014, 42001].includes(result.errcode)) {
1271
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1272
- await this.getAccessToken(true)
1273
- return await this.b2bFinish({ mchid, app_key, out_trade_no }, true)
1274
- }
1275
1070
  throw new Error(`${result.errcode};${result.errmsg}`)
1276
1071
  }
1277
1072
  return result
1278
1073
  }
1279
1074
 
1280
- async b2bSendMessage({ type = 2, to_user_list = [], data = {} }, _retried = false) {
1281
- const access_token = await this.getAccessToken()
1282
- const url = util.format(b2bSendMessageUrl, access_token)
1283
-
1284
- const result = await wxAxios
1285
- .post(url, {
1286
- type,
1287
- to_user_list,
1288
- content: JSON.stringify(data),
1289
- })
1290
- .then((res) => res.data)
1075
+ async b2bSendMessage({ type = 2, to_user_list = [], data = {} }) {
1076
+ const result = await this._withTokenRetry(async (access_token) => {
1077
+ const url = util.format(b2bSendMessageUrl, access_token)
1078
+ return await wxAxios
1079
+ .post(url, {
1080
+ type,
1081
+ to_user_list,
1082
+ content: JSON.stringify(data),
1083
+ })
1084
+ .then((res) => res.data)
1085
+ })
1291
1086
  if (result.errcode) {
1292
- if ([40001, 40014, 42001].includes(result.errcode)) {
1293
- if (_retried) throw new Error(`${result.errcode};${result.errmsg}`)
1294
- await this.getAccessToken(true)
1295
- return await this.b2bSendMessage({ type, to_user_list, data }, true)
1296
- }
1297
1087
  throw new Error(`${result.errcode};${result.errmsg}`)
1298
1088
  }
1299
1089
  return result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.8.54",
3
+ "version": "13.8.55",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {