lrf402788946 4 years ago
parent
commit
07adc7c5dd
2 changed files with 29 additions and 2 deletions
  1. 18 0
      app/model/cash_error.js
  2. 11 2
      app/service/wxpay.js

+ 18 - 0
app/model/cash_error.js

@@ -0,0 +1,18 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 提现错误记录表
+const cashError = {
+  content: { type: String }, // 内容
+  params: { type: Object }, // 参数
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(cashError, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('cashError', schema, 'cashError');
+};

+ 11 - 2
app/service/wxpay.js

@@ -16,6 +16,7 @@ class WxpayService extends CrudService {
     this.redis = this.app.redis;
     this.model = this.ctx.model.Card;
     this.cashModel = this.ctx.model.Cash;
+    this.cashError = this.ctx.model.CashError;
   }
 
   async toAuth({ code, id }) {
@@ -74,15 +75,22 @@ class WxpayService extends CrudService {
     // TODO 添加错误记录
     const { return_code, return_msg, result_code, err_code, err_code_des } = result;
     // 请求是否成功
-    if (return_code !== 'SUCCESS') throw new BusinessError(ErrorCode.SERVICE_FAULT, `${return_code}:${return_msg}`);
+    if (return_code !== 'SUCCESS') {
+      this.cashError.create({ word: `${return_code}:${return_msg}` });
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, `${return_code}:${return_msg}`);
+    }
     // 请求失败具体问题
-    if (result_code === 'FAIL') throw new BusinessError(ErrorCode.SERVICE_FAULT, `${err_code}:${err_code_des}`);
+    if (result_code === 'FAIL') {
+      this.cashError.create({ word: `${err_code}:${err_code_des}` });
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, `${err_code}:${err_code_des}`);
+    }
     // 成功,添加记录=>减去积分
     const cashRecord = { name: user.name, mobile: user.mobile, b_point: opoints, i_point: points, e_point: pres };
     try {
       await this.cashModel.create(cashRecord);
     } catch (error) {
       const word = '添加提现记录失败!但钱已转出.';
+      this.cashError.create({ word });
       throw new BusinessError(ErrorCode.SERVICE_FAULT, word);
     }
     user.points = pres;
@@ -90,6 +98,7 @@ class WxpayService extends CrudService {
       await user.save();
     } catch (error) {
       const word = '用户减掉已转出积分失败!钱已转出;提现记录已添加';
+      this.cashError.create({ word });
       throw new BusinessError(ErrorCode.SERVICE_FAULT, word);
     }
     let newUser = await this.model.findById(id);