lrf 2 лет назад
Родитель
Сommit
d593cb5a68
2 измененных файлов с 19 добавлено и 28 удалено
  1. 6 2
      app/model/trade/order.js
  2. 13 26
      app/service/trade/order.js

+ 6 - 2
app/model/trade/order.js

@@ -2,10 +2,14 @@
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 
-// 订单
+/**
+ * 订单表:
+ *  地址(address), 商品规格(goods) 都是快照 使用id重新查出来赋值回去
+ *  之后的优惠都放在total_detail中进行计算
+ */
 const order = {
   customer: { type: String, required: false, zh: '顾客', ref: 'User.Customer' }, //
-  address: { type: String, required: false, zh: '邮寄地址', ref: 'User.address' }, //
+  address: { type: String, required: false, zh: '邮寄地址' }, //
   goods: { type: Array, required: false, zh: '商品' }, // 按店铺分组,快照过来
   total_detail: { type: Object, required: false, zh: '总金额明细' }, // 优惠,活动;商品总额和运费由商品而来
   buy_time: { type: String, required: false, zh: '下单时间' }, //

+ 13 - 26
app/service/trade/order.js

@@ -17,6 +17,15 @@ class OrderService extends CrudService {
 
     this.cartModel = this.ctx.model.Trade.Cart;
   }
+
+  /**
+   * 创建订单
+   * 1.检测商品是否可以购买
+   * 2.
+   * @param {Object} body
+   */
+  async create(body) {}
+
   /**
    * 进入下单页面
    * @param {Object} body 请求参数
@@ -27,7 +36,6 @@ class OrderService extends CrudService {
     let data = await this.redis.get(key);
     if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
     data = JSON.parse(data);
-    console.log(data);
     let specsData = [];
     if (_.isArray(data)) {
       // 购物车来的: 1.循环校验 规格商品; 2.按店铺分组
@@ -67,8 +75,8 @@ class OrderService extends CrudService {
    */
   computedShopTotal(list) {
     for (const i of list) {
-      i.goods_total = i.goods.reduce((p, n) => p + (n.money || 0) * (n.num || 0), 0);
-      i.freight_total = i.goods.reduce((p, n) => p + (n.freight || 0) * (n.num || 0), 0);
+      i.goods_total = _.floor(i.goods.reduce((p, n) => p + (n.money || 0) * (n.num || 0), 0), 2);
+      i.freight_total = _.floor(i.goods.reduce((p, n) => p + (n.freight || 0) * (n.num || 0), 0), 2);
       console.log(i);
     }
     return list;
@@ -80,8 +88,8 @@ class OrderService extends CrudService {
    */
   computedAllTotal(list) {
     const obj = {
-      goods_total: list.reduce((p, n) => p + (n.goods_total || 0), 0),
-      freight_total: list.reduce((p, n) => p + (n.freight_total || 0), 0),
+      goods_total: _.floor(list.reduce((p, n) => p + (n.goods_total || 0), 0), 2),
+      freight_total: _.floor(list.reduce((p, n) => p + (n.freight_total || 0), 0), 2),
     };
     return obj;
   }
@@ -144,27 +152,6 @@ class OrderService extends CrudService {
     }
     return arr;
   }
-
-  /**
-   * 重组购物车商品
-   * @param {Object} goods 商品
-   * @param {Object} goodsSpec 商品规格
-   * @param {Object} data 购物车数据
-   */
-  setCartGoodsData(data) {
-    const { goods, goodsSpec, _id, num = 1 } = data;
-    const obj = {};
-    obj.cart_id = _id;
-    obj.goods_id = _.get(goods, '_id');
-    obj.goods_name = _.get(goods, 'name');
-    obj.goodsSpec_id = _.get(goodsSpec, '_id');
-    obj.goodsSpec_name = _.get(goodsSpec, 'name');
-    obj.freight = _.get(goodsSpec, 'freight');
-    obj.money = _.get(goodsSpec, 'sell_money');
-    obj.num = num;
-    obj.file = _.get(goods, 'file');
-    return obj;
-  }
 }
 
 module.exports = OrderService;