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