diff --git a/packageE/pages/cart/cart2/cart2.js b/packageE/pages/cart/cart2/cart2.js index 89ba542..2269f8f 100644 --- a/packageE/pages/cart/cart2/cart2.js +++ b/packageE/pages/cart/cart2/cart2.js @@ -7122,7 +7122,6 @@ Page({ //-- 判读一下促销分组的问题 -- async cart_cx_group(goods,car_item,func){ - //更改活动的时候,就不能再重新计算活动,只有再加减数量和选择商品的时候,就要重新计算活动 if(this.data.change_act){ var show_can_cx=car_item.show_can_cx; @@ -7165,6 +7164,30 @@ Page({ k_item.sele_prom_type=ck_prom_type.prom_type; + //-- 如果是要控制数量的时候,也就是全选的时候,或者加减号的时候 -- + if(is_need_ck_num){ + var t_pid=ck_prom_type.prom_id; + switch (ck_prom_type.prom_type){ + case 3: + if(!th.data.in_yhact_new[t_pid]) th.data.in_yhact_new[t_pid]=0; + th.data.in_yhact_new[t_pid]++; + break; + case 7: + if(!th.data.in_zhact_new[t_pid]) th.data.in_zhact_new[t_pid]=0; + th.data.in_zhact_new[t_pid]+=ck_prom_type.act_num; + //-- 数组的优化 -- + var nei_gd_map=ck_prom_type.in_zhact_gdmap; + if(nei_gd_map){ + for (var vg in nei_gd_map) { + if (!th.data.in_zhact_gdmap_new[vg]) th.data.in_zhact_gdmap_new[vg] = 0; + th.datain_zhact_gdmap_new[vg] += nei_gd_map[vg]; + } + } + break; + } + } + + //-- 此时要把购物车中的商品确定活动 -- for (var b=0; b 0){ + //-- 获取已经购买了多少件 -- + var url = "/api/weshop/ordergoods/getUserBuyPromNum?store_id="+os.stoid+"&user_id=" + +getApp().globalData.user_id+"&prom_type=3&prom_id="+zh_pro0.id; + await getApp().request.promiseGet(url, {}).then(rh => { + if(rh.data.code==0){ + item_act_map.buyed=rh.data.data.userbuynum; + } + }) + if(item_act_map.buyed+need_num>=fd['limit_num']){ + continue; //-- 进行下一个循环 -- + } + } + var fir_act=item_act_map.act.fir_act; var t_goods=item_act_map.goods; var all_num=0; @@ -7295,16 +7350,19 @@ Page({ if(!car_item.zh_prom_goods) car_item.zh_prom_goods={}; car_item.zh_prom_goods[item_act_map.prom_id] = zhact_gdlist; - var zh_pro0 = null; + var zh_pro0 = th.data.zhhe_act_map[item_act_map.prom_id]; //-- 获取活动信息 -- - var url = "/api/weshop/prom/zhbuy/get/" + os.stoid + "/" + item_act_map.prom_id + "/" + getApp().globalData.user_id; + var url = "/api/weshop/ordergoods/getUserBuyPromNum?store_id="+os.stoid+"&user_id=" + +getApp().globalData.user_id+"&prom_type=7&prom_id="+zh_pro0.id; await getApp().request.promiseGet(url, {}).then(rh => { - zh_pro0 = rh.data.data; + if(rh.data.code==0){ + item_act_map.buyed=rh.data.data.userbuynum; + } }) - if(!zh_pro0) break; + //当满足组合的要求:总数要满足,起购数要满足 - if(zh_calc.ch_zh_ok(zh_pro0,zhact_gdlist,zh_goods,th)){ - can_calc_cx.push(item_act_map) + if(zh_calc.ch_zh_ok(zh_pro0,zhact_gdlist,zh_goods,th,item_act_map,is_need_ck_num)){ + can_calc_cx.push(item_act_map) } break; default: @@ -7385,6 +7443,30 @@ Page({ k_item.sele_prom_type=ck_prom_type.prom_type; + //-- 如果是要控制数量的时候,也就是全选的时候,或者加减号的时候 -- + if(is_need_ck_num){ + var t_pid=ck_prom_type.prom_id; + switch (ck_prom_type.prom_type){ + case 3: + if(!th.data.in_yhact_new[t_pid]) th.data.in_yhact_new[t_pid]=0; + th.data.in_yhact_new[t_pid]++; + break; + case 7: + if(!th.data.in_zhact_new[t_pid]) th.data.in_zhact_new[t_pid]=0; + th.data.in_zhact_new[t_pid]+=ck_prom_type.act_num; + //-- 数组的优化 -- + var nei_gd_map=ck_prom_type.in_zhact_gdmap; + if(nei_gd_map){ + for (var vg in nei_gd_map) { + if (!th.data.in_zhact_gdmap_new[vg]) th.data.in_zhact_gdmap_new[vg] = 0; + th.data.in_zhact_gdmap_new[vg] += nei_gd_map[vg]; + } + } + break; + } + } + + //-- 此时要把购物车中的商品确定活动 -- for (var b=0; b-1 - && g_item.pick_id==pk - }) - if(fg>-1){ - goods[fg].prom_type=ck_prom_type.prom_type; - goods[fg].prom_id=ck_prom_type.prom_id; + + this.cx_check_ok2(function (ck_res){ + + if(ck_res.code==0){ + th.data.cartlist[idx].show_can_cx[gd_key].sele_prom_type=ck_prom_type.prom_type; + var goods= th.data.cartlist_y + //-- 此时要把购物车中的商品确定活动 -- + for (var b=0; b-1 + && g_item.pick_id==pk + }) + if(fg>-1){ + goods[fg].prom_type=ck_prom_type.prom_type; + goods[fg].prom_id=ck_prom_type.prom_id; + } + } + + wx.showLoading({ + title: "处理中.", + mask: true + }) + //-- 重新计算一下价格 -- + th.get_cart_next(null,1); + }else{ + wx.showToast({ + title: ck_res.msg, + icon: 'none', + duration:2500 + }); + + for(var dx=0;dx 0 && num > fd['limit_num']) { + func({'code': -1, 'msg': '超出优惠促销活动的限购'}); + return false; + } + } + } + + //-- 判断组合促销是不是超出活动总数量 -- + if (nei_in_zhact && Object.keys(nei_in_zhact).length) { + for (var ky in nei_in_zhact) { + + var num0 = nei_in_zhact[ky]; + var zh = this.data.zhhe_act_map[ky]; + var buyed=0; + + //获取已经购买了多少件 + var z_url = "/api/weshop/ordergoods/getUserBuyPromNum?store_id="+os.stoid+"user_id="+getApp().globalData.user_id+"prom_type=7&prom_id="+ky; + var zbuy=await getApp().request.promiseGet(z_url,{}); + if(zbuy && zbuy.data.code==0){ + buyed=zbuy.data.data.userbuynum; + } + if (zh['zh_num'] > 0 && zh['zh_buy_num'] + num0 > zh['zh_num']) { + func({'code': -1, 'msg': '超出组合购活动总数量'}); + return false; + } + } + } + //-- 判断组合促销是不是超出活动总数量 -- + if (nei_in_zhact_gdmap && Object.keys(nei_in_zhact_gdmap).length) { + for (var key in nei_in_zhact_gdmap) { + + var num1 = nei_in_zhact_gdmap[key]; + var key_a = key.split('.'); + var gid = key_a[0]; + var pid = key_a[1]; + + //-- 商品的限购 -- + var promgoodsbuynum = 0; + var rq = { + 'store_id': os.stoid, 'user_id': getApp().globalData.user_id, 'goods_id': gid, + 'prom_type': 7, 'prom_id': pid + }; + + var res = await getApp().request.promiseGet("/api/weshop/ordergoods/getUserBuyGoodsNum", {data: rq}); + if (res && res.data.code == 0) { + promgoodsbuynum = res.data.data.promgoodsbuynum; + } + + var gdlist_arr = this.data.zuhe_map_good[pid]; + var gdlist = gdlist_arr.find(function (e) { + return e.goods_id == gid; + }) + if (gdlist['buyqty'] > 0 && num1 + promgoodsbuynum > gdlist['buyqty']) { + func({'code': -1, 'msg': '超出组合购商品的限购'}); + return false; + } + } + } + func({'code': 0, 'msg': '成功'}) } diff --git a/packageE/pages/cart/cart2/zh_calculate.js b/packageE/pages/cart/cart2/zh_calculate.js index e709d4f..ceee93a 100644 --- a/packageE/pages/cart/cart2/zh_calculate.js +++ b/packageE/pages/cart/cart2/zh_calculate.js @@ -155,7 +155,7 @@ module.exports = { var delete_num=0; //看一下是几倍 - let be1 = parseInt(no_in_arr.length / act.zhbuyqty); + let be = parseInt(no_in_arr.length / act.zhbuyqty); //如果有总数控制的时候 if(act.zh_num){ var be1=act.zh_num-act.zh_buy_num-1; @@ -188,7 +188,7 @@ module.exports = { let new_arr = zhqty_bz_arr.filter(ii => { return ii['be'] == min_bz_num; }) - var be = new_arr[0].be; + be = new_arr[0].be; for (let i = 0; i < zhqty_bz.length; i++) { let item1 = zhqty_bz[i]; @@ -506,8 +506,9 @@ module.exports = { return zhqty_bz_flag; }, - //-- 判断组合购有没有满足 --- - ch_zh_ok(act,gdlist,c_goods,th){ + //-- 判断组合购有没有满足,只有在检查的时候,要用新的 --- + ch_zh_ok(act,gdlist,c_goods,th,item_act_map,is_need_ck_num){ + //-- 寻找一下 -- function get_num(ite) { for (let v1 in c_goods) { @@ -529,19 +530,39 @@ module.exports = { let no_in_arr = []; //剩余的未加入组合购 let out_arr = []; //超出活动限购的商品放入 - //-- 判断组合购的数量是不是到了 -- + //-- 判断组合购的总数量是不是到了 -- if(act.zh_num>0){ - if(th.data.in_zhact_new[aid] && th.data.in_zhact_new[aid]+act.zh_buy_num>=act.zh_num ){ - return false; - }else{ - min_can_buy= act.zh_num-act.zh_buy_num-th.data.in_zhact_new[aid]; - } + //如果是在全选的情况下,那么就需要in_zhact_new来控制数量 + var need_ck=0; + if(is_need_ck_num){ + need_ck=th.data.in_zhact_new[aid]?th.data.in_zhact_new[aid]:0; + } + if(need_ck+act.zh_buy_num>=act.zh_num ){ + return false; + }else{ + min_can_buy= act.zh_num-act.zh_buy_num-need_ck; + } } - if(act.buy_limit>0){ + //-- 判断组合购的限购是不是到了 -- + if(act.buy_limit>0){ + var need_ck0=0; + if(is_need_ck_num){ + need_ck0=th.data.in_zhact_new[aid]?th.data.in_zhact_new[aid]:0; + } + var min_can_buy0=0; + if(need_ck+item_act_map.buyed>=act.buy_limit ){ + return false; + }else{ + min_can_buy0=act.buy_limit-item_act_map.buyed-need_ck; + } + if(min_can_buy>0){ + if(min_can_buy0 0) { var cbuy = item.buyqty - zh_b_num; var key = item.goods_id + "." + act.id; - if (th.data.in_zhact_gdmap[key]) { + if (is_need_ck_num && th.data.in_zhact_gdmap_new[key]) { var cbuy = item.buyqty - zh_b_num - th.data.in_zhact_gdmap_new[key]; } //当可买的数量为0 @@ -614,6 +635,8 @@ module.exports = { //当满足组合的要求:总数要满足,起购数要满足 if (all_num >= act.zhbuyqty && !need_to_buy) { + //默认数量是1 + var act_num=1; function sortData(a, b) { return a.price - b.price } @@ -626,15 +649,15 @@ module.exports = { //看一下剩下的数量有没有满足组合购的要求,以及要不要倍增 if (act.is_bz && no_in_arr.length >= act.zhbuyqty) { - var act_num=1; + var bz_num_ok = 0; //超量倍增是否满足 var zhqty_len = 0; //几个超量倍增 var delete_num = 0; //看一下是几倍 let be = parseInt(no_in_arr.length / act.zhbuyqty); //如果有总数控制的时候 - if (act.zh_num) { - var be1 = act.zh_num - act.zh_buy_num - 1; + if (min_can_buy>0) { + var be1 = min_can_buy - 1; if (be1 < be) be = be1; } if(be) { @@ -663,9 +686,13 @@ module.exports = { let new_arr = zhqty_bz_arr.filter(ii => { return ii['be'] == min_bz_num; }) - //-- 要看一下倍数还有没有 --- be = new_arr[0].be; + //如果有总数控制的时候 + if (min_can_buy>0) { + var be1 = min_can_buy - 1; + if (be1 < be) be = be1; + } if(be){ for (let i = 0; i < zhqty_bz.length; i++) { let item1 = zhqty_bz[i]; @@ -692,6 +719,8 @@ module.exports = { } } + //-- 参与活动的金额要返回回去 -- + item_act_map.act_num=act_num; let goods_map = {}; //算一下剩余的商品 if (no_in_arr.length) { @@ -718,25 +747,29 @@ module.exports = { } } - + //-- 组合购的商品限购的数组控制 -- + var in_zhact_gdmap={}; //商品的列表 for (var u in goods) { var ite = goods[u]; var cart_num = get_num(ite).goods_num; - if (ite.buyqty > 0 && ite.cart_num > 0) { + if (ite.buyqty > 0 && cart_num > 0) { var key = ite.goods_id + "." + act.id; - - if(goods_map[ite.goods_id]){} - - - if (th.data.in_zhact_gdmap_new[key]) { - th.data.in_zhact_gdmap_new[key] += ite.num; - } else { - th.data.in_zhact_gdmap_new[key] = ite.num; + if(goods_map[ite.goods_id]){ + cart_num-=goods_map[ite.goods_id]; } + if(cart_num>0){ + if (in_zhact_gdmap[key]) { + in_zhact_gdmap[key] += ite.num; + } else { + in_zhact_gdmap[key] = ite.num; + } + } + } } + item_act_map.in_zhact_gdmap=in_zhact_gdmap; return true; } return false; diff --git a/pages/cart/cart/cart.js b/pages/cart/cart/cart.js index 76cea98..0fd9620 100644 --- a/pages/cart/cart/cart.js +++ b/pages/cart/cart/cart.js @@ -49,9 +49,11 @@ Page({ ladder_map: {}, //阶梯促销需要的map promgoods_map: {}, - promgoods_list: {} - + promgoods_list: {}, + in_zhact_gdmap_new: {}, //不同门店参与同一活动的限购,这些都是map表,是多促销互动专用,不能和in_zhact_gdmap重叠 + in_zhact_new:{}, //组合购活动在列表中的显示 是多促销互动专用,不能和in_zhact重叠 + in_yhact_new:{}, //组合购活动在列表中的显示 是多促销互动专用,不能和in_yhact重叠 }, @@ -109,7 +111,6 @@ Page({ wx.setNavigationBarTitle({ title: "购物车", }) - //判断是否有登录 t.auth.hadAuth() && t.getUserInfo(function () { //a.getCardList(); @@ -499,12 +500,10 @@ Page({ //按门店分类的数组 var arr = new Array(), carr = su.data.data.pageData; var all_num = 0; - - + if (carr && carr.length > 0) { var g_arr = []; for (var i = 0; i < carr.length; i++) { - //-- 清理购物车的选中状态 -- var url = '/api/weshop/cart/update'; getApp().request.put(url, { @@ -1100,7 +1099,6 @@ Page({ } }, - //删除商品 delete_th_item: function (t) { var e = this; @@ -1148,7 +1146,7 @@ Page({ } }, - //-------------加数量--------------------- + //-------------加数量------------- addNum: function (t) { if (!this.data.is_load) return false; if (this.data.up_dating == 1) return false; @@ -1206,8 +1204,6 @@ Page({ return false; } - - if (1 != a.goods_num) { var e = { goods_num: a.goods_num - 1, @@ -1342,16 +1338,19 @@ Page({ this.data.btn_click = 1; - - var offline_price = 0; var offline_num = 0; - th.data.in_zhact_gdmap = {}; + th.data.in_zhact_gdmap = {}; if (dda && dda.length > 0) { for (var i = 0; i < dda.length; i++) { var item = dda[i].goods; + var txt0 = "requestData[" + i + "].show_can_cx"; + e.setData({ + [txt0]: {}, + }); + if (e.data.checkAllToggle) { var txt = "requestData[" + i + "].selected"; @@ -1362,14 +1361,13 @@ Page({ var fir = 0; for (var j = 0; j < item.length; j++) { if (item[j].is_gift) continue; - var obj = JSON.parse(JSON.stringify(item[j])); - //计算之前先移除组合购的计算 - if (obj.prom_type == 7) - zh_calc.remove_zhprom(dda, i, obj); - //计算之前先移除阶梯购的计算 - if (obj.prom_type == 10) - ladder_calc.remove_ladder_prom(dda, i, obj); + // //计算之前先移除组合购的计算 + // if (obj.prom_type == 7) + // zh_calc.remove_zhprom(dda, i, obj); + // //计算之前先移除阶梯购的计算 + // if (obj.prom_type == 10) + // ladder_calc.remove_ladder_prom(dda, i, obj); if (fir == 0) { @@ -1385,28 +1383,26 @@ Page({ if (e.data.checkAllToggle) obj.goods_num = 0; - //加入组合购计算 - if (obj.prom_type == 7) zh_calc.add_zhprom(dda, i, obj); - //加入阶梯促销计算 - if (obj.prom_type == 10) ladder_calc.add_ladder_prom(dda, i, obj); + // //加入组合购计算 + // if (obj.prom_type == 7) zh_calc.add_zhprom(dda, i, obj); + // //加入阶梯促销计算 + // if (obj.prom_type == 10) ladder_calc.add_ladder_prom(dda, i, obj); e.setData({ - [txt]: !e.data.checkAllToggle, }) - - if (!e.data.checkAllToggle) { - //组合购的金额要另外算 - if (item[j].prom_type != 7 && item[j].prom_type != 10) - tfeel += item[j].goods_num * item[j].goods_price; - t_num += item[j].goods_num; - } - - //-- 如果这个商品是线下取价的时候 -- - if (item[j].is_offline && item[j].prom_type != 7) { - offline_price += (item[j].goods_price - item[j].offline_price) * item[j].goods_num; - offline_num += item[j].goods_num; - } + // if (!e.data.checkAllToggle) { + // //组合购的金额要另外算 + // if (item[j].prom_type != 7 && item[j].prom_type != 10) + // tfeel += item[j].goods_num * item[j].goods_price; + // t_num += item[j].goods_num; + // } + // + // //-- 如果这个商品是线下取价的时候 -- + // if (item[j].is_offline && item[j].prom_type != 7) { + // offline_price += (item[j].goods_price - item[j].offline_price) * item[j].goods_num; + // offline_num += item[j].goods_num; + // } } } @@ -1430,44 +1426,46 @@ Page({ } //总的价格,把组合商品的价格拿出来 - var zh_calc_res = zh_calc.calculate_zh(dda, i, this); - tfeel += zh_calc_res.tfeel; - offline_price += zh_calc_res.offline_price; - offline_num += zh_calc_res.offline_num; - - //总的价格,把阶梯促销的商品的价格拿出来 - var ladder_calc_res = ladder_calc.calculate_ladder(dda, i, this); - tfeel += ladder_calc_res.tfeel; - offline_price += ladder_calc_res.offline_price; - offline_num += ladder_calc_res.offline_num; - - //当有线下取价的时候 - if (offline_price) { - var txt1 = "requestData[" + i + "].offline_price"; - var txt2 = "requestData[" + i + "].offline_num"; - th.setData({ - [txt1]: offline_price.toFixed(2), - [txt2]: offline_num, - }); - } else { - if (dda[i].offline_price > 0) { - var txt1 = "requestData[" + i + "].offline_price"; - var txt2 = "requestData[" + i + "].offline_num"; - th.setData({ - [txt1]: 0, - [txt2]: 0 - }); - } - } + // var zh_calc_res = zh_calc.calculate_zh(dda, i, this); + // tfeel += zh_calc_res.tfeel; + // offline_price += zh_calc_res.offline_price; + // offline_num += zh_calc_res.offline_num; + // + // //总的价格,把阶梯促销的商品的价格拿出来 + // var ladder_calc_res = ladder_calc.calculate_ladder(dda, i, this); + // tfeel += ladder_calc_res.tfeel; + // offline_price += ladder_calc_res.offline_price; + // offline_num += ladder_calc_res.offline_num; + // + // //当有线下取价的时候 + // if (offline_price) { + // var txt1 = "requestData[" + i + "].offline_price"; + // var txt2 = "requestData[" + i + "].offline_num"; + // th.setData({ + // [txt1]: offline_price.toFixed(2), + // [txt2]: offline_num, + // }); + // } else { + // if (dda[i].offline_price > 0) { + // var txt1 = "requestData[" + i + "].offline_price"; + // var txt2 = "requestData[" + i + "].offline_num"; + // th.setData({ + // [txt1]: 0, + // [txt2]: 0 + // }); + // } + // } } e.setData({ checkAllToggle: !e.data.checkAllToggle, - total_fee: tfeel.toFixed(2), - total_num: t_num, + // total_fee: tfeel.toFixed(2), + // total_num: t_num, }); - e.all_check_prom_activity(); + this.doCheckAll(function (){ + e.all_check_prom_activity(); + }); } } @@ -1595,34 +1593,60 @@ Page({ title: '购买数量超出商品库存', icon: 'none', }); - return false; } } } - } - - - e.setData({ - [txt]: Number(!sele), - }); } - var txt = "requestData[" + pitems + "].selected"; - e.setData({ - [txt]: Number(!sele), - }); - e.data.duo_zp_num_arr = {}; - if (sele) { - await e.check_prom_activity(pitems); - } - for (var i in e.data.requestData) { - if (sele && i == pitems) continue; - await e.check_prom_activity(i); + //-- 选择了什么商品,就用什么商品来判断 -- + var sele_arr={}; + for (var i = 0; i < item.length; i++) { + sele_arr[i] = !sele; } - this.doCheckAll(); + + //-- 回调判断活动有没有冲突 -- + this.cx_check_ok(pitems,sele_arr,function (the_res) { + if(the_res.code==0) { + + + //-- 门店是所有的商品都要选择 -- + for (var i = 0; i < item.length; i++) { + var txt0 = "requestData[" + pitems + "].goods[" + i + "].selected"; + e.setData({ + [txt0]: Number(!sele), + }); + } + + //-- 门店按钮的选择 -- + var txt = "requestData[" + pitems + "].selected"; + e.setData({ + [txt]: Number(!sele), + }); + + th.doCheckAll(async function () { + + + + e.data.duo_zp_num_arr = {}; + if (sele) { + await e.check_prom_activity(pitems); + } + for (var i in e.data.requestData) { + if (sele && i == pitems) continue; + await e.check_prom_activity(i); + } + }); + }else{ + wx.showToast({ + title: the_res.msg, + icon: 'none', + }); + } + }) + }, //---------------单选,也要判断门店的配送方式是否一致-------------- check_th_item: async function (t) { @@ -1721,26 +1745,45 @@ Page({ } } } - - } - - if (a.prom_type == 3) { - e.data.duo_zp_num_arr = {}; - if (!a.selected) { - await e.check_prom_activity(pitems); - } - //判断商品是不是优惠活动 - for (var i in e.data.requestData) { - if (!a.selected && i == pitems) continue; - await e.check_prom_activity(i); - } } + var txt0 = "requestData[" + pitems + "].show_can_cx"; e.setData({ - [txt]: Number(!a.selected), + [txt0]: {}, }); - this.doCheckAll(); + //-- 选择了什么商品,就用什么商品来判断 -- + var sele_arr={}; + sele_arr[item]=!a.selected; + //回调判断活动有没有冲突 + this.cx_check_ok(pitems,sele_arr,function (the_res){ + if(the_res.code==0){ + th.setData({ + [txt]: Number(!a.selected), + }); + //要先处理了,再来判断要不要显示凑单 + th.doCheckAll(async function (){ + if (a.prom_type == 3) { + e.data.duo_zp_num_arr = {}; + if (!a.selected) { + await e.check_prom_activity(pitems); + } + //判断商品是不是优惠活动 + for (var i in e.data.requestData) { + if (!a.selected && i == pitems) continue; + await e.check_prom_activity(i); + } + } + + }); + }else{ + wx.showToast({ + title: the_res.msg, + icon: 'none', + }); + } + }) + } }, @@ -1831,7 +1874,7 @@ Page({ }, //----------检查是否全选--------- - doCheckAll:async function () { + doCheckAll:async function (func) { var th = this, tfeel = 0, t_num = 0, @@ -1841,6 +1884,11 @@ Page({ all_num = 0; + //-- 如果是全部的时候,就要把它清理掉 -- + this.data.in_zhact_gdmap_new={}; + this.data.in_zhact_new={}; + this.data.in_yhact_new={}; + //-- 购物车 -- if (car && car.length > 0) { th.data.in_zhact_gdmap = {}; @@ -1848,7 +1896,7 @@ Page({ var item = car[a].goods, is_s_sele = 1; //-- 先批量判断一下活动的类型,同时要同步跟新 -- - await th.cart_cx_group(item,car[a],(show_can_cx)=>{ + await th.cart_cx_group(item,car[a],1,(show_can_cx)=>{ var offline_price = 0; var offline_num = 0; for (var c = 0; c < item.length; c++) { @@ -1949,11 +1997,10 @@ Page({ } } }); - - } //-- 切换活动的按钮要把他返回 -- this.data.change_act=0; + if(func) func(); } //-- 服务卡 -- @@ -1975,14 +2022,10 @@ Page({ } } - var txt = "service_data[" + a + "].selected"; th.setData({ [txt]: Number(is_s_sele) }); - - - } } @@ -2016,7 +2059,9 @@ Page({ e.setData({ [txt]: store_count }); + e.doCheckAll(); + t.goods_num = store_count; } e.update_cart(t, pitem, item); @@ -2547,8 +2592,6 @@ Page({ }; }, - - //---检验线下库存的数量--- async check_down_line(t, pitem, item, erpwareid) { var ob = {}, @@ -4162,10 +4205,9 @@ Page({ }, //-- 判读一下促销分组的问题 -- - async cart_cx_group(goods,car_item,func){ - + async cart_cx_group(goods,car_item,is_need_ck_num,func){ //更改活动的时候,就不能再重新计算活动,只有再加减数量和选择商品的时候,就要重新计算活动 - if(this.data.change_act){ + if(this.data.change_act || (car_item.show_can_cx && Object.keys(car_item.show_can_cx).length>0)){ var show_can_cx=car_item.show_can_cx; for(var k in show_can_cx){ var k_item=show_can_cx[k]; @@ -4244,7 +4286,7 @@ Page({ }else{ var p_item={key:key,prom_type:n_item.prom_type,prom_id:n_item.prom_id,goods:[],act:n_item}; if(n_item.prom_type==7){ - p_item.zh_act=n_item.zh_pro; + p_item.zh_pro=n_item.zh_pro; } p_item.goods.push(c_item); cx_act_map.push(p_item); @@ -4277,6 +4319,34 @@ Page({ } switch(item_act_map.prom_type){ case 3: + //活动ID + var aid=item_act_map.prom_id; + var need_num=0; + //-- 这个时候要判断是不是要判断优惠促销多门店的时候的限购 -- + if(is_need_ck_num){ + need_num=th.data.in_yhact_new[aid]?th.data.in_yhact_new[aid]:0; + } + //-- 判断优惠促销要不要限购 -- + var fd=null; + var fd_res = await getApp().request.promiseGet('/api/weshop/promgoods/get/'+os.stoid+'/'+ky0,{}) + if(fd_res && fd_res.data.code==0 ){ + fd=fd_res.data.data; + } + //如果有限购的时候,也要计算一下 + if (fd && fd['limit_num'] > 0){ + //-- 获取已经购买了多少件 -- + var url = "/api/weshop/ordergoods/getUserBuyPromNum?store_id="+os.stoid+"&user_id=" + +getApp().globalData.user_id+"&prom_type=3&prom_id="+zh_pro0.id; + await getApp().request.promiseGet(url, {}).then(rh => { + if(rh.data.code==0){ + item_act_map.buyed=rh.data.data.userbuynum; + } + }) + if(item_act_map.buyed+need_num>=fd['limit_num']){ + continue; //-- 进行下一个循环 -- + } + } + var fir_act=item_act_map.act.fir_act; var t_goods=item_act_map.goods; var all_num=0; @@ -4285,7 +4355,6 @@ Page({ all_num+=t_goods[f].goods_num; all_pri+=t_goods[f].goods_num*t_goods[f].goods_price; } - var yh_ok=1; switch (fir_act.prom_type){ case 0: @@ -4299,7 +4368,6 @@ Page({ } break; } - //-- 如果有满足优惠条件的时候 -- if(yh_ok){ can_calc_cx.push(item_act_map) @@ -4310,7 +4378,6 @@ Page({ //-- 活动列表 -- var zhact_gdlist=item_act_map.act.gdlist; var zh_goods=item_act_map.goods; - //--- 要循环计算一下商品的已购数量 --- for(var h=0;h { - zh_pro0 = rh.data.data; + if(rh.data.code==0){ + item_act_map.buyed=rh.data.data.userbuynum; + } }) - if(!zh_pro0) break; //当满足组合的要求:总数要满足,起购数要满足 - if(zh_calc.ch_zh_ok(zh_pro0,zhact_gdlist,zh_goods,th)){ + if(zh_calc.ch_zh_ok(zh_pro0,zhact_gdlist,zh_goods,th,item_act_map,is_need_ck_num)){ can_calc_cx.push(item_act_map) } break; @@ -4355,8 +4425,8 @@ Page({ //-- 此时来给满足条件的活动进行分组~~相同参与商品和数量的归纳再一起, -- //-- 同时相同商品数量的活动,还要看取那一个来进行计算金额。 默认的时候是按照后台的顺序。-- //-- 有选择的时候,就按照切换选择了什么就按照什么来计算 -- + //-- can_calc_cx的元素(item_act_map)最后是放到act_arr里面去了 -- var show_can_cx={}; - if(can_calc_cx.length){ for(var p=0;p{ + car[c_idx].show_can_cx=show_can_cx; + th.cx_check_ok2(func,car); + }); + + + }, + + //--- 在点击商品和或者门店的时候调用,这个可以在活动切换的时候调用,也可以被单个选择调用--- + async cx_check_ok2(func,car) { + + //如果购物车没有带入,要进行赋值 + if(!car) { + car = JSON.parse(JSON.stringify(this.data.requestData)); + } + + var nei_in_zhact_gdmap = {}; + var nei_in_zhact = {}; + var nei_in_yhact = {}; + for (var i = 0; i < car.length; i++) { + var show_can_cx = car[i].show_can_cx; + for (var j in show_can_cx) { + var sele_act = show_can_cx[j].act_arr.find(function (m) { + return m.sele == 1; + }) + + if (!sele_act) continue; + var pid = sele_act['prom_id']; + if (sele_act['prom_type'] == 3) { + if(!nei_in_yhact[pid]) nei_in_yhact[pid] = 0; + nei_in_yhact[pid]++; + } + if (sele_act['prom_type'] == 7) { + if(!nei_in_zhact[pid]) nei_in_zhact[pid] = 0; + nei_in_zhact[pid] += sele_act['act_num']; + } + var in_zhact_gdmap1 = sele_act['in_zhact_gdmap']; + for (var vg in in_zhact_gdmap1) { + if (!nei_in_zhact_gdmap[vg]) nei_in_zhact_gdmap[vg] = 0; + nei_in_zhact_gdmap[vg] += in_zhact_gdmap1[vg]; + } + } + } + + //-- 判断优惠促销是不是超出限购 -- + if (nei_in_yhact && Object.keys(nei_in_yhact).length) { + for (var ky0 in nei_in_yhact) { + var num = nei_in_yhact[ky0]; + + var fd=null; + var fd_res = await getApp().request.promiseGet('/api/weshop/promgoods/get/'+os.stoid+'/'+ky0,{}) + if(fd_res && fd_res.data.code==0 ){ + fd=fd_res.data.data; + } + if (fd && fd['limit_num'] > 0 && num > fd['limit_num']) { + func({'code': -1, 'msg': '超出优惠促销活动的限购'}); + return false; + } + } + } + + //-- 判断组合促销是不是超出活动总数量 -- + if (nei_in_zhact && Object.keys(nei_in_zhact).length) { + for (var ky in nei_in_zhact) { + + var num0 = nei_in_zhact[ky]; + var zh = this.data.zuhe_map[ky]; + var buyed=0; + + //获取已经购买了多少件 + var z_url = "/api/weshop/ordergoods/getUserBuyPromNum?store_id="+os.stoid+"user_id="+getApp().globalData.user_id+"prom_type=7&prom_id="+ky; + var zbuy=await getApp().request.promiseGet(z_url,{}); + if(zbuy && zbuy.data.code==0){ + buyed=zbuy.data.data.userbuynum; + } + if (zh['zh_num'] > 0 && zh['zh_buy_num'] + num0 > zh['zh_num']) { + func({'code': -1, 'msg': '超出组合购活动总数量'}); + return false; + } + } + } + + //-- 判断组合促销是不是超出活动总数量 -- + if (nei_in_zhact_gdmap && Object.keys(nei_in_zhact_gdmap).length) { + for (var key in nei_in_zhact_gdmap) { + + var num1 = nei_in_zhact_gdmap[key]; + var key_a = key.split('.'); + var gid = key_a[0]; + var pid = key_a[1]; + + //-- 商品的限购 -- + var promgoodsbuynum = 0; + var rq = { + 'store_id': os.stoid, 'user_id': getApp().globalData.user_id, 'goods_id': gid, + 'prom_type': 7, 'prom_id': pid + }; + + var res = await getApp().request.promiseGet("/api/weshop/ordergoods/getUserBuyGoodsNum", {data: rq}); + if (res && res.data.code == 0) { + promgoodsbuynum = res.data.data.promgoodsbuynum; + } + + var gdlist_arr = this.data.zuhe_map_good[pid]; + var gdlist = gdlist_arr.find(function (e) { + return e.goods_id == gid; + }) + if (gdlist['buyqty'] > 0 && num1 + promgoodsbuynum > gdlist['buyqty']) { + func({'code': -1, 'msg': '超出组合购商品的限购'}); + return false; + } + } + } + func({'code': 0, 'msg': '成功'}) } diff --git a/pages/cart/cart/zh_calculate.js b/pages/cart/cart/zh_calculate.js index c4c07bb..fd92716 100644 --- a/pages/cart/cart/zh_calculate.js +++ b/pages/cart/cart/zh_calculate.js @@ -522,7 +522,8 @@ module.exports = { }, //-- 判断组合购有没有满足,只有在检查的时候,要用新的 --- - ch_zh_ok(act,gdlist,c_goods,th){ + ch_zh_ok(act,gdlist,c_goods,th,item_act_map,is_need_ck_num){ + //-- 寻找一下 -- function get_num(ite) { for (let v1 in c_goods) { @@ -536,6 +537,47 @@ module.exports = { var goods =gdlist; var all_num=0; var need_to_buy=0; + var all_zhqty=0; + //-- 是综合考虑到组合购的总数量和限购数量 -- + var min_can_buy=-1; + var aid=act.id; + + let no_in_arr = []; //剩余的未加入组合购 + let out_arr = []; //超出活动限购的商品放入 + + //-- 判断组合购的总数量是不是到了 -- + if(act.zh_num>0){ + //如果是在全选的情况下,那么就需要in_zhact_new来控制数量 + var need_ck=0; + if(is_need_ck_num){ + need_ck=th.data.in_zhact_new[aid]?th.data.in_zhact_new[aid]:0; + } + if(need_ck+act.zh_buy_num>=act.zh_num ){ + return false; + }else{ + min_can_buy= act.zh_num-act.zh_buy_num-need_ck; + } + } + + //-- 判断组合购的限购是不是到了 -- + if(act.buy_limit>0){ + var need_ck0=0; + if(is_need_ck_num){ + need_ck0=th.data.in_zhact_new[aid]?th.data.in_zhact_new[aid]:0; + } + var min_can_buy0=0; + if(need_ck+item_act_map.buyed>=act.buy_limit ){ + return false; + }else{ + min_can_buy0=act.buy_limit-item_act_map.buyed-need_ck; + } + if(min_can_buy>0){ + if(min_can_buy0 0) { var cbuy = item.buyqty - zh_b_num; var key = item.goods_id + "." + act.id; - if (th.data.in_zhact_gdmap[key]) { - var cbuy = item.buyqty - zh_b_num - th.data.in_zhact_gdmap[key]; + if (is_need_ck_num && th.data.in_zhact_gdmap_new[key]) { + var cbuy = item.buyqty - zh_b_num - th.data.in_zhact_gdmap_new[key]; } //当可买的数量为0 if (cbuy <= 0) { item.num = 0; + out_arr.push({ + goods_id: item.goods_id, + num: cart_num + }) } else { if (cbuy >= cart_num) item.num = cart_num; else { item.num = cbuy; + out_arr.push({ + goods_id: item.goods_id, + num: cart_num - cbuy + }) } } } else { @@ -572,9 +622,24 @@ module.exports = { all_num += item.num; //当有起购数的控制的时候 if (item.zhqty) { + all_zhqty += item.zhqty; if (item.num < item.zhqty) { need_to_buy += item.zhqty - item.num; } + if (item.num > item.zhqty) { + for (let i = 0; i < item.num - item.zhqty; i++) { + no_in_arr.push({ + goods_id: item.goods_id, + }) + } + } + } + else { + for (let j = 0; j < item.num; j++) { + no_in_arr.push({ + goods_id: item.goods_id, + }) + } } } else { if (item.zhqty) { @@ -585,19 +650,141 @@ module.exports = { //当满足组合的要求:总数要满足,起购数要满足 if (all_num >= act.zhbuyqty && !need_to_buy) { + //默认数量是1 + var act_num=1; + function sortData(a, b) { + return a.price - b.price + } + if (no_in_arr.length > 0) no_in_arr.sort(sortData); + if (act.zhbuyqty > all_zhqty) { + for (let n = 0; n < act.zhbuyqty - all_zhqty; n++) { + no_in_arr.pop(); + } + } + + //看一下剩下的数量有没有满足组合购的要求,以及要不要倍增 + if (act.is_bz && no_in_arr.length >= act.zhbuyqty) { + + var bz_num_ok = 0; //超量倍增是否满足 + var zhqty_len = 0; //几个超量倍增 + var delete_num = 0; + //看一下是几倍 + let be = parseInt(no_in_arr.length / act.zhbuyqty); + //如果有总数控制的时候 + if (min_can_buy>0) { + var be1 = min_can_buy - 1; + if (be1 < be) be = be1; + } + if(be) { + if (act.is_bzyh && zhqty_bz.length > 0) { + + let zhqty_bz_arr = []; + let zhqty_bz_flag = this.zhqty_bz_fun(zhqty_bz, be, zhqty_bz_arr); + if (zhqty_bz_flag) { + for (let i = 0; i < zhqty_bz.length; i++) { + var vv = zhqty_bz[i]; + for (let j = 0; j < be * vv['zhqty']; j++) { + let index = no_in_arr.findIndex(i => { + return vv.goods_id === i.goods_id + }) + if (index > -1) { + delete_num++ + no_in_arr.splice(index, 1) + } + } + } + } else { + zhqty_len = 1; + let min_bz_num = Math.min.apply(Math, zhqty_bz_arr.map(function (o) { + return o['be']; + })); + let new_arr = zhqty_bz_arr.filter(ii => { + return ii['be'] == min_bz_num; + }) + //-- 要看一下倍数还有没有 --- + be = new_arr[0].be; + //如果有总数控制的时候 + if (min_can_buy>0) { + var be1 = min_can_buy - 1; + if (be1 < be) be = be1; + } + if(be){ + for (let i = 0; i < zhqty_bz.length; i++) { + let item1 = zhqty_bz[i]; + for (let j = 0; j < be * item1['zhqty']; j++) { + let index = no_in_arr.findIndex(i => { + return item1.goods_id === i.goods_id + }) + if (index > -1) { + no_in_arr.splice(index, 1) + } + } + } + act_num+=be; + } + } + } + if (!zhqty_len) { + let pop_num = be * act.zhbuyqty - delete_num; + for (var m = 0; m < pop_num; m++) { + no_in_arr.pop(); + } + act_num+=be; + } + } + } + + //-- 参与活动的金额要返回回去 -- + item_act_map.act_num=act_num; + let goods_map = {}; + //算一下剩余的商品 + if (no_in_arr.length) { + for (let ii in no_in_arr) { + let item = no_in_arr[ii]; + if (goods_map[item.goods_id]) { + var num = goods_map[item.goods_id]; + goods_map[item.goods_id] = ++num; + } else { + goods_map[item.goods_id] = 1; + } + } + } + //超量的部分也要计算一下 + if (out_arr.length) { + for (var m in out_arr) { + if (goods_map[item.goods_id]) { + var num = goods_map[item.goods_id]; + num += item.num + goods_map[item.goods_id] = num; + } else { + goods_map[item.goods_id] = item.num; + } + } + } + + //-- 组合购的商品限购的数组控制 -- + var in_zhact_gdmap={}; //商品的列表 for (var u in goods) { var ite = goods[u]; - if (ite.buyqty > 0 && ite.num > 0) { - console.log(222, th.data.in_zhact_gdmap_gp); + var cart_num = get_num(ite).goods_num; + + if (ite.buyqty > 0 && cart_num > 0) { var key = ite.goods_id + "." + act.id; - if (th.data.in_zhact_gdmap[key]) { - th.data.in_zhact_gdmap[key] += ite.num; - } else { - th.data.in_zhact_gdmap[key] = ite.num; + if(goods_map[ite.goods_id]){ + cart_num-=goods_map[ite.goods_id]; + } + if(cart_num>0){ + if (in_zhact_gdmap[key]) { + in_zhact_gdmap[key] += ite.num; + } else { + in_zhact_gdmap[key] = ite.num; + } } + } } + item_act_map.in_zhact_gdmap=in_zhact_gdmap; return true; } return false; @@ -605,5 +792,4 @@ module.exports = { - } \ No newline at end of file