point.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class PointService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'point');
  11. this.model = this.ctx.model.User.Point;
  12. this.orderModel = this.ctx.model.Trade.Order;
  13. this.userModel = this.ctx.model.User.User;
  14. }
  15. /**
  16. * 添加积分;将处理添加至事务之中
  17. * @param {Object} params 参数
  18. * @param params.order_id 订单id
  19. * @param params.openid 支付人的openid
  20. * @param {Transaction} tran 数据库事务实例
  21. * @return
  22. */
  23. async afterPayOrder({ order_id, openid }, tran) {
  24. if (!tran) throw new BusinessError(ErrorCode.DATA_INVALID, '支付成功添加积分:缺少事务参数');
  25. console.log('line 25 in function:');
  26. const orderData = await this.orderModel.findById(order_id);
  27. if (!orderData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '支付成功添加积分:未找到支付订单信息');
  28. console.log('line 28 in function:');
  29. // 10块钱给1积分,不到10块不给
  30. const pay_money = _.get(orderData, 'pay.pay_money');
  31. // if (!_.isNumber(pay_money) || pay_money < 10) return;
  32. const point = _.floor(pay_money / 10);
  33. // 添加积分
  34. const user = await this.userModel.findOne({ openid });
  35. if (!user) {
  36. console.error('未找到用户信息----添加积分');
  37. return;
  38. }
  39. const customer = _.get(user, '_id');
  40. const pointData = {
  41. customer,
  42. point,
  43. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  44. source: '0',
  45. };
  46. console.log(pointData);
  47. tran.insert('Point', pointData);
  48. }
  49. }
  50. module.exports = PointService;