wangxin 6 years ago
parent
commit
0b477399c6
3 changed files with 39 additions and 15 deletions
  1. 34 6
      template/carts/index.js
  2. 4 3
      template/carts/index.wxml
  3. 1 6
      template/home/index.js

+ 34 - 6
template/carts/index.js

@@ -5,20 +5,18 @@ Page({
   data: {
     isUseAddress: false
   },
-
   onLoad: function (options) {
-    // 初始化购物车
-    //self.dever.carts(self, 'carts.save', 'carts/index');
-
+    //获取自定义授权组件
     this.authdialog = this.selectComponent("#authdialog");
 
     //test - begin
     var list = new Array();
-    for(var i=1; i<2; i++){
+    for(var i=1; i<=10; i++){
       var item = new Object();
       item.name = i+'盒有机韭菜';
       item.price = i*20;
       item.id = i;
+      item.num = 1;
       list.push(item);
     }
     this.dever.setList(this, list);
@@ -34,11 +32,41 @@ Page({
       content: '确认删除该订单吗?',
       success: function(res){
         if (res.confirm){
-          that.dever.alert('订单删除成功!');
+          that.dever.alert('删除订单失败!');
         }
       }
     })
   },
+
+  /**
+   * 减少商品数量>0 and <=1
+   */
+  doSub: function(e){
+    var id = e.currentTarget.dataset.id;
+    var list = this.data.list;
+    for(var i=0; i<list.length; i++){
+      if (list[i].id == id){
+        list[i].num = list[i].num-1<=0?1:list[i].num-1;
+        break;
+      }
+    }
+    this.dever.setList(this, list);
+  },
+  /**
+   * 增加商品数量>1 and <=库存
+   */
+  doAdd: function(e){
+    var id = e.currentTarget.dataset.id;
+    var list = this.data.list;
+    for (var i = 0; i < list.length; i++) {
+      if (list[i].id == id) {
+        list[i].num = list[i].num + 1;
+        break;
+      }
+    }
+    this.dever.setList(this, list);
+  },
+
   /**
    * 使用微信地址
    */

+ 4 - 3
template/carts/index.wxml

@@ -11,6 +11,7 @@
     </view>
     <view class='tips' wx:else><text>使用微信地址</text></view>
     <image class='right' src='/static/img/item-right.png'></image>
+    <!--兼容授权提醒-->
     <authdialog id='authdialog' 
       title='授权提示' 
       content='小程序需要您的授权才能提供更好的服务哦'
@@ -22,9 +23,9 @@
     <view class='oper'>
       <text class='price'>¥ {{item.price}}</text>
       <view class='edit-num'>
-        <image class='sub' src='/static/img/carts-sub.png'></image>
-        <view class='num'>1</view>
-        <image class='add' src='/static/img/carts-add.png'></image>
+        <image class='sub' src='/static/img/carts-sub.png' data-id='{{item.id}}' bindtap='doSub'></image>
+        <view class='num'>{{item.num}}</view>
+        <image class='add' src='/static/img/carts-add.png' data-id='{{item.id}}' bindtap='doAdd'></image>
       </view>
     </view>
     <view class='split_line'/>

+ 1 - 6
template/home/index.js

@@ -41,12 +41,7 @@ Page({
    */
   doBuy: function (event){
     var that = this;
-    var id;
-    if (typeof (event) == 'object') {
-      id = event.currentTarget.dataset.id
-    } else {
-      id = event
-    }
+    var id = event.currentTarget.dataset.id;
     this.dever.isLogin(function(res){
       if(res){
         that.dever.location('carts/index?id=' + id);