wechat.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. const BaseController = require('../base.js');
  3. // 微信接口操作类
  4. module.exports = class WechatController extends BaseController {
  5. /**
  6. * [tokenSignValidate 通讯验证器]
  7. * @return {[type]} [description]
  8. */
  9. get tokenSignValidate() {
  10. const that = this;
  11. return {
  12. signature: that.ctx.rules.name('加密签名')
  13. .required()
  14. .notEmpty()
  15. .trim(),
  16. timestamp: that.ctx.rules.name('时间戳')
  17. .required()
  18. .notEmpty(),
  19. nonce: that.ctx.rules.name('随机数')
  20. .required()
  21. .notEmpty(),
  22. echostr: that.ctx.rules.name('随机字符串')
  23. .required()
  24. .notEmpty(),
  25. };
  26. }
  27. get getWxConfigValidate() {
  28. const that = this;
  29. return {
  30. url: that.ctx.rules.name('需要调用JS接口的URL')
  31. .trim(),
  32. };
  33. }
  34. /**
  35. * [tokenSign 微信通讯验证器]
  36. * @return {[type]} [description]
  37. */
  38. async tokenSign() {
  39. const that = this;
  40. try {
  41. const token = 'szjcomo2021';
  42. const data = await that.ctx.validate(that.tokenSignValidate, await that.ctx.getParse());
  43. const tmpstr = [ token, data.nonce, data.timestamp ].sort()
  44. .join('');
  45. const sha1str = that.app.szjcomo.SHA1(tmpstr);
  46. if (sha1str == data.signature) {
  47. that.ctx.body = data.echostr;
  48. return true;
  49. }
  50. that.ctx.body = '';
  51. return false;
  52. } catch (err) {
  53. await that.logs('wechat.js', err);
  54. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  55. }
  56. }
  57. /**
  58. * [message 处理微信消息]
  59. * @return {[type]} [description]
  60. */
  61. async message() {
  62. const that = this;
  63. try {
  64. const data = await that.app.szjcomo.parseXml(that.ctx.request.body);
  65. that.ctx.body = await that.service.wechat.msgText('思智捷科技,需要开发者自定义开发逻辑', data);
  66. return false;
  67. } catch (err) {
  68. await that.logs('wechat.js', err);
  69. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  70. }
  71. }
  72. /**
  73. * [wxJsAPIConfig 获取微信JSapiconfig]
  74. * @return {[type]} [description]
  75. */
  76. async wxJsAPIConfig() {
  77. const that = this;
  78. const urlData = await that.ctx.validate(that.getWxConfigValidate, await that.ctx.getParse());
  79. try {
  80. const result = await that.service.wechat.jsapiConfig(urlData);
  81. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  82. } catch (err) {
  83. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  84. }
  85. }
  86. /**
  87. * [payCallback 微信支付回调]
  88. * @return {[type]} [description]
  89. */
  90. async payCallback() {
  91. const that = this;
  92. const result = await that.app.szjcomo.parseXml(that.ctx.request.body);
  93. let transaction;
  94. try {
  95. transaction = await that.app.model.transaction();
  96. const attach = that.app.szjcomo.json_decode(that.app.szjcomo.base64_decode(result.attach));
  97. const data = {
  98. user_id: attach.user_id, pay_id: attach.pay_id,
  99. out_trade_no: result.out_trade_no, total_fee: result.total_fee,
  100. trade_type: result.trade_type, transaction_id: result.transaction_id,
  101. time_end: result.time_end, openid: result.openid || '', sub_is_subscribe: result.sub_is_subscribe || '',
  102. sub_openid: result.sub_openid || '', is_subscribe: result.is_subscribe,
  103. fee_type: result.fee_type, bank_type: result.bank_type, cash_fee: result.cash_fee,
  104. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  105. };
  106. const createBean = await that.app.comoBean.instance(data, { transaction });
  107. const insert = await that.service.base.create(createBean, that.app.model.Payments, '支付回调记录失败,请稍候重试');
  108. const updateBean = await that.app.comoBean.instance({
  109. order_status: 1,
  110. pay_sn: result.out_trade_no,
  111. }, { transaction, where: { order_id: attach.order_id } });
  112. const updateRes = await that.service.base.update(updateBean, that.app.model.Orders, '支付回调后更新订单状态失败,请稍候重试');
  113. try {
  114. if (updateRes) {
  115. // 2023/2/28 餐饮币发放
  116. that.service.diningCoin.orderDiningCoinDispatchAfterPay(attach.order_id);
  117. setTimeout(() => {
  118. // 2022/11/26 付款订单执行分佣
  119. that.service.commission.orderCommissionDispatchAfterPay(attach.order_id);
  120. }, 3000);
  121. }
  122. } catch (e) {
  123. that.logger.error('订单id:%s 出现异常:%s', attach.order_id, e.toString());
  124. }
  125. // 用户下单扣除库存,受控于后台配置的扣除时机
  126. await that.service.order.productStockSub(attach.order_id, transaction, 1);
  127. await transaction.commit();
  128. await that.service.shop.orderPrinter(attach.order_id);
  129. that.ctx.body = await that.app.szjcomo.createXml({ return_code: 'SUCCESS', return_msg: 'OK' });
  130. return false;
  131. } catch (err) {
  132. if (transaction) await transaction.rollback();
  133. await that.logs('wechat.js', err);
  134. that.ctx.body = 'fial';
  135. return false;
  136. }
  137. }
  138. };