diff --git a/packageA/pages/goodsInfo/goodsInfo.js b/packageA/pages/goodsInfo/goodsInfo.js index 894dc15..7435a37 100644 --- a/packageA/pages/goodsInfo/goodsInfo.js +++ b/packageA/pages/goodsInfo/goodsInfo.js @@ -447,7 +447,7 @@ Page({ sto_sele_name: e.pickup_name, sto_sele_id: e.pickup_id, sto_sele_distr: e.distr_type, - sto_sele_keyid:e.keyid, + sto_sele_keyid:e.keyid, }) } } @@ -562,9 +562,6 @@ Page({ // <---- 秒杀 - - - @@ -829,7 +826,21 @@ Page({ }); }, - + //获取redis中的数量 + async getactLen(func) { + var r_num = 0, + prom_type = this.data.prom_type, + prom_id = this.data.prom_id; + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + os.stoid + "/" + prom_type + "/" + prom_id, { + 1: 1 + }).then(res => { + var em = res; + if (em.data.code == 0) { + r_num = em.data.data; + } + }) + func(r_num); + }, //------------加入购物车-------------- addCart: async function(t) { @@ -837,7 +848,8 @@ Page({ 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; th.setData({ open_ind_store: ind }); @@ -848,98 +860,144 @@ Page({ }; - // 秒杀活动 - if(this.data.prom_type == 1) { - // 如果是秒杀活动下的单独购买,is_normal为1 - if(this.data.openSpecModal_flash_normal) this.data.is_normal = 1; - - if(!this.data.is_normal) {// 秒杀购买 - - // 获取redis当前可以购买的数量 - // 如果数量为0,设置和显示已抢光 - // 否则,进一步判断是否超出限购或超出库存 - await this.getactLen().then(async function(res) { - let curNum = th.data.goodsInputNum; - // res: redis可购买数量 - console.log('当前可以购买的数量:', res); - if(res <= 0) { - // 可购买数量<=0, 设置和显示已抢光 - th.setData({ - prom_r_null: 1, - }); - wx.showModal({ - title: '超出活动库存', - }); - return false; - } else { - // 可购买数量>0 - // 计算自己还可以购买的数量 - // 自己还可购买的数量c = 每人活动限购数量a - 自己已经购买的数量b - // 如果限购数量a>redis可购买数量d,当增加数量t>d, 提示超出库存 - // 如果限购数量a<=redis可购买数量d, 当增加数量t>a,提示超出限购 - let actInfo = th.data.sele_g; - await th.get_buy_num2().then(function (data) { - let limited = actInfo.buy_limit; // 限购数量a - let promcardbuynum = data.data.data.promcardbuynum; - let buyedNum = promcardbuynum; // 自己已经购买的数量b - let canBuyNum = limited - buyedNum; // 自己还可购买的数量c - - if(canBuyNum <= 0) { - canBuyNum = 0; - }; - - if(limited > res) { - if(curNum > res) { // t当前增减的数量 - wx.showModal({ + if(this.data.prom_type == 1) { + + this.data.sele_g.viplimited = this.data.sele_g.buy_limit; + + // 每人限购数 + th.data.sele_g.viplimited = th.data.sele_g.buy_limit; + + // 已经付款的单数 + var gd_buy_num = !th.data.sele_g.buy_num ? 0 : th.data.sele_g.buy_num; + + // 检查redis库存量 + var redisNum = 0; + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + os.stoid + "/1/" + prom_id, {}).then(res => { + if (res.data.code == 0) { + redisNum = !res.data.data ? 0 : res.data.data; + }; + }); + + // 获取购物车同款数量 判断是否超库存 + 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 ? 0 : tmpObj.goods_num; + } + + }); + + // 有库存的情况下,走秒杀活动流程 否则 走正常购买流程 + // 没有库存,已抢光的情况下,走正常购买流程 + if(redisNum > 0 && !this.data.is_normal) { + // 输入的数量 + var t = th.data.goodsInputNum; + + if (t > redisNum) { + wx.showToast({ title: '超出活动库存', - }); - th.setData({ - goodsInputNum: res, - }); - return false; - }; - }; + icon: 'none' + }); + + // 开启限购 + if (th.data.sele_g.viplimited > 0) { + + let num = th.data.sele_g.viplimited - gd_buy_num; + if (action !="buy") { + // 购物车 + if (num > redisNum) { + th.setData({goodsInputNum: redisNum - cartGoodsNum < 0 ? 0 : redisNum - cartGoodsNum}); + } else { + th.setData({goodsInputNum: num - cartGoodsNum < 0 ? 0 : num - cartGoodsNum}); + } + } else { + // 立即购买 + if (num > redisNum) { + th.setData({goodsInputNum: redisNum}); + } else { + th.setData({goodsInputNum: num}); + } + } + + + } else { + th.setData({goodsInputNum: redisNum}); + } + + return false; + } + - if(limited <= res) { - if(curNum > limited) { - wx.showModal({ - title: '超出限购数量', - }); - th.setData({ - goodsInputNum: canBuyNum, + + // 跳过<立即购买> 购物车 + if (action !="buy") { + + // 输入的数量 + 购物车同款商品的数量 + var maxNum = parseInt(t) + parseInt(cartGoodsNum); + + // 是否开启个人限购 + if (th.data.sele_g.viplimited > 0) { + + if (maxNum + gd_buy_num > th.data.sele_g.buy_limit) { + wx.showToast({ + title: '超出活动限购数量', + icon: 'none' + }); + + // 可购买数量 + let num = th.data.sele_g.viplimited - gd_buy_num - cartGoodsNum; + th.setData({goodsInputNum: num}); + return false; + } + + } + + if (maxNum > redisNum) { + wx.showToast({ + title: '超出活动库存', + icon: 'none' }); + // 可购买数量 + let num = redisNum - cartGoodsNum; + th.setData({goodsInputNum: num}); return false; + } + + } else { + // 以下为立即购买 + // 是否开启个人限购 + if (th.data.sele_g.viplimited > 0) { + + if (t + gd_buy_num > th.data.sele_g.viplimited) { + wx.showToast({ + title: '超出活动限购数量', + icon: 'none' + }); + + let num = th.data.sele_g.viplimited - gd_buy_num; + if (num > redisNum) { + th.setData({goodsInputNum: redisNum}); + } else { + th.setData({goodsInputNum: num}); + } + return false; + } + + }; - }; - - - }); - - - + + } + }; - }); - }; - }; - - - - - - - - - - - - - if(action=="buy"){ + if(action=="buy"){ // 立即购买 //--------------此时操作的数据------------ var newd = { id: th.data.data.id, @@ -961,13 +1019,23 @@ Page({ newd['prom_type'] = 0; newd['prom_price'] = this.data.data.shop_price; }; + + // 判断是否为0库存的情况 + var tmpRedisNum = 0; + // 检查redis库存量 + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + os.stoid + "/1/" + prom_id, {}).then(res => { + if (res.data.code == 0) { + tmpRedisNum = res.data.data; + }; + }); + if(this.data.prom_type == 1 && tmpRedisNum == 0) { + newd['prom_type'] = 0; + newd['prom_price'] = this.data.data.shop_price; + }; // console.log('newd++++++++', newd); th.buyNow(newd); - } else { - - - + } else { // 购物车 var newd = { service_id: th.data.data.id, @@ -994,7 +1062,19 @@ Page({ newd['money'] = th.data.prom_price; }; }; - + + // 判断是否为0库存的情况 + var tmpRedisNum = 0; + // 检查redis库存量 + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + os.stoid + "/1/" + prom_id, {}).then(res => { + if (res.data.code == 0) { + tmpRedisNum = res.data.data; + }; + }); + if(th.data.options.prom_type == 1 && tmpRedisNum == 0) { + newd['prom_type'] = 0; + newd['money'] = th.data.data.shop_price; + }; //----先看会员在购物车中是否加入了该商品----- getApp().request.get("/api/weshop/cartService/page", { @@ -1091,121 +1171,142 @@ Page({ }, //------检查数量是不是超出限购------ - checkCartNum: async function(t) { - var th = this; + checkCartNum: function(t) { - if(!th.data.def_pick_store) { - wx.showModal({title: '请选择门店',}); - return false; - }; - - // 非秒杀活动 if(this.data.prom_type != 1) { - this.setData({ - goodsInputNum: t, - }); + this.setData({goodsInputNum: t}); + return false; }; - // 秒杀活动 - if(this.data.prom_type == 1) { - // 如果是秒杀活动下的单独购买,is_normal为1 - if(this.data.openSpecModal_flash_normal) this.data.is_normal = 1; - - if(this.data.is_normal) {// 单独购买 - this.setData({ - goodsInputNum: t, - }); - } else {// 秒杀购买 - - // 获取redis当前可以购买的数量 - // 如果数量为0,设置和显示已抢光 - // 否则,进一步判断是否超出限购或超出库存 - await this.getactLen().then(async function(res) { - // res: redis可购买数量 - console.log('当前可以购买的数量:', res); - if(res <= 0) { - // 可购买数量<=0, 设置和显示已抢光 - th.setData({ - prom_r_null: 1, - }); - } else { - // 可购买数量>0 - // 计算自己还可以购买的数量 - // 自己还可购买的数量c = 每人活动限购数量a - 自己已经购买的数量b - // 如果限购数量a>redis可购买数量d,当增加数量t>d, 提示超出库存 - // 如果限购数量a<=redis可购买数量d, 当增加数量t>a,提示超出限购 - let actInfo = th.data.sele_g; - await th.get_buy_num2().then(function (data) { - let limited = actInfo.buy_limit; // 限购数量a - let promcardbuynum = data.data.data.promcardbuynum; - let buyedNum = promcardbuynum; // 自己已经购买的数量b - let canBuyNum = limited - buyedNum; // 自己还可购买的数量c - - if(canBuyNum <= 0) { - canBuyNum = 0; - }; - - if(limited > res) { - if(t > res) { // t当前增减的数量 - wx.showModal({ - title: '超出活动库存', - }); - th.setData({ - goodsInputNum: res, - }); - return false; - }; - }; - - if(limited <= res) { - if(t>limited) { - wx.showModal({ - title: '超出限购数量', - }); - th.setData({ - goodsInputNum: canBuyNum, - }); - return false; - }; - }; - - th.setData({ - goodsInputNum: t, - }); - }); - }; - }); - - } - - - }; + var th = this; + // if(this.data.prom_act.is_shop_buy && this.data.options.prom_type != 1) { + + if(t == 0) return false; + // th.setData({goodsInputNum:t}); - + // if(this.data.options.prom_type == 1 && !this.data.openSpecModal_flash_normal) { + + this.get_buy_num(this.data.sele_g, async function () { + th.data.sele_g.viplimited = th.data.sele_g.buy_limit; + + //--判断商品是否超出限购-- + // if (th.data.g_buy_num != null && th.data.sele_g.viplimited > 0) { + // if(!th.data.prom_act.is_shop_buy) { + + // if (th.data.sele_g.viplimited > 0) { + // var gd_buy_num = th.data.sele_g.buy_num; + + // if (t + gd_buy_num> th.data.sele_g.viplimited) { + // wx.showModal({ + // title: '超出商品限购', + // }); + + // var num = th.data.sele_g.viplimited - gd_buy_num; + // if (num <= 0) num = 1; + // th.setData({goodsInputNum: num}) + // return false; + // } + // }; + + + // 单独购买 + if(th.data.openSpecModal_flash_normal) th.data.is_normal = 1; + + // 秒杀:判断活动是否抢光 + if (th.data.sele_g.prom_type == 1 && !th.data.is_normal) { + var redis_num = 0; + //------判断活动是否抢光----- + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + + os.stoid + "/" + th.data.sele_g.prom_type + "/" + th.data.sele_g.prom_id, { + 1: 1 + }).then(res => { + redis_num = res.data.data; + }); + + if (t > redis_num) { + wx.showToast({ + // title: '超出商品活动库存', + title: '超出活动库存', + icon: 'none', + }); + th.setData({goodsInputNum: redis_num}) + return false; + } + }; + //--秒杀:判断商品是否超出活动限购-- + if (th.data.sele_g.viplimited > 0 && !th.data.is_normal && th.data.prom_type == 1) { + var gd_buy_num = th.data.promcardbuynum; + + // 如果限购数量>活动库存数量,增加数量时会先超过库存数量,此时应该提示“超出活动库存”; + // 如果限购数量<=活动库存数量,增加数量时会先超过限购数量,此时应提示“超出活动限购数量” + if(th.data.sele_g.viplimited > th.data.sele_g.goods_num) { + + // 计算还可以购买的数量:库存量 - 已购数 + var num = th.data.sele_g.goods_num - gd_buy_num; + if(num < 0) num = 0; + if(t > num) { + wx.showToast({ + title: '超出活动库存', + icon: 'none', + }); + + th.setData({goodsInputNum: num}) + return false; + + }; + + } else { + if ((t + gd_buy_num> th.data.sele_g.viplimited) && (t<=th.data.sele_g.goods_num)) { + wx.showToast({ + title: '超出活动限购数量', + icon: 'none', + }); + + var num = th.data.sele_g.viplimited - gd_buy_num; + if (num <= 0) num = 1; + th.setData({goodsInputNum: num}) + return false; + }; + }; + + + + }; - + //--秒杀:判断商品是否超出活动限购-- + // if (th.data.prom_buy_num != -1 && th.data.prom_buy_limit > 0 && !th.data.is_normal) { + // if (t + th.data.prom_buy_num > th.data.prom_buy_limit) { + // wx.showModal({ + // title: '超出商品活动限购', + // }); + + // var num = th.data.prom_buy_limit - th.data.prom_buy_num; + // if (num < 0) num = 0; + // th.setData({goodsInputNum: num}) + // return false; + // }; + // }; // var e = th.data.sele_g.goods_num; - // var p_type = th.data.prom_type; //&& p_type!=1 && p_type!=4 - // if (th.data.sales_rules == 2 && (p_type != 1 && p_type != 4 && p_type != 6 || th.data.openSpecModal_inte_normal == 1 || th.data.is_normal == 1)) { - // if (!th.data.def_pick_store) { - // wx.showModal({title: '请选择门店',}); - // return false; - // } else { - // e = th.data.def_pick_store.CanOutQty; - // } - // } - - - // th.setData({goodsInputNum: t}); + var p_type = th.data.prom_type; //&& p_type!=1 && p_type!=4 + if (th.data.sales_rules == 2 && (p_type != 1 && p_type != 4 && p_type != 6 || th.data.openSpecModal_inte_normal == 1 || th.data.is_normal == 1)) { + if (!th.data.def_pick_store) { + wx.showModal({title: '请选择门店',}); + return false; + } else { + // e = th.data.def_pick_store.CanOutQty; + } + } + + th.setData({goodsInputNum: t}); - // }); + }); }, @@ -1237,10 +1338,12 @@ Page({ return false; } - var ind = t.currentTarget.dataset.ind; + var ind = t.currentTarget.dataset.ind; + this.setData({ openSpecModal: !0, - openSpecModal_ind: ind, + openSpecModal_ind: ind, + goodsInputNum: 1, }); }, @@ -3197,18 +3300,19 @@ Page({ //获取redis中的数量 - async getactLen() { - let prom_type = this.data.options.prom_type; - let prom_id = this.data.options.prom_id; - return await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + os.stoid + "/" + prom_type + "/" + prom_id, { + async getactLen(func) { + var r_num = 0, + prom_type = this.data.prom_type, + prom_id = this.data.prom_id; + await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" + os.stoid + "/" + prom_type + "/" + prom_id, { 1: 1 }).then(res => { - if (res.data.code == 0) { - // 当前可以购买的数量 - let r_num = res.data.data; - return r_num; - }; + var em = res; + if (em.data.code == 0) { + r_num = em.data.data; + } }) + func(r_num); }, @@ -3447,7 +3551,7 @@ Page({ //----获取商品购买数---- if (th.data.prom_type == 1) { //----获取活动购买数---- - getApp().request.promiseGet("/api/weshop/rechargeServicelist/getUserBuyGoodsNum", { + getApp().request.get("/api/weshop/rechargeServicelist/getUserBuyGoodsNum", { data: { store_id: os.stoid, user_id: user_id, @@ -3475,49 +3579,6 @@ Page({ }, - get_buy_num2: async function () { - // var map = this.data.g_buy_num, - var th = this, - user_id = getApp().globalData.user_id; - // if (user_id == null) { - // // map.set(gd.goods_id, 0); - // th.setData({ - // // g_buy_num: map, - // prom_buy_num: 0, - // }); - // "function" == typeof func && func(); - // return false; - // } - - - //----获取商品购买数---- - - //----获取活动购买数---- - return await getApp().request.promiseGet("/api/weshop/rechargeServicelist/getUserBuyGoodsNum", { - data: { - store_id: os.stoid, - user_id: user_id, - card_id: th.data.options.goods_id, - prom_type: th.data.options.prom_type, - prom_id: th.data.options.prom_id - }, - //-----获取----- - success: function (tt) { - if (tt.data.code == 0) { - // map.set(gd.goods_id, g_buy_num); - th.setData({ - // g_buy_num: map, - promcardbuynum: tt.data.data.promcardbuynum, - cardbuynum: tt.data.data.cardbuynum, - }); - } - } - }); - - - }, - -