lrf402788946 4 lat temu
rodzic
commit
1dd74c58d2
2 zmienionych plików z 35 dodań i 27 usunięć
  1. 2 2
      app/model/cash.js
  2. 33 25
      app/service/wxpay.js

+ 2 - 2
app/model/cash.js

@@ -4,12 +4,12 @@ const moment = require('moment');
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 // 提现表
 const cash = {
-  account: { type: String, required: true }, // 账户
+  account: { type: String }, // 账户
   name: { type: String, required: true }, // 用户名
   mobile: { type: String, required: true }, // 手机号
   b_point: { type: Number }, // 提现前积分
   i_point: { type: Number }, // 本次提现积分
-  e_point: { type: Number }, // 剩余积分, 计算公式:b_p - (i_p+ _.ceil(i_p*0.06)) 收取6%手续费, 不足1积分按1积分计算
+  e_point: { type: Number }, // 剩余积分
   remark: { type: String, maxLength: 200 },
   create_time: {
     type: String,

+ 33 - 25
app/service/wxpay.js

@@ -12,44 +12,37 @@ const fs = require('fs');
 class WxpayService extends CrudService {
   constructor(ctx) {
     super(ctx, 'wxpay');
-    // this.model = this.ctx.model.Wxpay;
     this.appInfo = this.app.config.appInfo;
     this.redis = this.app.redis;
+    this.model = this.ctx.model.Card;
+    this.cashModel = this.ctx.model.Cash;
   }
 
   async toAuth({ code, id }) {
     const token = await this.redis.get(id);
     if (token) return;
-    let url;
-    try {
-      console.log(this.appInfo.id, this.appInfo.secret, code);
-      url = `https://api.weixin.qq.com/sns/jscode2session?appid=${this.appInfo.id}&secret=${this.appInfo.secret}&js_code=${code}&grant_type=authorization_code`;
-      console.log(`url=>${url}`);
-    } catch (error) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, `url拼接出错.参数=>appInfo:${JSON.stringify(this.appInfo) || ''};code:${code};id:${id}`);
-    }
-    try {
-      const res = await this.ctx.curl(url, {
-        method: 'get',
-        dataType: 'json',
-      });
-      console.log(`res=>${res}`);
-      const info = JSON.stringify(res.data);
-      await this.redis.set(id, info, 'EX', 3600);
-    } catch (error) {
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求出错');
-    }
-    // const url = appInfo => `https://api.weixin.qq.com/sns/jscode2session?appid=${appInfo.id}&secret=${appInfo.secret}&js_code=${code}&grant_type=authorization_code`;
+    const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${this.appInfo.id}&secret=${this.appInfo.secret}&js_code=${code}&grant_type=authorization_code`;
+    const res = await this.ctx.curl(url, {
+      method: 'get',
+      dataType: 'json',
+    });
+    const info = JSON.stringify(res.data);
+    await this.redis.set(id, info, 'EX', 3600);
 
   }
 
-  async cash({ id, name, money }) {
+  async cash({ id, money, points }) {
     const token = await this.redis.get(id);
     if (!token) throw new BusinessError(ErrorCode.SERVICE_FAULT, '未找到用户的openid,请重新登陆小程序!');
     const { openid } = JSON.parse(token);
     if (!this.appInfo) throw new BusinessError(ErrorCode.FILE_FAULT, '未设置小程序相关设置');
+    const user = await this.model.findById(id);
+    if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户信息!');
+    const opoints = _.get(user, 'points', 0);
+    const pres = _.subtract(opoints, points);
+    if (pres < 0) throw new BusinessError(ErrorCode.BADPARAM, '积分不足');
     // check_name:校验真名,暂不校验
-    const object = { amount: money, check_name: 'NO_CHECK', desc: '提现', mch_appid: this.appInfo.id, mchid: this.appInfo.store, nonce_str: random(16), openid, partner_trade_no: `cash${moment().format('YYYYMMDDHHmmss')}${random(12)}`, re_user_name: name };
+    const object = { amount: money, check_name: 'NO_CHECK', desc: '提现', mch_appid: this.appInfo.id, mchid: this.appInfo.store, nonce_str: random(16), openid, partner_trade_no: `cash${moment().format('YYYYMMDDHHmmss')}${random(12)}`, re_user_name: user.name };
     const clientIp = _.get(this.ctx.request, 'header.x-real-ip');
     if (clientIp) object.spbill_create_ip = clientIp;
     const sign = this.turnToSign(object);
@@ -60,7 +53,7 @@ class WxpayService extends CrudService {
       <partner_trade_no>${object.partner_trade_no}</partner_trade_no>
       <openid>${openid}</openid>
       <check_name>NO_CHECK</check_name>
-      <re_user_name>${name}</re_user_name>
+      <re_user_name>${user.name}</re_user_name>
       <amount>${money}</amount>
       <desc>提现</desc>
       ${clientIp ? '<spbill_create_ip></spbill_create_ip>' : ''}
@@ -83,7 +76,22 @@ class WxpayService extends CrudService {
     if (return_code !== 'SUCCESS') 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}`);
-
+    // 成功,添加记录=>减去积分
+    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 = '添加提现记录失败!但钱已转出.';
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, word);
+    }
+    user.points = pres;
+    try {
+      await user.save();
+    } catch (error) {
+      const word = '用户减掉已转出积分失败!钱已转出;提现记录已添加';
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, word);
+    }
+    return await this.model.findById(id);
   }
 
   turnToSign(object) {