瀏覽代碼

购物车促销价格计算

chao 4 年之前
父節點
當前提交
9885f931b9

+ 8 - 5
src/main/resources/static/css/shopping/cart.css

@@ -74,14 +74,17 @@ li{list-style:none}
     .summaryWrap .summary em{font-style:normal}
     .summaryWrap .summary .left,.summaryWrap .summary .right{display:inline;}
     .summaryWrap .summary .left .item:first-child{float:left;}
-    .summaryWrap .summary .item{display:inline-block;margin-right:32px;}
+    .summaryWrap .summary .item{display:inline-block;margin-right:32px;position:relative;}
     .summaryWrap .summary .check{width:14px;height:14px;line-height:14px;vertical-align:top;margin:18px 16px 0 16px}
     .summaryWrap .summary .delete{margin-left:16px;color:#f55c5c}
+    .summaryWrap .summary .right .item{position:relative;text-align:right;}
     .summaryWrap .summary .right .item em{color:#f55c5c}
     .summaryWrap .summary .submit{display:inline-block;width:232px;height:50px;line-height:50px;text-align:center;background-color:#e15616;border-radius:2px;color:#FFF}
     .summaryWrap.fixed{position:fixed;bottom:0;left:0;margin:0;z-index:99999;box-shadow:20px 20px 102px rgba(0,0,0,0.1)}
     .pageTitle .step label.line{width: 50px;height: 1px;background: #707070;display: inline-block; margin: 0px 10px}
-
+    .summaryWrap .summary .item .del{position:absolute;right:0;top:-20px;cursor:pointer;height:24px;font-size:14px}
+    .summaryWrap .summary .item .del em{position:absolute;top:-20px;height:24px;white-space:nowrap;right:0;background:#ff4500;line-height:24px;color:#FFF;padding:0 12px;border-radius:2px}
+    .summaryWrap .summary .item .del em:before{content:'';width:0;height:0;border-width:5px 5px 0 5px;border-style:solid;border-color:#ff4500 transparent;position:absolute;top:24px;left:50%}
 
 
 
@@ -140,12 +143,12 @@ li{list-style:none}
     .summaryWrap .summary .right{height:11.2vw;line-height:11.2vw}
     .summaryWrap .summary .check{width:3.5vw;height:3.5vw;line-height:3.5vw;vertical-align:top;margin:3vw 3vw 0 3.3vw}
     .summaryWrap .summary .delete{width:12vw;height:6vw;line-height:6vw;display:inline-block;text-align:center;color:#f55c5c}
-    .summaryWrap .summary .right .item{margin-left:6.8vw}
+    .summaryWrap .summary .right .item{position:relative;text-align:right;margin-left:6.8vw}
     .summaryWrap .summary .right .item em{color:#f55c5c}
     .summaryWrap .summary .submit{display:inline-block;width:30.4vw;height:11.2vw;line-height:11.2vw;text-align:center;background-color:#e15616;border-radius:2px;color:#FFF}
     .summaryWrap.fixed{position:fixed;bottom:0;left:0;margin:0;z-index:99999;box-shadow:20px 20px 102px rgba(0,0,0,0.1)}
-
-
+    .summaryWrap .summary .item .del{position:absolute;right:0;top:-4vw;cursor:pointer;height:6vw;font-size:3.1vw}
+    .summaryWrap .summary .item .del em{position:absolute;top:0;left:100%;height:5vw;white-space:nowrap;background:#ff4500;line-height:5vw;color:#FFF;padding:0 1vw;border-radius:2px}
 
 }
 

+ 63 - 10
src/main/resources/static/js/shopping/cart.js

@@ -7,15 +7,24 @@ var shoppingCart = new Vue({
         listLoading: true,
         listData: [],
         invalidData: [],
+        promotionsList: [],
         kindCount: 0,
         totalCount: 0,
         totalPrice: 0,
+        reducedPrice: 0,
+        originalPrice: 0,
         allChecked: true,
         submitIds: [],//去结算商品Ids
 
     },
-    computed: {
-
+    watch:{
+        listData: {
+            handler: function() {
+                console.log('obj.a changed');
+            },
+            immediate: true,
+            deep: true
+        }
     },
     methods: {
         getCartLists: function () {
@@ -28,6 +37,7 @@ var shoppingCart = new Vue({
                     _self.kindCount = r.data.kindCount;
                     _self.totalCount = r.data.totalCount;
                     _self.totalPrice = r.data.totalPrice;
+                    _self.promotionsList = r.data.promotions;
                     // 默认全选
                     _self.listData.forEach(function(supplier){
                         supplier.checked = true;
@@ -38,6 +48,7 @@ var shoppingCart = new Vue({
                         }
                     });
                     _self.allChecked = true;
+                    _self.computedPrice();
                 }
                 _self.listLoading = false;
             });
@@ -102,27 +113,64 @@ var shoppingCart = new Vue({
                 _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 originalPrice = 0;
+                var reducedPrice = 0;
                 var totalPrice = 0;
                 var kindCount = 0;
                 var totalCount = 0;
                 _self.listData.forEach(function(supplier){
+                    var supplierOriginalPrice = 0;
                     var supplierPrice = 0;
                     if(supplier.cartList.length>0){
                         supplier.cartList.forEach(function(cart){
                             if (_self.submitIds.includes(cart.productId*1)){
+                                supplierOriginalPrice += cart.originalPrice*cart.number;
                                 supplierPrice += cart.price*cart.number;
                                 kindCount += 1;
                                 totalCount += cart.number;
                             }
                         })
                     }
+                    supplier.originalPrice = supplierOriginalPrice;
+                    originalPrice += supplier.originalPrice;
                     supplier.totalPrice = supplierPrice;
                     totalPrice += supplier.totalPrice;
                 });
-                _self.totalPrice = totalPrice;
+                _self.promotionsList.forEach(function(promotions){
+                    if(promotions.mode ===2){
+                        var promotionsPrice = 0;
+                        promotions.productList.forEach(function(product){
+                            promotionsPrice += product.number * product.price;
+                        });
+                        if(promotionsPrice>=promotions.touchPrice){
+                            reducedPrice += promotions.reducedPrice;
+                        }
+                    } else if (promotions.type===1 && promotions.mode ===1) {
+                        promotions.productList.forEach(function(product){
+                            reducedPrice += product.number * (product.originalPrice - product.price);
+                        });
+                    }
+                });
+                _self.reducedPrice = reducedPrice;
+                _self.originalPrice = originalPrice;
+                _self.totalPrice = totalPrice-reducedPrice;
                 _self.kindCount = kindCount;
                 _self.totalCount = totalCount;
             });
@@ -150,18 +198,23 @@ var shoppingCart = new Vue({
                     }
                 });
             }
+            // 设置优惠数量
+            this.setPromotions();
             // 计算价格
             this.computedPrice();
             // 更新购物车
             var _self = this;
             this.$nextTick(function(){
-                tokenAjax("post", "/shoppingCart/update", {
-                    userID: this.userId,
-                    productID: cart.productId,
-                    productCount: cart.number
-                },function (res) {
-                    console.log(res);
-                });
+                _self.updateCart(cart);
+            });
+        },
+        updateCart: function(cart){
+            tokenAjax("post", "/shoppingCart/update", {
+                userID: this.userId,
+                productID: cart.productId,
+                productCount: cart.number
+            },function (res) {
+                console.log(res);
             });
         },
         deleteCart: function(cartId){

+ 27 - 1
src/main/resources/templates/shopping/cart.html

@@ -54,6 +54,32 @@
                         <img class="img" :src="supplier.logo">
                         <span class="name" v-text="supplier.name"></span>
                     </a>
+                    <!--店铺促销-->
+                    <div class="price">
+                        <div v-if="supplier.promotions" class="priceTag">
+                            <i @click="toggleThisLadder($event)" class="tag icon mIcon" v-text="supplier.promotions.name"></i>
+                            <div class="promotion mFixed">
+                                <div>
+                                    <p class="t">
+                                        <em v-if="supplier.promotions.mode==2">{{supplier.promotions.name+',满 ¥'+toFloat(supplier.promotions.touchPrice)+' 减 ¥'+toFloat(supplier.promotions.reducedPrice)}}</em>
+                                        <em v-if="supplier.promotions.mode==3">{{supplier.promotions.name+',满 ¥'+toFloat(supplier.promotions.touchPrice)+' 赠送商品'}}</em>
+                                    </p>
+                                    <p>促销时间:<em v-if="supplier.promotions.status==1">不限时</em><em v-else>{{supplier.promotions.beginTime.substr(0,10)+' ~ '+supplier.promotions.endTime.substr(0,10)}}</em></p>
+                                    <template v-if="supplier.promotions.mode==3">
+                                        <p>赠品:</p>
+                                        <p>
+                                            <span v-for="g in supplier.promotions.giftList" class="p">
+                                                <img :src="g.image">
+                                                <span v-text="g.name"></span>
+                                                <span v-text="'× '+g.number"></span>
+                                            </span>
+                                        </p>
+                                    </template>
+                                    <p class="r"><a class="close" @click="hideThisLadder($event)" href="JavaScript:void(0);">了解</a></p>
+                                </div>
+                            </div>
+                        </div>
+                    </div>
                 </div>
                 <div class="cartItem clear"  v-for="cart in supplier.cartList" >
                     <div class="c0">
@@ -172,7 +198,7 @@
                 <div class="item">商品数量:<em v-text="totalCount"></em></div>
             </div>
             <div class="right mfbt">
-                <div class="item">总价:<em v-text="'¥'+toFloat(totalPrice)"></em></div>
+                <div class="item">合计:<em v-text="'¥'+toFloat(totalPrice)"></em><span class="del"><del v-text="'¥'+toFloat(originalPrice)"></del><em>减{{'¥'+toFloat(reducedPrice)}}</em></span></div>
                 <a class="submit" href="javascript:void(0);" @click="submitCart()">去结算</a>
             </div>
         </div>