Przeglądaj źródła

购物车优化、凑单促销叠加计算

zhengjinyi 3 lat temu
rodzic
commit
3fa4c17e77

+ 11 - 6
src/main/resources/static/css/shopping/shopping.css

@@ -58,7 +58,8 @@ li{list-style:none}
     .listWrap .c10{float:left;width:495px}
     .listWrap .c11{float:left;width:480px;text-align:left}
     .listWrap .c1 span.tip{position:absolute;background:#ff4500;line-height:20px;height:20px;padding:0 10px;white-space:nowrap;font-size:12px;color:#FFF;top:0;left:0}
-    .listWrap .check{width:16px;height:16px;line-height:14px;vertical-align:middle}
+    .listWrap .coll-checked{width: 16px;height: 16px;cursor: pointer;background: url("/img/cart/icon_weigouxuan@2x.png") no-repeat;background-size: cover;display: block;margin-right: 6px;margin-top: 24px;}
+    .listWrap .coll-checked.checked{background: url("/img/cart/icon_yixuanze@2x.png") no-repeat;background-size: cover;}
     .listWrap .image{display:block;width:100px;height:100px;border:1px solid #F7F7F7}
     .listWrap .name,.listWrap .unit{line-height:20px;display:inline-block;max-height:100px;overflow:hidden;vertical-align:middle}
     .listWrap .cartItem .name{padding-left:14px}
@@ -103,7 +104,8 @@ li{list-style:none}
     .summaryWrap .summary .item.priceTotal{float: right;margin-right: 0;}
     .summaryWrap .summary .item p.priceTotal-t{height: 32px;font-size: 16px;display: inline-block;width: 100%;float: left;text-align: left;line-height: 30px;}
     .summaryWrap .summary .item p.priceTotal-b{height: 18px;font-size: 14px;display: inline-block;width: 100%;float: left;text-align: left;line-height: 18px;}
-    .summaryWrap .summary .check{width:14px;height:14px;line-height:14px;vertical-align:top;margin:18px 16px 0 16px}
+    .summaryWrap .summary .coll-checked{width: 16px;height: 16px;float: left;cursor: pointer;background: url("/img/cart/icon_weigouxuan@2x.png") no-repeat;background-size: cover;display: block;margin-right: 6px;vertical-align:top;margin:18px 16px 0 16px;}
+    .summaryWrap .summary .coll-checked.checked{background: url("/img/cart/icon_yixuanze@2x.png") no-repeat;background-size: cover;}
     .summaryWrap .summary .delete{margin-left:16px;color:#f55c5c}
     .summaryWrap .summary .totalMoney{display:inline-block;margin-right:32px;position:relative;}
     .summaryWrap .summary .totalPrice{line-height: 24px;font-size: 18px;}
@@ -279,6 +281,7 @@ li{list-style:none}
     .listWrap .supplierItem.disable{background:#f1f0f5}
     .listWrap .supplierItem em{font-style:normal}
     .listWrap .supplierTit{border-bottom:1px solid #F7F7F7;height:7.4vw;line-height:7.4vw;padding:1.3vw 3.3vw;position:relative;}
+    .listWrap .supplierTit .coll-checked{margin-top: 2vw;}
     .listWrap .supplierTit .price{height:6.4vw;float: left;}
     .listWrap .supplierTit .shop{display: block;float: left;}
     .listWrap .supplierTit .couponTag{float:left;margin-left: 10px;height: 6.4vw;line-height: 6.4vw;box-sizing: border-box;border-radius: 0.2vw;}
@@ -299,8 +302,9 @@ li{list-style:none}
     .listWrap .supplierBtn .note{text-align:left;white-space:nowrap;overflow:hidden}
     .listWrap .supplierBtn .note input{width:79vw;height:8vw;line-height:8vw;background:#F7F7F7;border-radius:1vw;border:none;padding:0 2vw;font-size:3.1vw}
     .listWrap .supplierBtn .item{display:inline-block;padding-top:2vw;line-height:7vw;}
-    .listWrap .supplierItem .c0{float:left;width:9.6vw;text-align:center}
-    .listWrap .check{width:3.5vw;height:3.5vw;line-height:3.5vw;vertical-align:middle}
+    .listWrap .supplierItem .c0{float:left;width:9.6vw;text-align:center;    height: 100%;display: flex;align-items: center;}
+    .listWrap .coll-checked{width: 16px;height: 16px;cursor: pointer;background: url("/img/cart/icon_weigouxuan@2x.png") no-repeat;background-size: cover;display: block;margin-right: 6px;margin-top: 0;}
+    .listWrap .coll-checked.checked{background: url("/img/cart/icon_yixuanze@2x.png") no-repeat;background-size: cover;}
     .listWrap .supplierItem .c1{float:left;width:24vw;position:relative;}
     .listWrap .c1 span.tip{position:absolute;background:#ff4500;line-height:5vw;height:5vw;padding:0 2vw;white-space:nowrap;font-size:3.1vw;color:#FFF;top:0;left:0}
     .listWrap .supplierItem .image{display:block;box-sizing:border-box;width:24vw;height:24vw;border:1px solid #F7F7F7}
@@ -346,11 +350,12 @@ li{list-style:none}
     .summaryWrap .summary .left{height:9.4vw;line-height:9.4vw}
     .summaryWrap .summary .right{height:11.2vw;line-height:11.2vw}
     .summaryWrap .summary .right .item{float: left;}
-    .summaryWrap .summary .item.priceTotal{float: left;margin-left: 11vw;}
+    .summaryWrap .summary .item.priceTotal{float: left;margin-left:0;}
     .summaryWrap .summary .item p.priceTotal-t{height:11.2vw;line-height:11.2vw;font-size: 3.4vw;display: inline-block;width: 100%;float: left;text-align: left;}
     .summaryWrap .summary .item.priceTotal p.priceTotal-t{height: 6.6vw;line-height: 6.6vw;}
     .summaryWrap .summary .item p.priceTotal-b{height: 4.6vw;font-size: 3vw;display: inline-block;width: 100%;float: left;text-align: left;line-height: 4.6vw;}
-    .summaryWrap .summary .check{width:3.5vw;height:3.5vw;line-height:3.5vw;vertical-align:top;margin:3vw 3vw 0 3.3vw}
+    .summaryWrap .summary .coll-checked{width: 16px;height: 16px;float: left;cursor: pointer;background: url("/img/cart/icon_weigouxuan@2x.png") no-repeat;background-size: cover;display: block;margin-right: 6px;vertical-align:top;margin:11px 16px 0 0;}
+    .summaryWrap .summary .coll-checked.checked{background: url("/img/cart/icon_yixuanze@2x.png") no-repeat;background-size: cover;}
     .summaryWrap .summary .delete{width:12vw;height:6vw;line-height:6vw;display:inline-block;text-align:center;color:#f55c5c}
     .summaryWrap .summary .totalPrice{line-height: 5.1vw;font-size: 3.4vw;}
     .summaryWrap .summary .totalPrice.none{line-height: 11.2vw;}

+ 135 - 94
src/main/resources/static/js/shopping/cart.js

@@ -17,6 +17,7 @@ var shoppingCart = new Vue({
         originalPrice: 0,
         allChecked: true,
         submitIds: [],//去结算商品Ids
+        checkenProsList:[],// 勾选中的商品
         isShowPopup:false,
         currentTab:1,
         couponParam:{// 获取弹窗优惠券领取参数
@@ -31,6 +32,7 @@ var shoppingCart = new Vue({
         couponPrice:0,							// 优惠金额
         totalDiscountAmount:0,					// 总共减去金额
         isDiscount:false,						// 控制显示优惠明细
+        isCheckedProductStatus:false
     },
     watch:{
         listData: {
@@ -84,8 +86,11 @@ var shoppingCart = new Vue({
                     _self.listData.forEach(function(supplier){
                         supplier.isChecked = true;
                         if(supplier.cartList.length>0){
-                            supplier.cartList.forEach(function(cartItem){
-                                _self.submitIds.push(cartItem.productId);
+                            supplier.cartList.forEach(function(pros){
+                                pros.isChecked = true
+                                _self.submitIds.push(pros.productId);
+                                _self.checkenProsList.push(pros);
+                                _self.isCheckedProductStatus = true;
                             })
                         }
                     });
@@ -112,57 +117,86 @@ var shoppingCart = new Vue({
             $(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);
+        ckeckItem: function(supplier,pro){//为未选中的时候改变为true,反之为true
+            var _self = this;
+            pro.isChecked = !pro.isChecked;
+            if (pro.isChecked) {
+                if (!_self.submitIds.includes(pro.productId * 1)) {
+                    _self.submitIds.push(pro.productId);
+                    _self.checkenProsList.push(pro);
+                }
+                _self.isCheckedProductStatus = true;
+            } else {
+                var lent = _self.submitIds.indexOf(pro.productId * 1)
+                if (lent >= 0) {
+                    _self.submitIds.splice(lent, 1);
+                    _self.checkenProsList.splice(lent, 1);
+                }
             }
-            this.computedPrice();
+            _self.updateProductCheckedAllBtn(supplier)
+            _self.updateCheckAllBtn()
+            _self.computedPrice();
         },
-        ckeckSupplier: function(event, supplierChecked){
-            var el = event.currentTarget;
+        ckeckSupplier: function(supplier){
             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;
-                        }
-                    });
+            supplier.isChecked = !supplier.isChecked;
+            _self.setProductChecked(supplier);
+            _self.updateCheckAllBtn();
+            _self.computedPrice();
+        },
+        updateProductCheckedAllBtn: function(supplier) { // 单独每个供应商的勾选判断
+            var productsList = supplier.cartList,
+                productsCheckedLength = 0;
+            productsList.forEach(pros => {
+                if (pros.isChecked) {
+                    productsCheckedLength++;
                 }
-                _self.computedPrice();
-            });
+            })
+            supplier.isChecked = productsCheckedLength === productsList.length
         },
-        ckeckAll: function(){
+        updateCheckAllBtn: function() { // 全选勾选判断
             var _self = this;
-            this.$nextTick(function(){
-                if (_self.allChecked) {
-                    _self.submitIds = [];
-                    _self.listData.forEach(function(supplier){
-                        supplier.isChecked = 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.isChecked = false;
-                    });
+            var goodsCheckedLength = 0,
+                goodsList = _self.listData;
+                goodsList.forEach(function(supplier){
+                    if (supplier.isChecked) {
+                        goodsCheckedLength++;
+                    }
+                })
+            _self.allChecked = goodsCheckedLength === goodsList.length;
+        },
+        updateBothCheckBtn: function(){// 更新商铺勾选状态
+            var _self = this;
+            _self.listData.forEach(supplier => {
+                supplier.isChecked = _self.allChecked;
+                _self.setProductChecked(supplier);
+            })
+        },
+        setProductChecked: function(supplier) {//
+            var _self = this;
+            supplier.cartList.forEach(pros => {
+                if (supplier.isChecked) {
+                    pros.isChecked = true;
+                    if (!_self.submitIds.includes(pros.productId * 1)) {
+                        _self.submitIds.push(pros.productId);
+                        _self.checkenProsList.push(pros);
+                    }
+                    _self.isCheckedProductStatus = true;
+                } else {
+                    pros.isChecked = false;
+                    var lent = this.submitIds.indexOf(pros.productId * 1);
+                    if (lent >= 0) {
+                        _self.submitIds.splice(lent, 1);
+                        _self.checkenProsList.splice(lent, 1);
+                    }
                 }
-                _self.computedPrice();
-            });
+            })
+            _self.computedPrice();
+        },
+        ckeckAll: function(){
+            var _self = this;
+            _self.allChecked = !_self.allChecked;
+            _self.updateBothCheckBtn()
         },
         setPromotions:function(){
             var _self = this;
@@ -248,59 +282,69 @@ var shoppingCart = new Vue({
                     reducedPrice += supplier.reducedPrice;
                     originalPrice += supplier.originalPrice;
                 });
-                // 总促销计算
-                _self.promotionsList.forEach(function (promotions) {
-                    // 凑单满减
-                    if (promotions.mode * 1 === 2 && promotions.type * 1 === 2) {
-                        var collecTotal = 0;
-                        var collecDiscountNum = 0;
-                        promotions.productList.forEach(function (product) {
-                            if (_self.submitIds.includes(product.productId * 1)) {
-                                total += product.number * product.price;
-                            }
-                        });
-                        if(promotions.discount == 1){// 支持凑单满减促销叠加条件的执行
-                            collecDiscountNum += parseInt(collecTotal/promotions.touchPrice) // 凑单满减促销叠加次数 = 凑单商品合计价格/凑单满减促销价格
-                            console.log('凑单促销满减叠加通道叠加次数',`${collecDiscountNum}次`)
-                            if (collecTotal >= promotions.touchPrice) {
-                                totalPrice -= promotions.reducedPrice*collecDiscountNum
-                                reducedPrice += promotions.reducedPrice*collecDiscountNum
-                            }
-                        }else{
-                            console.log('凑单促销满减非叠加通道')
-                            if (collecTotal >= promotions.touchPrice) {
-                                totalPrice -= promotions.reducedPrice
-                                reducedPrice += promotions.reducedPrice
+                if(_self.isCheckedProductStatus){
+                    // 总促销计算
+                    _self.promotionsList.forEach(function (promotions) {
+                        // 凑单满减
+                        if (promotions.mode * 1 === 2 && promotions.type * 1 === 2) {
+                            var collecTotal = 0;
+                            var collecDiscountNum = 0;
+                            promotions.productList.forEach(function (product) {
+                                collecTotal += _self.collecTotalPrice(product);
+                            });
+                            if(promotions.discount == 1){// 支持凑单满减促销叠加条件的执行
+                                collecDiscountNum += parseInt(collecTotal/promotions.touchPrice) // 凑单满减促销叠加次数 = 凑单商品合计价格/凑单满减促销价格
+                                console.log('凑单促销满减叠加通道叠加次数',`${collecDiscountNum}次`)
+                                if (collecTotal >= promotions.touchPrice) {
+                                    totalPrice -= promotions.reducedPrice*collecDiscountNum
+                                    reducedPrice += promotions.reducedPrice*collecDiscountNum
+                                }
+                            }else{
+                                console.log('凑单促销满减非叠加通道')
+                                if (collecTotal >= 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) {
+                        let eligibleCoupons = _self.calculationCoupon();
+                        if (eligibleCoupons.length > 0) {
+                            _self.eligibleCoupons.splice(0, _self.eligibleCoupons.length);
+                            _self.eligibleCoupons = eligibleCoupons.sort((a, b) => b.couponAmount - a.couponAmount)
+                            _self.couponPrice = this.eligibleCoupons[0].couponAmount
+                        } else {
+                            _self.couponPrice = 0
+                        }
                     }
-                });
-                _self.totalPrice = totalPrice;
-                _self.reducedPrice = reducedPrice;
-                _self.originalPrice = originalPrice;
-                _self.kindCount = kindCount;
-                _self.totalCount = totalCount;
-                // 计算优惠券
-                if (_self.totalCouponList.length > 0) {
-                    let eligibleCoupons = _self.calculationCoupon();
-                    if (eligibleCoupons.length > 0) {
-                        _self.eligibleCoupons.splice(0, _self.eligibleCoupons.length);
-                        _self.eligibleCoupons = eligibleCoupons.sort((a, b) => b.couponAmount - a.couponAmount)
-                        _self.couponPrice = this.eligibleCoupons[0].couponAmount
+                    // 最后满减金额 = 店铺减去金额 + 单品减去金额 + 凑单减去金额
+                    _self.totalDiscountAmount = _self.reducedPrice + _self.couponPrice;
+                    console.log('最终优惠金额', _self.totalDiscountAmount);
+                    // 控制显示优惠明细
+                    if (_self.totalDiscountAmount > 0) {
+                        _self.isDiscount = true;
                     } else {
-                        _self.couponPrice = 0
+                        _self.isDiscount = false;
                     }
                 }
-                // 最后满减金额 = 店铺减去金额 + 单品减去金额 + 凑单减去金额
-                _self.totalDiscountAmount = _self.reducedPrice + _self.couponPrice;
-                console.log('最终优惠金额', _self.totalDiscountAmount);
-                // 控制显示优惠明细
-                if (_self.totalDiscountAmount > 0) {
-                    _self.isDiscount = true;
-                } else {
-                    _self.isDiscount = false;
+            })
+        },
+        collecTotalPrice:function(pros){// 凑单满减计算勾选的凑单商品总价
+            var _self = this;
+            var price;
+            _self.checkenProsList.forEach(el => {
+                if(pros.productId == el.productId ){
+                    price = el.number * el.price
                 }
             })
+            return price
         },
         calculationCoupon:function(){// 优惠券计算
             var _self = this;
@@ -466,10 +510,7 @@ var shoppingCart = new Vue({
                     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{

+ 6 - 6
src/main/resources/templates/shopping/cart.html

@@ -36,7 +36,7 @@
         </div>
         <div class="listWrap" v-cloak>
             <div v-if="isPC" class="cartTit clear">
-                <div class="c0"><input class="check" type="checkbox" @change="ckeckAll()" v-model="allChecked"></div>
+                <div class="c0"><span class="coll-checked" :class="[allChecked ? 'checked': '']" @click="ckeckAll"></span></div>
                 <div class="c1">全选</div>
                 <div class="c2">商品信息</div>
                 <div class="c4">单价(元)</div>
@@ -46,8 +46,8 @@
             </div>
             <div class="supplierItem" v-for="supplier,index in listData">
                 <div class="supplierTit">
-                    <div class="c0">
-                        <input class="check" type="checkbox" @change="ckeckSupplier($event, supplier.isChecked)" v-model="supplier.isChecked">
+                    <div class="c0" @click="ckeckSupplier(supplier)">
+                        <span class="coll-checked" :class="[supplier.isChecked ? 'checked': '']"></span>
                     </div>
                     <a  class="shop" :href="'/supplier-'+supplier.shopId+'.html'" target="_blank">
                         <img class="img" :src="supplier.shopLogo">
@@ -82,8 +82,8 @@
                     </div>
                 </div>
                 <div class="cartItem clear"  v-for="cart in supplier.cartList" >
-                    <div class="c0">
-                        <input class="check" type="checkbox" name="cartList" :value="cart.productId" v-model="submitIds" @change="ckeckItem($event)">
+                    <div class="c0" @click="ckeckItem(supplier,cart)">
+                        <span class="coll-checked" :class="[cart.isChecked ? 'checked': '']"></span>
                     </div>
                     <div class="c1"><a :href="'/product-'+cart.productId+'.html'" target="_blank">
                         <img class="image" :src="cart.image">
@@ -206,7 +206,7 @@
         <div class="summary">
             <div class="left mfbt">
                 <div class="item">
-                    <input class="check" type="checkbox" @change="ckeckAll()" v-model="allChecked">
+                    <span class="coll-checked" :class="[allChecked ? 'checked': '']" @click="ckeckAll"></span>
                     <span>全选</span>
                     <a class="delete" href="javascript:void(0);" @click="deleteSelected()">删除</a>
                 </div>