q-koa 10.2.2 → 10.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/app.js CHANGED
@@ -266,7 +266,10 @@ class APP {
266
266
 
267
267
  starter.on('loadAll', async () => {
268
268
  const appName = this.config.includes[0]
269
- if (!this.app[appName]) throw new Error(`没有生成应用${this.config.includes[0]},请检查config includes`)
269
+ if (!this.app[appName])
270
+ throw new Error(
271
+ `没有生成应用${this.config.includes[0]},请检查config includes`
272
+ )
270
273
  this.server = this.app.listen(this.port, () => {
271
274
  console.log(
272
275
  `${moment().format(
@@ -1048,6 +1051,18 @@ class APP {
1048
1051
  Array.isArray(excludeInclude) &&
1049
1052
  excludeInclude.length > 0
1050
1053
  ) {
1054
+ // const attributes = _.omit(app[appName].attributes[controller].filter(
1055
+ // (i) => {
1056
+ // return i.type !== Sequelize.VIRTUAL
1057
+ // }
1058
+ // )
1059
+ // const currentAttribute = allAttributes.filter((item) => {
1060
+ // if (Array.isArray(attributes)) {
1061
+ // return attributes.includes(item)
1062
+ // } else {
1063
+ // return attributes.exclude.includes(item)
1064
+ // }
1065
+ // })
1051
1066
  myInclude = myInclude.filter((i) => {
1052
1067
  return excludeInclude.every((m) => {
1053
1068
  return app[appName].model[m] !== i.model
@@ -831,6 +831,105 @@ exports.h5_pay = async (ctx) => {
831
831
  )
832
832
  }
833
833
 
834
+ exports.pc_pay = async (ctx) => {
835
+ const { app, appName } = getAppByCtx(ctx)
836
+ const {
837
+ name,
838
+ order_id,
839
+ price,
840
+ prefix = '',
841
+ type = 'PC-WEIXIN',
842
+ out_trade_no: _out_trade_no,
843
+ is_admin = false,
844
+ config = 'weixin_mp',
845
+ pay_config = 'weixin_pay',
846
+ } = ctx.request.body
847
+
848
+ if (price === 0) throw new Error('价格不能为0')
849
+
850
+ const appConfig = getConfig(app)
851
+
852
+ const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
853
+
854
+ const { is_dev, site_host } = await appConfig.getObject('base')
855
+ // 公众号支付用 服务号 appid,其他用小程序 appid
856
+ const app_id = (await appConfig.getObject(config)).app_id
857
+
858
+ const wxpay = WXPay({
859
+ appid: app_id,
860
+ mch_id: mchId,
861
+ partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
862
+ })
863
+
864
+ const pay = (options) =>
865
+ new Promise((resolve, reject) => {
866
+ wxpay.createUnifiedOrder(options, (err, result) => {
867
+ if (err) return reject(err)
868
+ return resolve(result)
869
+ })
870
+ })
871
+
872
+ const out_trade_no = _out_trade_no
873
+ ? _out_trade_no
874
+ : prefix
875
+ ? `${key.length > 10 ? appName : key}_${prefix}-${order_id}_${type}`
876
+ : `${key.length > 10 ? appName : key}_${order_id}_${type}`
877
+
878
+ const notify_url = `https://${
879
+ site_host || 'api.kuashou.com'
880
+ }/${appName}/weixin/notify`
881
+
882
+ const result = await pay({
883
+ body: name,
884
+ detail: '公众号支付测试',
885
+ out_trade_no,
886
+ trade_type: 'NATIVE',
887
+ total_fee: is_admin || is_dev ? 1 : Math.round(price * 100),
888
+ spbill_create_ip: '192.168.2.210',
889
+ notify_url,
890
+ })
891
+
892
+ if (result.result_code === 'FAIL') throw new Error(result.err_code_des)
893
+
894
+ return ctx.SUCCESS(
895
+ `https://${site_host || 'api.kuashou.com'}/${appName}/common/createQr?url=${
896
+ result.code_url
897
+ }`
898
+ )
899
+ }
900
+
901
+ exports.pc_pay = async (ctx) => {
902
+ const { app, appName } = getAppByCtx(ctx)
903
+ const {
904
+ name,
905
+ order_id,
906
+ price,
907
+ prefix = '',
908
+ out_trade_no: _out_trade_no,
909
+ is_admin = false,
910
+ config = 'weixin_mp',
911
+ pay_config = 'weixin_pay',
912
+ } = ctx.request.body
913
+
914
+ if (price === 0) throw new Error('价格不能为0')
915
+
916
+ if (!app.service.weixin) throw new Error(`weixin可能没有service.js`)
917
+ const result = await app.service.weixin.pc_pay({
918
+ app,
919
+ appName,
920
+ name,
921
+ order_id,
922
+ price,
923
+ config,
924
+ pay_config,
925
+ is_admin,
926
+ out_trade_no: _out_trade_no,
927
+ prefix,
928
+ })
929
+
930
+ return ctx.SUCCESS(result)
931
+ }
932
+
834
933
  exports.mp_pay = async (ctx) => {
835
934
  const { app, appName } = getAppByCtx(ctx)
836
935
  const {
@@ -1078,6 +1177,7 @@ exports.url_link = async (ctx) => {
1078
1177
  const { app } = getAppByCtx(ctx)
1079
1178
 
1080
1179
  const { page, scene, config = 'weixin_mp' } = ctx.request.body
1180
+ if (!app.service.weixin) throw new Error(`weixin可能没有service.js`)
1081
1181
  const result = await app.service.weixin.url_link({
1082
1182
  app,
1083
1183
  page,
@@ -1116,6 +1216,7 @@ exports.qr_code = async (ctx) => {
1116
1216
  config = 'weixin_mp',
1117
1217
  } = ctx.request.body
1118
1218
 
1219
+ if (!app.service.weixin) throw new Error(`weixin可能没有service.js`)
1119
1220
  const result = await app.service.weixin.qr_code({
1120
1221
  app,
1121
1222
  page,
@@ -1171,6 +1272,8 @@ exports.refund = async (ctx) => {
1171
1272
  } = ctx.request.body
1172
1273
 
1173
1274
  const fn = is_new_pay ? 'refund_new' : 'refund'
1275
+
1276
+ if (!app.service.weixin) throw new Error(`weixin可能没有service.js`)
1174
1277
  await app.service.weixin[fn]({
1175
1278
  ctx,
1176
1279
  id,
@@ -1440,6 +1543,7 @@ exports.messagePush = async (ctx) => {
1440
1543
  const result = ctx.request.body
1441
1544
  if (!lodash.isEmpty(result)) {
1442
1545
  const event = result.Event
1546
+ if (!app.service.weixin) throw new Error(`weixin可能没有service.js`)
1443
1547
  if (event) {
1444
1548
  if (app.service.weixin[event]) {
1445
1549
  await app.service.weixin[event]({
@@ -1,5 +1,6 @@
1
1
  const { getAppByCtx, getAppConfig, getConfig, lodash } = require('q-koa')
2
2
  const { Pay } = require('@sigodenjs/wechatpay')
3
+ const WXPay = require('weixin-pay-fork')
3
4
  const path = require('path')
4
5
  const fsPromise = require('fs/promises')
5
6
  const WeixinMp = require('../../services/weixinMP')
@@ -441,3 +442,67 @@ exports.funds_order_refund = async ({ app, result }) => {
441
442
  }
442
443
  }
443
444
  }
445
+
446
+ exports.pc_pay = async ({
447
+ app,
448
+ appName,
449
+ name,
450
+ order_id,
451
+ price,
452
+ config,
453
+ pay_config,
454
+ out_trade_no: _out_trade_no,
455
+ is_admin,
456
+ type = 'PC-WEIXIN',
457
+ prefix,
458
+ }) => {
459
+ const appConfig = getConfig(app)
460
+
461
+ const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
462
+
463
+ const { is_dev, site_host } = await appConfig.getObject('base')
464
+ // 公众号支付用 服务号 appid,其他用小程序 appid
465
+ const app_id = (await appConfig.getObject(config)).app_id
466
+
467
+ const wxpay = WXPay({
468
+ appid: app_id,
469
+ mch_id: mchId,
470
+ partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
471
+ })
472
+
473
+ const pay = (options) =>
474
+ new Promise((resolve, reject) => {
475
+ wxpay.createUnifiedOrder(options, (err, result) => {
476
+ if (err) return reject(err)
477
+ return resolve(result)
478
+ })
479
+ })
480
+
481
+ const out_trade_no = _out_trade_no
482
+ ? _out_trade_no
483
+ : prefix
484
+ ? `${key.length > 10 ? appName : key}_${prefix}-${order_id}_${type}`
485
+ : `${key.length > 10 ? appName : key}_${order_id}_${type}`
486
+
487
+ const notify_url = `https://${
488
+ site_host || 'api.kuashou.com'
489
+ }/${appName}/weixin/notify`
490
+
491
+ const result = await pay({
492
+ body: name,
493
+ detail: '公众号支付测试',
494
+ out_trade_no,
495
+ trade_type: 'NATIVE',
496
+ total_fee: is_admin || is_dev ? 1 : Math.round(price * 100),
497
+ spbill_create_ip: '192.168.2.210',
498
+ notify_url,
499
+ })
500
+
501
+ if (result.return_code === 'FAIL') {
502
+ throw new Error(result.return_msg)
503
+ }
504
+
505
+ return `https://${
506
+ site_host || 'api.kuashou.com'
507
+ }/${appName}/common/createQr?url=${result.code_url}`
508
+ }
@@ -49,7 +49,7 @@ const getBrandListUrl =
49
49
  'https://api.weixin.qq.com/shop/account/get_brand_list?access_token=%s'
50
50
 
51
51
  const uploadImageUrl =
52
- 'https://api.weixin.qq.com/shop/img/upload?access_token=%s'
52
+ 'https://api.weixin.qq.com/channels/ec/basics/img/upload?access_token=%s'
53
53
 
54
54
  const fsPromise = require('fs/promises')
55
55
  const LRU = require('lru-cache')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "10.2.2",
3
+ "version": "10.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {