lrf 2 年之前
父节点
当前提交
8a9858866b
共有 2 个文件被更改,包括 29 次插入14 次删除
  1. 20 8
      app/extend/context.js
  2. 9 6
      app/service/user/cashBack.js

+ 20 - 8
app/extend/context.js

@@ -1,37 +1,49 @@
 'use strict';
 'use strict';
 const Decimal = require('decimal.js');
 const Decimal = require('decimal.js');
+const _ = require('lodash');
 // Decimal.set({ precision: 2 });
 // Decimal.set({ precision: 2 });
 module.exports = {
 module.exports = {
+  turnDecimal(n) {
+    if (_.isObject(n)) {
+      n = JSON.parse(JSON.stringify(n));
+      if (_.isObject(n)) n = _.get(n, '$numberDecimal');
+    }
+    return new Decimal(n);
+  },
   // 加法
   // 加法
   plus(n1 = 0, n2 = 0) {
   plus(n1 = 0, n2 = 0) {
-    const number1 = new Decimal(n1);
-    const number2 = new Decimal(n2);
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
     const result = number1.add(number2).toFixed(2, Decimal.ROUND_DOWN);
     const result = number1.add(number2).toFixed(2, Decimal.ROUND_DOWN);
     return this.toNumber(result);
     return this.toNumber(result);
   },
   },
   // 减法
   // 减法
   minus(n1 = 0, n2 = 0) {
   minus(n1 = 0, n2 = 0) {
-    const number1 = new Decimal(n1);
-    const number2 = new Decimal(n2);
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
     const result = number1.minus(number2).toFixed(2, Decimal.ROUND_DOWN);
     const result = number1.minus(number2).toFixed(2, Decimal.ROUND_DOWN);
     return this.toNumber(result);
     return this.toNumber(result);
   },
   },
   // 乘法
   // 乘法
   multiply(n1 = 0, n2 = 0) {
   multiply(n1 = 0, n2 = 0) {
-    const number1 = new Decimal(n1);
-    const number2 = new Decimal(n2);
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
     const result = number1.mul(number2).toFixed(2, Decimal.ROUND_DOWN);
     const result = number1.mul(number2).toFixed(2, Decimal.ROUND_DOWN);
     return this.toNumber(result);
     return this.toNumber(result);
   },
   },
   // 除法
   // 除法
   divide(n1 = 0, n2 = 0) {
   divide(n1 = 0, n2 = 0) {
-    const number1 = new Decimal(n1);
-    const number2 = new Decimal(n2);
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
     const result = number1.div(number2).toFixed(2, Decimal.ROUND_DOWN);
     const result = number1.div(number2).toFixed(2, Decimal.ROUND_DOWN);
     return this.toNumber(result);
     return this.toNumber(result);
   },
   },
 
 
   toNumber(num) {
   toNumber(num) {
+    if (_.isObject(num)) {
+      num = JSON.parse(JSON.stringify(num));
+      if (_.isObject(num)) num = _.get(num, '$numberDecimal');
+    }
     return new Decimal(num).toNumber();
     return new Decimal(num).toNumber();
   },
   },
 };
 };

+ 9 - 6
app/service/user/cashBack.js

@@ -14,12 +14,15 @@ class CashBackService extends CrudService {
 
 
   async computedTotal({ customer }) {
   async computedTotal({ customer }) {
     assert(customer, '缺少用户信息');
     assert(customer, '缺少用户信息');
-    const res = await this.model.find({ inviter: customer });
-    const total = res.reduce((p, n) => {
-      let money = n.money;
-      if (!(n.source === '0' || n.source === '1')) money = -money;
-      return this.ctx.plus(p, money);
-    }, 0);
+    // status(状态) 和 source(来源) 均表示钱是入账还是出账,判断一个就行,主要还是依靠来源吧
+    const res = await this.model.find({ inviter: customer }).lean();
+    let total = 0;
+    for (const i of res) {
+      const { source, money } = i;
+      const sn = parseInt(source);
+      if (sn >= 0) total = this.ctx.plus(total, money);
+      else total = this.ctx.minus(total, money);
+    }
     return total;
     return total;
   }
   }
   /**
   /**