lrf %!s(int64=2) %!d(string=hai) anos
pai
achega
be9bc6fbd1

+ 8 - 0
app/controller/trade/config/.cart.js

@@ -45,4 +45,12 @@ module.exports = {
       },
     },
   },
+  checkGoodsNum: {
+    parameters: {
+      query: {
+        goodsSpecId: 'goodsSpecId',
+        num: 'num',
+      },
+    },
+  },
 };

+ 6 - 5
app/service/trade/cart.js

@@ -25,7 +25,7 @@ class CartService extends CrudService {
     list = JSON.parse(JSON.stringify(list));
     list = list.map(i => {
       const obj = {};
-      obj.cart_id = _.get(i, '_id');
+      obj.cart = _.get(i, '_id');
       obj.shop_name = _.get(i.shop, 'name');
       obj.shop = _.get(i.shop, '_id');
       obj.goods = this.setCartGoodsData(i.goods, i.goodsSpec);
@@ -58,6 +58,7 @@ class CartService extends CrudService {
     obj.goodsSpec_name = _.get(goodsSpec, 'name');
     obj.money = _.get(goodsSpec, 'sell_money');
     obj.num = _.get(goodsSpec, 'num');
+    obj.file = _.get(goods, 'file');
     return obj;
   }
 
@@ -77,12 +78,12 @@ class CartService extends CrudService {
     const data = await this.model.findOne(body);
     if (data) {
       const buyNum = parseInt(data.num) + parseInt(num);
-      const enough = await this.checkGoodsNum(goodsSpec, buyNum);
+      const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
       if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
       data.num = buyNum;
       await data.save();
     } else {
-      const enough = await this.checkGoodsNum(goodsSpec, num);
+      const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
       if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
       await this.model.create({ num, ...body });
     }
@@ -94,11 +95,11 @@ class CartService extends CrudService {
    * @param {Number} num 需要的库存数量
    * @return {Boolean} true:库存满足购物车条件;false:库存不满足条件
    */
-  async checkGoodsNum(goodsSpecId, num) {
+  async checkGoodsNum({ goodsSpecId, num }) {
     const goodsSpec = await this.goodsSpecModel.findById(goodsSpecId);
     if (!goodsSpec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品的规格数据');
     const { num: gsnum = 0 } = goodsSpec;
-    return gsnum >= num;
+    return { enough: gsnum >= num, total: gsnum };
   }
 }
 

+ 1 - 0
app/z_router/trade/cart.js

@@ -7,6 +7,7 @@ const rkey = 'cart';
 const ckey = 'trade.cart';
 const keyZh = '购物车';
 const routes = [
+  { method: 'get', path: `${rkey}/checkGoodsNum`, controller: `${ckey}.checkGoodsNum`, name: `${ckey}checkGoodsNum`, zh: `查询库存${keyZh}` },
   { method: 'get', path: `${rkey}/selfCart`, controller: `${ckey}.selfCart`, middleware: [ 'setColumnForQuery' ], name: `${ckey}SelfCart`, zh: `用户自己的${keyZh}` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },