huanggang 7 years ago
parent
commit
691121b27d
2 changed files with 190 additions and 19 deletions
  1. 172 11
      pages/shopCart/shopCart.js
  2. 18 8
      pages/shopCart/shopCart.wxml

+ 172 - 11
pages/shopCart/shopCart.js

@@ -1,30 +1,43 @@
 const getReq = require('./../../config.js').getReq;
 Page({
   data: {
-    free_info:{
-      type:Array
+    free_info: {
+      type: Array
     },
-    summary:{
-      type:Array
+    summary: {
+      type: Array
     },
-    cart_list:{
-      type:Array
-    }
+    cart_list: {
+      type: Array
+    },
+    all_checked: false,
+    allPrice: 0,
+    allGoodsPrice: 0
   },
   onLoad: function () {
-    
+
   },
-  onShow:function(){
+  onShow: function () {
     let self = this;
     getReq({
       act: 'cart',
       op: 'list'
     }, function (res) {
-      console.log(res);
       if (res.code == 200) {
         let free_info = res.datas.free_info;
-        let summary = res.datas.summary;
+        let summary_datas = res.datas.summary;
         let cart_list = res.datas.cart_list;
+        let summary = [];
+        for (let item of cart_list) {
+          item.checked = false;
+        }
+        for (let i = 0; i < cart_list.length; i++) {
+          for (let j = 0; j < summary_datas.length; j++) {
+            if (cart_list[i].goods_id == summary_datas[j].goods_id) {
+              summary.push(summary_datas[j]);
+            }
+          }
+        }
         self.setData({
           free_info,
           summary,
@@ -32,5 +45,153 @@ Page({
         });
       }
     })
+  },
+
+  goods_checked(e) {
+    let goods_id = e.currentTarget.dataset.goodsid;
+    let cart_list = this.data.cart_list;
+    let all_checked_state = 0;
+    for (let item of cart_list) {
+      if (item.goods_id == goods_id) {
+        item.checked = !item.checked;
+      }
+    }
+    for (let item of cart_list) {
+      if (item.checked) {
+        all_checked_state = all_checked_state + 1;
+      }
+    }
+
+    if (all_checked_state == cart_list.length) {
+      all_checked_state = true;
+    }
+    else {
+      all_checked_state = false;
+    }
+    this.setData({
+      cart_list,
+      all_checked: all_checked_state
+    });
+    this.total();
+  },
+  all_checked() {
+    let cart_list = this.data.cart_list;
+    let all_checked_state = this.data.all_checked;
+    for (let item of cart_list) {
+      item.checked = !all_checked_state;
+    }
+    this.setData({
+      cart_list,
+      all_checked: !all_checked_state
+    });
+    this.total();
+  },
+  req_goods_num(cart_id, quantity,callback){
+    let self = this;
+    getReq({
+      act: 'cart',
+      op: 'edit',
+      cart_id,
+      quantity
+    }, function (res) {
+      if (res.code == 200) {
+        let goods_num = res.datas.goods_num;
+        let cart_list = self.data.cart_list;
+        for (let item of cart_list) {
+          if (item.cart_id == cart_id) {
+            item.goods_num = goods_num;
+          }
+        }
+        self.setData({
+          cart_list
+        });
+        callback;
+        for (let i = 0; i < self.data.cart_list.length;i++) {
+          if (self.data.cart_list[i].checked) {
+            self.total();
+            break;
+          }
+        }
+      }
+      else {
+        wx.showToast({
+          title:res.message,
+          icon:"none"
+        })
+      }
+    })
+  },
+  goods_num_handle(e) {
+    let self = this;
+    let cart_id = e.target.dataset.cartid;
+    let num = e.target.dataset.num;
+    let compute_function = e.target.dataset.function;
+    let goods_id = e.target.dataset.goodsid;
+    let quantity = 1;
+    if (compute_function == 'add') {
+      quantity = num + quantity;
+      this.req_goods_num(cart_id, quantity);
+    }
+    else {
+      quantity = num - quantity;
+      if (quantity <= 0) {
+        wx.showModal({
+          title: "提示",
+          content: "确定要删除吗?",
+          success: function (res) {
+            if (res.confirm) {
+              self.req_goods_num(cart_id, quantity, self.del_cart(cart_id, goods_id));
+            } else if (res.cancel) {
+              return;
+            }
+          }
+        })
+      }
+      else {
+        self.req_goods_num(cart_id, quantity);
+      }
+    }
+  },
+  del_cart(cart_id, goods_id) {
+    let summary = this.data.summary;
+    let cart_list = this.data.cart_list;
+    for (let i = 0; i < summary.length;i++) {
+      if (summary[i].goods_id == goods_id) {
+        summary.splice(i,1);
+      }
+    }
+    for (let i = 0; i < cart_list.length; i++) {
+      if (cart_list[i].goods_id == goods_id) {
+        cart_list.splice(i,1);
+      }
+    }
+    this.setData({
+      summary,
+      cart_list
+    })
+  },
+  total(){
+    let self = this;
+    let cart_item = [];
+    for (let item of this.data.cart_list){
+      cart_item.push(item.cart_id);
+    }
+    let carts = cart_item.join(",");
+    getReq({
+      act: 'member_buy',
+      op: 'calc_cash',
+      ifcart: 1,
+      cart_id: carts
+    }, function (res) {
+      if (res.code == 200) {
+        let datas = res.datas.payinfo;
+        let allPrice = parseFloat((datas.goods_amount + datas.freight - datas.available_pred - datas.full_discount - datas.opgoods_discount).toFixed(2));
+        let allGoodsPrice = parseFloat((datas.goods_amount).toFixed(2));
+        self.setData({
+          allPrice,
+          allGoodsPrice
+        })
+      }
+    })
   }
 })

+ 18 - 8
pages/shopCart/shopCart.wxml

@@ -10,8 +10,13 @@
 <view class='cart_list'>
   <block wx:for="{{summary}}" wx:key="{{index}}">
     <view class='cart_item'>
-      <view class='check_btn'>
-        <view class="icon_empty"></view>
+      <view class='check_btn' data-goodsid="{{cart_list[index]['goods_id']}}" bindtap='goods_checked'>
+        <block wx:if="{{cart_list[index]['checked']}}">
+          <icon type="success" size="15" color="#ff4e4e"></icon>
+        </block>
+        <block wx:else>
+          <view class="icon_empty"></view>
+        </block>
       </view>
       <view class='goods_image'>
         <image mode='scaleToFill' src="{{item.goods_image_url}}"></image>
@@ -24,11 +29,11 @@
           <text class="original_price">天猫价{{item.goods_price}}</text>
         </view>
         <view class='num_handle'>
-          <text class='handle_btn'>-</text>
+          <text class='handle_btn' bindtap='goods_num_handle' data-goodsid="{{cart_list[index]['goods_id']}}" data-cartid="{{cart_list[index]['cart_id']}}" data-function='minus' data-num="{{cart_list[index]['goods_num']}}">-</text>
           <block>
             <text class='num'>{{cart_list[index]['goods_num']}}</text>
           </block>
-          <text class='handle_btn'>+</text>
+          <text class='handle_btn' bindtap='goods_num_handle' data-goodsid="{{cart_list[index]['goods_id']}}" data-cartid="{{cart_list[index]['cart_id']}}" data-function='add' data-num="{{cart_list[index]['goods_num']}}">+</text>
         </view>
       </view>
     </view>
@@ -37,14 +42,19 @@
 </view>
 
 <view class="handle_fixed">
-  <view class="all_check_btn">
-    <view class="icon_empty"></view>
+  <view class="all_check_btn" bindtap='all_checked'>
+    <block wx:if="{{all_checked}}">
+      <icon type="success" size="15" color="#ff4e4e" style='margin-right:22rpx;position:relative;top:7rpx;'></icon>
+    </block>
+    <block wx:else>
+      <view class="icon_empty" style='position:relative;bottom:5rpx;'></view>
+    </block>
     <text>全选</text>
   </view>
   <view style='float:right'>
     <view class='cope'>
-      <text class='bonus_price'>应付:¥50.00</text>
-      <text class="original_price">¥88</text>
+      <text class='bonus_price'>应付:¥{{allPrice}}</text>
+      <text class="original_price">¥{{allGoodsPrice}}</text>
     </view>
     <view class='settlement_btn'>
       <text>结算</text>