point.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const orderData = await this.orderModel.findById(order_id);
  26. if (!orderData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '支付成功添加积分:未找到支付订单信息');
  27. // 10块钱给1积分,不到10块不给
  28. const pay_money = _.get(orderData, 'pay.pay_money');
  29. if (!_.isNumber(pay_money) || pay_money < 10) return;
  30. const point = _.floor(pay_money / 10);
  31. // 添加积分
  32. const user = await this.userModel.findOne({ openid });
  33. if (!user) {
  34. console.error('未找到用户信息----添加积分');
  35. return;
  36. }
  37. const customer = _.get(user, '_id');
  38. const pointData = {
  39. customer,
  40. point,
  41. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  42. source: '0',
  43. };
  44. console.log(pointData);
  45. tran.insert('Point', pointData);
  46. }
  47. }
  48. module.exports = PointService;