123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 'use strict';
- const BaseController = require('../base.js');
- // 微信接口操作类
- module.exports = class WechatController extends BaseController {
- /**
- * [tokenSignValidate 通讯验证器]
- * @return {[type]} [description]
- */
- get tokenSignValidate() {
- const that = this;
- return {
- signature: that.ctx.rules.name('加密签名')
- .required()
- .notEmpty()
- .trim(),
- timestamp: that.ctx.rules.name('时间戳')
- .required()
- .notEmpty(),
- nonce: that.ctx.rules.name('随机数')
- .required()
- .notEmpty(),
- echostr: that.ctx.rules.name('随机字符串')
- .required()
- .notEmpty(),
- };
- }
- get getWxConfigValidate() {
- const that = this;
- return {
- url: that.ctx.rules.name('需要调用JS接口的URL')
- .trim(),
- };
- }
- /**
- * [tokenSign 微信通讯验证器]
- * @return {[type]} [description]
- */
- async tokenSign() {
- const that = this;
- try {
- const token = 'szjcomo2021';
- const data = await that.ctx.validate(that.tokenSignValidate, await that.ctx.getParse());
- const tmpstr = [ token, data.nonce, data.timestamp ].sort()
- .join('');
- const sha1str = that.app.szjcomo.SHA1(tmpstr);
- if (sha1str == data.signature) {
- that.ctx.body = data.echostr;
- return true;
- }
- that.ctx.body = '';
- return false;
- } catch (err) {
- await that.logs('wechat.js', err);
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [message 处理微信消息]
- * @return {[type]} [description]
- */
- async message() {
- const that = this;
- try {
- const data = await that.app.szjcomo.parseXml(that.ctx.request.body);
- that.ctx.body = await that.service.wechat.msgText('思智捷科技,需要开发者自定义开发逻辑', data);
- return false;
- } catch (err) {
- await that.logs('wechat.js', err);
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [wxJsAPIConfig 获取微信JSapiconfig]
- * @return {[type]} [description]
- */
- async wxJsAPIConfig() {
- const that = this;
- const urlData = await that.ctx.validate(that.getWxConfigValidate, await that.ctx.getParse());
- try {
- const result = await that.service.wechat.jsapiConfig(urlData);
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
- } catch (err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [payCallback 微信支付回调]
- * @return {[type]} [description]
- */
- async payCallback() {
- const that = this;
- const result = await that.app.szjcomo.parseXml(that.ctx.request.body);
- let transaction;
- try {
- transaction = await that.app.model.transaction();
- const attach = that.app.szjcomo.json_decode(that.app.szjcomo.base64_decode(result.attach));
- const data = {
- user_id: attach.user_id, pay_id: attach.pay_id,
- out_trade_no: result.out_trade_no, total_fee: result.total_fee,
- trade_type: result.trade_type, transaction_id: result.transaction_id,
- time_end: result.time_end, openid: result.openid || '', sub_is_subscribe: result.sub_is_subscribe || '',
- sub_openid: result.sub_openid || '', is_subscribe: result.is_subscribe,
- fee_type: result.fee_type, bank_type: result.bank_type, cash_fee: result.cash_fee,
- create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- };
- const createBean = await that.app.comoBean.instance(data, { transaction });
- const insert = await that.service.base.create(createBean, that.app.model.Payments, '支付回调记录失败,请稍候重试');
- const updateBean = await that.app.comoBean.instance({
- order_status: 1,
- pay_sn: result.out_trade_no,
- }, { transaction, where: { order_id: attach.order_id } });
- const updateRes = await that.service.base.update(updateBean, that.app.model.Orders, '支付回调后更新订单状态失败,请稍候重试');
- try {
- if (updateRes) {
- // 2023/2/28 餐饮币发放
- that.service.diningCoin.orderDiningCoinDispatchAfterPay(attach.order_id);
- setTimeout(() => {
- // 2022/11/26 付款订单执行分佣
- that.service.commission.orderCommissionDispatchAfterPay(attach.order_id);
- }, 3000);
- }
- } catch (e) {
- that.logger.error('订单id:%s 出现异常:%s', attach.order_id, e.toString());
- }
- // 用户下单扣除库存,受控于后台配置的扣除时机
- await that.service.order.productStockSub(attach.order_id, transaction, 1);
- await transaction.commit();
- await that.service.shop.orderPrinter(attach.order_id);
- that.ctx.body = await that.app.szjcomo.createXml({ return_code: 'SUCCESS', return_msg: 'OK' });
- return false;
- } catch (err) {
- if (transaction) await transaction.rollback();
- await that.logs('wechat.js', err);
- that.ctx.body = 'fial';
- return false;
- }
- }
- };
|