您的位置:首页 > 移动开发 > 微信开发

微信小程序 购物车代码

2018-01-15 23:10 399 查看
// pages/goods/goods.js

Page({

  data: {

    goods: [

      {

        "name": "热销榜",

        "type": -1,

        "foods": [

          {

            "name": "皮蛋瘦肉粥",

            "price": 10,

            "oldPrice": "",

            "description": "咸粥",

            "sellCount": 229,

            "Count": 0,

            "rating": 100,

            "info": "一碗皮蛋瘦肉粥,总是我到粥店心满意足",

            "ratings": [

              {

                "username": "3******c",

                "rateTime": 1469281964000,

                "rateType": 0,

                "text": "很喜欢的粥",

                "avatar": "http://static.galileo.xiaojukeji.com/static/tms/default_header.png"

              }

           

            ],

            "icon": "http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/114/h/114",

            "image": "http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/750/h/750"

          }

       

            ],

            "icon": "http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/114/h/114",

            "image": "http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/750/h/750"

          }

         

        ]

      }

    ],

    toView: '0',

    scrollTop: 100,

    foodCounts: 0,

    totalPrice: 0,// 总价格

    totalCount: 0, // 总商品数

    carArray: [],

    minPrice: 20,//起送價格

    payDesc: '',

    deliveryPrice: 4,//配送費

    fold: true,

    selectFoods: [{ price: 20, count: 2 }],

    cartShow: 'none'

  },

  // selectFoods: function (event) {

  //     var food = event.currentTarget.dataset.food;

  //     console.log(food);

  //     console.log(this.data.goods)

  //     let foods = [];

  //     this.data.goods.forEach((good) => {

  //         good.foods.forEach((food) => {

  //             // console.log(food)

  //             if (food.sellCount) {

  //                 foods.push(food);

  //                 console.log(foods);

  //             }

  //         })

  //     })

  // },

  selectMenu: function (e) {

    var index = e.currentTarget.dataset.itemIndex;

    this.setData({

      toView: 'order' + index.toString()

    })

    console.log(this.data.toView);

  },

  //移除商品

  decreaseCart: function (e) {

    var index = e.currentTarget.dataset.itemIndex;

    var parentIndex = e.currentTarget.dataset.parentindex;

    this.data.goods.forEach((good) => {

      good.foods.forEach((food) => {

        var num = this.data.goods[parentIndex].foods[index].Count;

        var mark = 'a' + index + 'b' + parentIndex

        if (food.Count > 0) {

          this.data.goods[parentIndex].foods[index].Count--

          var price = this.data.goods[parentIndex].foods[index].price;

          var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };

          var carArray1 = this.data.carArray.filter(item => item.mark != mark);

          carArray1.push(obj);

          console.log(carArray1);

          this.setData({

            carArray: carArray1,

            goods: this.data.goods

          })

          this.calTotalPrice()

          this.setData({

            payDesc: this.payDesc()

          })

        }

        if (num > 0) {

          var carArray1 = this.data.carArray.filter(item => item.num > 0)

          console.log(carArray1)

          this.setData({

            carArray: carArray1,

          })

        }

      })

    })

  },

  decreaseShopCart: function (e) {

    this.decreaseCart(e);

  },

  //添加到购物车

  addCart(e) {

    var index = e.currentTarget.dataset.itemIndex;

    var parentIndex = e.currentTarget.dataset.parentindex;

    this.data.goods[parentIndex].foods[index].Count++;

    var mark = 'a' + index + 'b' + parentIndex

    var price = this.data.goods[parentIndex].foods[index].price;

    var num = this.data.goods[parentIndex].foods[index].Count;

    var name = this.data.goods[parentIndex].foods[index].name;

    var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };

    var carArray1 = this.data.carArray.filter(item => item.mark != mark)

    carArray1.push(obj)

    console.log(carArray1);

    this.setData({

      carArray: carArray1,

      goods: this.data.goods

    })

    this.calTotalPrice();

    this.setData({

      payDesc: this.payDesc()

    })

  },

  addShopCart: function (e) {

    this.addCart(e);

  },

  //计算总价

  calTotalPrice: function () {

    var carArray = this.data.carArray;

    var totalPrice = 0;

    var totalCount = 0;

    for (var i = 0; i < carArray.length; i++) {

      totalPrice += carArray[i].price * carArray[i].num;

      totalCount += carArray[i].num

    }

    this.setData({

      totalPrice: totalPrice,

      totalCount: totalCount,

      //payDesc: this.payDesc()

    });

  },

  //差几元起送

  payDesc() {

    if (this.data.totalPrice === 0) {

      return `¥${this.data.minPrice}元起送`;

    } else if (this.data.totalPrice < this.data.minPrice) {

      let diff = this.data.minPrice - this.data.totalPrice;

      return '还差' + diff + '元起送';

    } else {

      return '去结算';

    }

  },

  //結算

  pay() {

    if (this.data.totalPrice < this.data.minPrice) {

      return;

    }

    // window.alert('支付' + this.totalPrice + '元');

    //确认支付逻辑

    var resultType = "success";

    wx.redirectTo({

      url: '../goods/pay/pay?resultType=' + resultType

    })

  },

  //彈起購物車

  toggleList: function () {

    if (!this.data.totalCount) {

      return;

    }

    this.setData({

      fold: !this.data.fold,

    })

    var fold = this.data.fold

    //console.log(this.data.fold);

    this.cartShow(fold)

  },

  cartShow: function (fold) {

    console.log(fold);

    if (fold == false) {

      this.setData({

        cartShow: 'block',

      })

    } else {

      this.setData({

        cartShow: 'none',

      })

    }

    console.log(this.data.cartShow);

  },

  listShow() {

    if (!this.data.totalCount) {

      this.data.fold = true;

      return false;

    }

    let show = !this.fold;

    // if (show) {

    //     this.$nextTick(() => {

    //         if (!this.scroll) {

    //             this.scroll = new BScroll(this.$refs.listContent, {

    //                 click: true

    //             });

    //         } else {

    //             this.scroll.refresh();

    //         }

    //     });

    // }

    return show;

  },

  onLoad: function (options) {

    // 页面初始化 options为页面跳转所带来的参数

    this.setData({

      payDesc: this.payDesc()

    });

  },

  onReady: function () {

    // 页面渲染完成

  },

  onShow: function () {

    // 页面显示

  },

  onHide: function () {

    // 页面隐藏

  },

  onUnload: function () {

    // 页面关闭

  }

})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: