var shoppingCart = new Vue({ el: "#shoppingCart", data: { userId: 0, userIdentity: '', userToken: '', listLoading: true, listData: [], invalidData: [], promotionsList: [], totalCouponList:[], // 优惠券算列表 eligibleCoupons:[], // 最终需要用到优惠券 kindCount: 0, totalCount: 0, totalPrice: 0, reducedPrice: 0, originalPrice: 0, allChecked: true, submitIds: [],//去结算商品Ids isShowPopup:false, currentTab:1, couponParam:{// 获取弹窗优惠券领取参数 userId:0, shopId:0, status:1, source:1 }, isCouponEmpty:false, productCoupon:[], // 优惠券 productCouponList:[], // 优惠券弹窗列表 couponPrice:0, // 优惠金额 totalDiscountAmount:0, // 总共减去金额 isDiscount:false, // 控制显示优惠明细 }, watch:{ listData: { handler: function() { console.log('obj.a changed'); }, immediate: true, deep: true } }, filters: { NumFormat: function (text) {//处理金额 return Number(text).toFixed(2); }, TypeFormat:function(value) { switch (value) { case 0: return '活动券'; break; case 1: return '品类券'; break; case 2: return '用户专享券'; break; case 3: return '店铺券'; break; case 4: return '新用户券'; break; } } }, methods: { getCartLists: function () { var _self = this; if(this.userId ===0){return;} ShoppingApi.QueryShoppingCartList({userId: this.userId},function(response){ if(response.code === 0){ var data = response.data; _self.listData = data.list; _self.invalidData = data.invalid; _self.kindCount = data.kindCount; _self.totalCount = data.totalCount; _self.totalPrice = data.totalPrice; _self.promotionsList = data.promotions; _self.totalCouponList = data.couponList; // 默认全选 _self.listData.forEach(function(supplier){ supplier.checked = true; if(supplier.cartList.length>0){ supplier.cartList.forEach(function(cartItem){ _self.submitIds.push(cartItem.productId); }) } }); _self.allChecked = true; _self.computedPrice(); _self.listLoading = false; }else{ CAIMEI.Alert(response.msg,'确定',true, function(){}); } }) }, toggleThisLadder: function(event){ var el = event.currentTarget; if($(el).hasClass("on")){ $(el).removeClass("on").siblings('.mFixed').hide(); if(!isPC){looseBody();} }else{ $(el).addClass("on").siblings('.mFixed').show(); if(!isPC){fixedBody();} } }, hideThisLadder: function(event){ var el = event.currentTarget; $(el).parents('.priceTag').find('.tag').removeClass("on").siblings('.mFixed').hide(); if(!isPC){looseBody();} }, ckeckItem: function(event){ var el = event.currentTarget; if(!$(el).prop("checked")){ $(el).parents('.supplierItem').find('.supplierTit').find('input.check').prop("checked", false); } this.computedPrice(); }, ckeckSupplier: function(event, supplierChecked){ var el = event.currentTarget; var _self = this; var cartList = $(el).parents('.supplierItem').find('input[name=cartList]'); this.$nextTick(function(){ if (supplierChecked) { $.each(cartList, function (index, item) { if (!_self.submitIds.includes(item.value*1)){ _self.submitIds.push(item.value*1); } }); }else{ $.each(cartList, function (index, item) { var loc = _self.submitIds.indexOf(item.value*1); if(loc >=0 ){ _self.submitIds.splice(loc, 1); _self.allChecked = false; } }); } _self.computedPrice(); }); }, ckeckAll: function(){ var _self = this; this.$nextTick(function(){ if (_self.allChecked) { _self.submitIds = []; _self.listData.forEach(function(supplier){ supplier.checked = true; if(supplier.cartList.length>0){ supplier.cartList.forEach(function(cartItem){ _self.submitIds.push(cartItem.productId); }) } }); }else{ _self.submitIds = []; _self.listData.forEach(function(supplier){ supplier.checked = false; }); } _self.computedPrice(); }); }, setPromotions:function(){ var _self = this; this.promotionsList.forEach(function(promotions){ promotions.productList.forEach(function(product){ _self.listData.map(function(supplier){ supplier.cartList.map(function(cart){ if(product.productId === cart.productId){ product.number = cart.number } }); }); }); }); }, computedPrice: function(){ var _self = this; this.$nextTick(function(){ var totalPrice = 0; var reducedPrice = 0; var originalPrice = 0; var kindCount = 0; var totalCount = 0; _self.listData.forEach(function(supplier){ var supplierPrice = 0; var supplierReducedPrice = 0; if(supplier.cartList.length>0){ supplier.cartList.forEach(function(cart){ if (_self.submitIds.includes(cart.productId*1)){ supplierPrice += cart.price*cart.number; kindCount += 1; totalCount += cart.number; // 单品满减 if(cart.promotions && cart.promotions.type*1===1 && cart.promotions.mode*1===2){ // 单品满减-重新计算供应商总价/满减金额 if(cart.price*cart.number >= cart.promotions.touchPrice){ supplierPrice -= cart.promotions.reducedPrice; supplierReducedPrice += cart.promotions.reducedPrice; } } } }); // 店铺满减 if(supplier.promotions && supplier.promotions.mode*1===2){ // 店铺满减-计算供应商总价/满减金额 if(supplierPrice >= supplier.promotions.touchPrice){ supplierPrice -= supplier.promotions.reducedPrice; supplierReducedPrice += supplier.promotions.reducedPrice; } } } supplier.totalPrice = supplierPrice; supplier.reducedPrice = supplierReducedPrice; supplier.originalPrice = (supplierPrice+supplierReducedPrice); totalPrice += supplier.totalPrice; reducedPrice += supplier.reducedPrice; originalPrice += supplier.originalPrice; }); // 总促销计算 _self.promotionsList.forEach(function(promotions){ // 凑单满减 if(promotions.mode*1===2 && promotions.type*1===2){ var total = 0; promotions.productList.forEach(function(product){ if (_self.submitIds.includes(product.productId*1)){ total += product.number * product.price; } }); if(total>=promotions.touchPrice){ totalPrice -= promotions.reducedPrice; reducedPrice += promotions.reducedPrice; } } }); _self.totalPrice = totalPrice; _self.reducedPrice = reducedPrice; _self.originalPrice = originalPrice; _self.kindCount = kindCount; _self.totalCount = totalCount; // 计算优惠券 if(_self.totalCouponList.length>0){ _self.calculationCoupon(); } // 最后满减金额 = 店铺减去金额 + 单品减去金额 + 凑单减去金额 _self.totalDiscountAmount = _self.reducedPrice + _self.couponPrice; console.log('totalDiscountAmount',_self.totalDiscountAmount); // 控制显示优惠明细 if(_self.totalDiscountAmount > 0 ){ _self.isDiscount = true; }else{ _self.isDiscount = false; } }); }, calculationCoupon:function(){// 优惠券计算 var _self = this; var eligibleCoupons = []; _self.listData.forEach(function(shop,index){ shop.cartList.forEach(function(pros){ if(_self.submitIds.includes(pros.productId*1)){ _self.totalCouponList.forEach(function (coupon,index){//循环优惠券 switch(coupon.couponType){ case 0:// 活动券 if(coupon.productType == 1){// 活动券-全商城商品 if(_self.totalPrice >= coupon.touchPrice){ eligibleCoupons = _self.setCouponsArr(coupon) } }else if(coupon.productType == 2){// 活动券-指定商品 if(coupon.productIds && coupon.productIds.includes(pros.productId)){ if(_self.totalPrice >= coupon.touchPrice){ eligibleCoupons = _self.setCouponsArr(coupon) } } } break; case 1:// 品类券适用于产品或者仪器 categoryType(commodityType) : 1 产品 2 仪器 if( coupon.categoryType && coupon.categoryType == pros.commodityType){ if(_self.totalPrice >= coupon.touchPrice){ eligibleCoupons = _self.setCouponsArr(coupon) } } break; case 2:// 用户专享券 适用于商城全部商品 if(_self.totalPrice >= coupon.touchPrice){ eligibleCoupons = _self.setCouponsArr(coupon) } break; case 3:// 店铺券适用于某个供应商的全部商品 if(coupon.shopId && pros.supplierId == coupon.shopId){ if(_self.totalPrice >= coupon.touchPrice){ eligibleCoupons = _self.setCouponsArr(coupon) } } break; case 4://新用户券 适用于商城全部商品 if(_self.totalPrice >= coupon.touchPrice){ eligibleCoupons = _self.setCouponsArr(coupon) } break; } }) }else{ return; } }) }) if(eligibleCoupons.length>0){ _self.eligibleCoupons.splice(0,_self.eligibleCoupons.length); _self.eligibleCoupons.push(eligibleCoupons[0]) _self.couponPrice = _self.eligibleCoupons.sort((a,b)=> b.couponAmount - a.couponAmount)[0].couponAmount }else{ _self.couponPrice = 0 } }, setCouponsArr: function(coupon){//处理 var list = []; list.push(coupon) list.sort((a,b)=> b.couponAmount - a.couponAmount ) return list }, cartNumberSub: function(cart){ cart.number -= cart.step; if (cart.numbercart.stock){ cart.number = cart.stock; } this.numberChange(cart); }, cartNumberChange: function(cart){ cart.number = Math.ceil(cart.number/cart.step)*cart.step; this.numberChange(cart); }, numberChange: function(cart){ if(cart.ladderFlag){ cart.ladderPrices.forEach(function(ladder){ if(cart.number>=ladder.buyNum){ cart.price = ladder.buyPrice; cart.originalPrice = ladder.buyPrice; } }); } // 设置优惠数量 this.setPromotions(); // 计算价格 this.computedPrice(); // 更新购物车 var _self = this; this.$nextTick(function(){ _self.updateCart(cart); }); }, updateCart: function(cart){ var _self = this; ShoppingApi.ShoppingCartUpdate({userID:_self.userId,productID: cart.productId,productCount: cart.number},function(response){ if(response.code === 0){ console.log('更新购物车商品数量成功'); }else{ CAIMEI.Alert(response.msg,'确定',true, function(){}); } }) }, deleteCart: function(productIds){ var _self = this; ShoppingApi.DeleteCartProducts({userID:_self.userId,productIDs:productIds},function(response){ if(response.code === 0){ CAIMEI.dialog('删除成功',true,function () { _self.getCartLists(); }) }else{ CAIMEI.Alert(response.msg,'确定',true, function(){}); } }) }, deletefailureList:function(){ var _self = this; var delGoodsList=''; _self.invalidData.forEach(function(item){ delGoodsList+= item.productId+',' }); CAIMEI.Modal('确定清空全部失效商品吗?','取消','确定',function(){ _self.deleteCart(delGoodsList) }); }, deleteCartOne:function(productIds){ var _self = this; CAIMEI.Modal('确定删除该商品吗?','取消','确定',function(){ _self.deleteCart(productIds); }); }, deleteSelected: function(){ var _self = this; var productIds = _self.submitIds.join(","); if(productIds == ''){ CAIMEI.dialog('请选择要删除的商品~'); return false; } CAIMEI.Modal('确定删除选中的商品吗?','取消','确定',function(){ _self.deleteCart(productIds); }); }, submitCart: function(){ if (this.submitIds.length>0){ var productIds = this.submitIds.join(","); window.localStorage.setItem("shoppingProductIds", productIds); // type:(1购物车提交[对应表cm_cart],2直接购买提交, 3协销下单) window.location.href = '/shopping/confirm.html?type=1'; } }, queryPopupCoupons:function(){// 获取弹窗优惠券列表 var _self = this; ShoppingApi.ShoppingCartGetCoupon(_self.couponParam, function (response) { if(response.code == 0){ var data = response.data; if(data.couponList && data.couponList.length >0){ _self.productCouponList = data.couponList; _self.isCouponEmpty = false; console.log('有优惠券') console.log(_self.productCouponList) }else{ console.log('没有优惠券') _self.isCouponEmpty = true; } }else{ console.log('获取优惠券列表失败') } }) }, receiveCoupon:function(coupon){// 点击领取优惠券 var _self = this; ProductApi.ReceiveCoupon({userId:_self.couponParam.userId, couponId:coupon.couponId, source:1}, function (response) { if(response.code == 0){ CAIMEI.dialog('领取成功',true,function () { _self.currentTab = 2; _self.couponParam.status = 2; _self.queryPopupCoupons(); }); }else{ CAIMEI.Alert(response.msg, '确定', false); } }) }, queryCouponTabs:function (value){ var _self = this; _self.currentTab = _self.couponParam.status = value; _self.queryPopupCoupons(); }, showPopup:function(shop){// 显示优惠券弹窗 var _self = this; _self.productCouponList = []; _self.currentTab = 1; _self.couponParam.status = 1; _self.couponParam.shopId = shop.id _self.queryPopupCoupons(); _self.isShowPopup = true; }, hidePopup:function(){// 隐藏优惠券弹窗 var _self = this; _self.isShowPopup = false; }, }, created: function () { if(globalUserData){ this.userId = this.couponParam.userId = globalUserData.userId; this.userIdentity = globalUserData.identity; this.userToken = globalUserData.token; } // 获取列表数据 this.getCartLists(); }, mounted: function () { var _self = this; var minAwayBtm = $("footer").height(); //处理滚动条控制底部提交fixed $(window).scroll(function() { var awayBtm = $(document).height() - $(window).scrollTop() - $(window).height(); if(minAwayBtm <= awayBtm){ $(".summaryWrap").addClass('fixed'); }else{ $(".summaryWrap").removeClass("fixed"); } }); } });