q-koa 10.5.5 → 10.6.1

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.
@@ -11,6 +11,8 @@ const WXBizDataCrypt = require('../../services/weixinCrypt')
11
11
  const weixinArticle = require('../../services/weixinArticle')
12
12
  const fxp = require('fast-xml-parser')
13
13
  const crypto = require('crypto')
14
+ const path = require('path')
15
+ const fsPromise = require('fs/promises')
14
16
  const { getAppConfig } = require('../../utils')
15
17
 
16
18
  const check = ({ timestamp, nonce, signature, token }) => {
@@ -1548,129 +1550,64 @@ exports.messagePush = async (ctx) => {
1548
1550
  exports.profitsharing = async (ctx) => {
1549
1551
  const { app, appName } = getAppByCtx(ctx)
1550
1552
  const {
1551
- transaction_id = '',
1552
- out_order_no = '',
1553
- type = 'PERSONAL_OPENID',
1554
- amount = 0,
1555
- openid = '',
1556
- description = '手续费',
1557
- config = 'weixin_mp',
1558
- pay_config = 'weixin_pay',
1553
+ transaction_id,
1554
+ out_order_no,
1555
+ type,
1556
+ amount,
1557
+ openid,
1558
+ description,
1559
+ config,
1560
+ pay_config,
1559
1561
  } = ctx.request.body
1562
+
1560
1563
  if (!amount) return ctx.ERROR('?amount分')
1561
1564
  if (!transaction_id) return ctx.ERROR('?transaction_id')
1562
1565
  if (!out_order_no) return ctx.ERROR('?out_order_no')
1563
1566
  if (!openid) return ctx.ERROR('?openid')
1564
1567
 
1565
- const appConfig = getConfig(app)
1566
-
1567
- const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
1568
-
1569
- const { is_dev, site_host } = await appConfig.getObject('base')
1570
- // 公众号支付用 服务号 appid,其他用小程序 appid
1571
- const app_id = (await appConfig.getObject(config)).app_id
1572
-
1573
- const wxpay = WXPay({
1574
- appid: app_id,
1575
- mch_id: mchId,
1576
- partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
1577
- pfx: await fs.readFileSync(
1578
- path.resolve(
1579
- __dirname,
1580
- `${process.cwd()}/app/${appName}/plugins/weixin/${key}/apiclient_cert.p12`
1581
- )
1582
- ),
1583
- })
1584
-
1585
- const res = await wxpay.profitsharingSync({
1568
+ const res = await app.service.weixin.profitsharing({
1569
+ ctx,
1586
1570
  transaction_id,
1587
1571
  out_order_no,
1588
- receivers: [
1589
- {
1590
- type,
1591
- account: openid,
1592
- description,
1593
- amount,
1594
- },
1595
- ],
1572
+ type,
1573
+ openid,
1574
+ description,
1575
+ amount,
1576
+ config,
1577
+ pay_config,
1596
1578
  })
1597
1579
  return ctx.SUCCESS(res)
1598
1580
  }
1599
1581
 
1600
1582
  exports.addreceiver = async (ctx) => {
1601
1583
  const { app, appName } = getAppByCtx(ctx)
1602
- const {
1603
- type = 'PERSONAL_OPENID',
1604
- openid = '',
1605
- relation_type = 'USER',
1606
- config = 'weixin_mp',
1607
- pay_config = 'weixin_pay',
1608
- } = ctx.request.body
1584
+ const { type, openid, relation_type, config, pay_config } = ctx.request.body
1609
1585
 
1610
1586
  if (!openid) return ctx.ERROR('?openid')
1611
1587
 
1612
- const appConfig = getConfig(app)
1613
-
1614
- const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
1615
-
1616
- const app_id = (await appConfig.getObject(config)).app_id
1617
-
1618
- const wxpay = WXPay({
1619
- appid: app_id,
1620
- mch_id: mchId,
1621
- partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
1622
- pfx: await fs.readFileSync(
1623
- path.resolve(
1624
- __dirname,
1625
- `${process.cwd()}/app/${appName}/plugins/weixin/${key}/apiclient_cert.p12`
1626
- )
1627
- ),
1628
- })
1629
-
1630
- const res = await wxpay.addreceiverSync({
1631
- receiver: {
1632
- type,
1633
- account: openid,
1634
- relation_type,
1635
- },
1588
+ const res = await app.service.weixin.addreceiver({
1589
+ ctx,
1590
+ type,
1591
+ openid,
1592
+ relation_type,
1593
+ config,
1594
+ pay_config,
1636
1595
  })
1637
1596
  return ctx.SUCCESS(res)
1638
1597
  }
1639
1598
 
1640
1599
  exports.removereceiver = async (ctx) => {
1641
1600
  const { app, appName } = getAppByCtx(ctx)
1642
- const {
1643
- type = 'PERSONAL_OPENID',
1644
- openid = '',
1645
- config = 'weixin_mp',
1646
- pay_config = 'weixin_pay',
1647
- } = ctx.request.body
1601
+ const { type, openid, config, pay_config } = ctx.request.body
1648
1602
 
1649
1603
  if (!openid) return ctx.ERROR('?openid')
1650
1604
 
1651
- const appConfig = getConfig(app)
1652
-
1653
- const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
1654
-
1655
- const app_id = (await appConfig.getObject(config)).app_id
1656
-
1657
- const wxpay = WXPay({
1658
- appid: app_id,
1659
- mch_id: mchId,
1660
- partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
1661
- pfx: await fs.readFileSync(
1662
- path.resolve(
1663
- __dirname,
1664
- `${process.cwd()}/app/${appName}/plugins/weixin/${key}/apiclient_cert.p12`
1665
- )
1666
- ),
1667
- })
1668
-
1669
- const res = await wxpay.removereceiverSync({
1670
- receiver: {
1671
- type,
1672
- account: openid,
1673
- },
1605
+ const res = await app.service.weixin.removereceiver({
1606
+ ctx,
1607
+ type,
1608
+ openid,
1609
+ config,
1610
+ pay_config,
1674
1611
  })
1675
1612
  return ctx.SUCCESS(res)
1676
1613
  }
@@ -19,6 +19,7 @@ exports.refund = async ({
19
19
  out_trade_no: _out_trade_no,
20
20
  ...rest
21
21
  }) => {
22
+ if (!ctx) throw new Error('?ctx')
22
23
  const { app, appName } = getAppByCtx(ctx)
23
24
  const appConfig = getConfig(app)
24
25
  const { mchId, key, appId, partner_key } = await appConfig.getObject(
@@ -94,6 +95,7 @@ exports.refund_new = async ({
94
95
  out_trade_no: _out_trade_no,
95
96
  ...rest
96
97
  }) => {
98
+ if (!ctx) throw new Error('?ctx')
97
99
  const { app, appName } = getAppByCtx(ctx)
98
100
  const appConfig = getConfig(app)
99
101
 
@@ -162,6 +164,7 @@ exports.cash = async ({
162
164
  number,
163
165
  pay_config = 'weixin_pay',
164
166
  }) => {
167
+ if (!ctx) throw new Error('?ctx')
165
168
  const { app, appName } = getAppByCtx(ctx)
166
169
  const appConfig = getConfig(app)
167
170
 
@@ -455,6 +458,7 @@ exports.pc_pay = async ({
455
458
  type = 'PC-WEIXIN',
456
459
  prefix = '',
457
460
  }) => {
461
+ if (!ctx) throw new Error('?ctx')
458
462
  if (!order_id) throw new Error('缺order_id')
459
463
  if (!price) throw new Error('缺price')
460
464
  if (!name) throw new Error('缺name')
@@ -509,3 +513,114 @@ exports.pc_pay = async ({
509
513
  site_host || 'api.kuashou.com'
510
514
  }/${appName}/common/createQr?url=${result.code_url}`
511
515
  }
516
+
517
+ exports.profitsharing = async ({
518
+ ctx,
519
+ transaction_id = '',
520
+ out_order_no = '',
521
+ type = 'PERSONAL_OPENID',
522
+ amount = 0,
523
+ openid = '',
524
+ description = '手续费',
525
+ config = 'weixin_mp',
526
+ pay_config = 'weixin_pay',
527
+ }) => {
528
+ if (!ctx) throw new Error('?ctx')
529
+ const { app, appName } = getAppByCtx(ctx)
530
+
531
+ const appConfig = getConfig(app)
532
+
533
+ const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
534
+
535
+ const app_id = (await appConfig.getObject(config)).app_id
536
+
537
+ const wxpay = WXPay({
538
+ appid: app_id,
539
+ mch_id: mchId,
540
+ partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
541
+ pfx: await fsPromise.readFile(
542
+ path.resolve(
543
+ __dirname,
544
+ `${process.cwd()}/app/${appName}/plugins/weixin/${key}/apiclient_cert.p12`
545
+ )
546
+ ),
547
+ })
548
+
549
+ const res = await wxpay.profitsharingSync({
550
+ transaction_id,
551
+ out_order_no,
552
+ receivers: [
553
+ {
554
+ type,
555
+ account: openid,
556
+ description,
557
+ amount,
558
+ },
559
+ ],
560
+ })
561
+ return res
562
+ }
563
+
564
+ exports.addreceiver = async ({
565
+ ctx,
566
+ type = 'PERSONAL_OPENID',
567
+ openid = '',
568
+ relation_type = 'USER',
569
+ config = 'weixin_mp',
570
+ pay_config = 'weixin_pay',
571
+ }) => {
572
+ if (!ctx) throw new Error('?ctx')
573
+ const { app, appName } = getAppByCtx(ctx)
574
+
575
+ const appConfig = getConfig(app)
576
+
577
+ const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
578
+
579
+ const app_id = (await appConfig.getObject(config)).app_id
580
+
581
+ const wxpay = WXPay({
582
+ appid: app_id,
583
+ mch_id: mchId,
584
+ partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
585
+ })
586
+
587
+ const res = await wxpay.addreceiverSync({
588
+ receiver: {
589
+ type,
590
+ account: openid,
591
+ relation_type,
592
+ },
593
+ })
594
+ return res
595
+ }
596
+
597
+ exports.removereceiver = async ({
598
+ ctx,
599
+ type = 'PERSONAL_OPENID',
600
+ openid = '',
601
+ config = 'weixin_mp',
602
+ pay_config = 'weixin_pay',
603
+ }) => {
604
+ if (!ctx) throw new Error('?ctx')
605
+ const { app, appName } = getAppByCtx(ctx)
606
+
607
+ const appConfig = getConfig(app)
608
+
609
+ const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
610
+
611
+ const app_id = (await appConfig.getObject(config)).app_id
612
+
613
+ const wxpay = WXPay({
614
+ appid: app_id,
615
+ mch_id: mchId,
616
+ partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
617
+ })
618
+
619
+ const res = await wxpay.removereceiverSync({
620
+ receiver: {
621
+ type,
622
+ account: openid,
623
+ },
624
+ })
625
+ return res
626
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "10.5.5",
3
+ "version": "10.6.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {