From da959847c64e5401f189766c4da0fda20b857781 Mon Sep 17 00:00:00 2001 From: banche Date: Fri, 18 Mar 2022 14:30:30 +0800 Subject: [PATCH] fix:购物车库存限制 --- packageA/pages/goodsInfo/goodsInfo.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/packageA/pages/goodsInfo/goodsInfo.js b/packageA/pages/goodsInfo/goodsInfo.js index a4769ed..c33c953 100644 --- a/packageA/pages/goodsInfo/goodsInfo.js +++ b/packageA/pages/goodsInfo/goodsInfo.js @@ -843,12 +843,16 @@ Page({ }, //------------加入购物车-------------- - addCart: function(t) { + addCart: async function(t) { var th = this; var ind = t.currentTarget.dataset.openSpecModal_ind; var action= t.currentTarget.dataset.action; if(!ind) ind = t.currentTarget.dataset.openspecmodal_ind; - + // 活动ID + var prom_id = th.data.prom_id; + + var redisNum = 0; + th.setData({ open_ind_store: ind }); @@ -859,69 +863,79 @@ Page({ }; - - - //000000 - //如果是秒杀的话,要看redis够不够 - // if (this.data.prom_type == 1) { - - // if (this.data.openSpecModal_flash_normal) { - // this.data.is_normal=1; //是普通购买 - // return false; - // } - - - // this.getactLen(function (num) { - // if (num < th.data.goodsInputNum) { - // getApp().my_warnning("活动库存不足!", 0, th); - // return false; - // } else { - // // th.add_cart_func(t); - // } - // }); - // } else { - // // th.add_cart_func(t); - // } - //000000 - // if(!this.data.openSpecModal_ind) { if(this.data.prom_type == 1) { this.data.sele_g.viplimited = this.data.sele_g.buy_limit; - if(this.data.sele_g.viplimited > 0 && !this.data.is_normal) { + if(!this.data.is_normal) { + // 输入的数量 var t = th.data.goodsInputNum; + + // 检查redis库存量 + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + + os.stoid + "/1/" + prom_id, + {} + ).then(res => { + if (res.data.code == 0) { + redisNum = res.data.data; + }; + }); + + if (t > redisNum) { + wx.showModal({ + title: '超出活动库存', + }); + return false; + } + + // 获取购物车同类数量 判断是否超库存 + var cartGoodsNum = 0; + await getApp().request.promiseGet("/api/weshop/cartService/page?store_id="+os.stoid+"&user_id="+oo.user_id+"&service_id="+th.data.data.id+"&pick_id="+th.data.sto_sele_id, + { }).then(res => { + if (res.data.data.pageData.length > 0) { + const tmpObj = res.data.data.pageData[0]; + cartGoodsNum = tmpObj.goods_num; + } + + }); + + if (cartGoodsNum >= redisNum) { + wx.showModal({ + title: '超出活动库存', + }); + return false; + } + + // 每人限购数 th.data.sele_g.viplimited = th.data.sele_g.buy_limit; + + // 是否开启个人限购 if (th.data.sele_g.viplimited > 0) { - var gd_buy_num = th.data.sele_g.buy_num; - + // 已经付款的单数 + var gd_buy_num = th.data.sele_g.buy_num; + if (t + gd_buy_num > th.data.sele_g.viplimited) { wx.showModal({ - title: '超出活动库存', + title: '超出活动限购数量', }); let num = th.data.sele_g.viplimited - gd_buy_num; if (num <= 0) num = 1; th.setData({goodsInputNum: num}); return false; - } + } + + }; }; }; - // }; - - - - - - - - if(action=="buy"){ + if(action=="buy"){ // 直接购买 //--------------此时操作的数据------------ var newd = { id: th.data.data.id, @@ -946,10 +960,7 @@ Page({ // console.log('newd++++++++', newd); th.buyNow(newd); - } else { - - - + } else { // 购物车 var newd = { service_id: th.data.data.id, @@ -976,7 +987,6 @@ Page({ newd['money'] = th.data.prom_price; }; }; - //----先看会员在购物车中是否加入了该商品----- getApp().request.get("/api/weshop/cartService/page", { -- libgit2 0.21.4