index.js 5.19 KB
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [{
      
    }],
    chooseIndex: null,
    shoppingcartFlag: true
  },
  goodsActionReady(e) {
    this.setData({
      padding_bottom: e.detail
    })
  },
  changeCount(e) {
    console.log(e)
    let count = e.detail
    let {
      index,
      item,
      ind
    } = e.currentTarget.dataset
    if (item.goodsNumber == count) return
    let {
      chooseIndex
    } = this.data
    this.setData({
      [`goodsList[${index}].list[${ind}].goodsNumber`]: count
    })
    if (chooseIndex == index) this.cheackAll(index)
    wx.p.wxApi.showLoading('正在加载...')
    let params = {
      id: item.id,
      goodsId: item.goodsId,
      goodsNumber: count
    }
    if (item.specificationKey) params.specificationKey = item.specificationKey
    wx.p.api.shopmallApi.updateShoppingcart(params).then(res => {
      wx.hideLoading()
    })
  },
  choose(e) {
    let {
      index,
      ind
    } = e.currentTarget.dataset
    let {
      goodsList,
      chooseIndex
    } = this.data
    if (chooseIndex != null && chooseIndex != index) return
    if (goodsList[index].list[ind].showDel) {
      this.hide(e)
      return
    }
    this.setData({
      [`goodsList[${index}].list[${ind}].active`]: !goodsList[index].list[ind].active
    })
    this.cheackAll(index)
  },
  cheackAll(index) {
    let count = 0
    let sum = 0
    let {
      goodsList
    } = this.data
    let allActive = true
    try {
      goodsList[index].list.map(e => {
        if (!e.active) throw false
      })
    } catch (error) {
      allActive = error
    }
    goodsList[index].list.map(e => {
      if (e.active) {
        count += e.goodsNumber
        sum += e.retailPrice * e.goodsNumber
      }
    })
    this.setData({
      [`goodsList[${index}].active`]: allActive,
      count,
      sum: sum == 0 ? 0 : Number(sum).toFixed(2),
      chooseIndex: count == 0 ? null : index
    })
  },
  chooseAll(e) {
    let {
      index
    } = e.currentTarget.dataset
    let {
      goodsList,
      chooseIndex
    } = this.data
    if (chooseIndex != null && chooseIndex != index) return
    let all = true
    try {
      goodsList[index].list.map(i => {
        if (!i.active) throw false
      })
    } catch (error) {
      goodsList[index].list.map(i => (i.active = true))
      all = error
    }
    if (all) goodsList[index].list.map(i => (i.active = false))
    this.setData({
      goodsList
    })
    this.cheackAll(index)
  },
  checkOut() {
    let {
      chooseIndex,
      goodsList
    } = this.data
    let {
      list,
      payeeMerchantName,
    } = goodsList[chooseIndex]
    let data = []
    list.map(e => (e.active ? data.push(e) : ''))
    wx.p.router.go('/packageMall/pages/pay/pay', {
      goodsList: data,
      merchantName: payeeMerchantName
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getShoppingcartList()
  },
  getShoppingcartList() {
    wx.p.wxApi.showLoading('加载中...')
    let para = {
      cartType: 1
    }
    wx.p.api.shopmallApi
      .getShoppingcartList(para)
      .then(data => {
        data.map(e => {
          e.list.map(i => (i.retailPrice = Number(i.retailPrice).toFixed(2)))
        })
        this.setData({
          goodsList: data,
          shoppingcartFlag: true,
          count: 0,
          sum: 0,
          chooseIndex: null
        })
        wx.hideLoading()
      })
      .catch(() => {
        this.setData({
          shoppingcartFlag: true
        })
      })
  },
  del(e) {
    wx.p.wxApi.showLoading('请稍后...')
    let {
      id
    } = e.target.dataset
    let para = {
      cartType: 1,
      id
    }
    wx.p.api.shopmallApi.delShoppingcart(para).then(res => {
      wx.p.wxApi.showToast('删除成功')
      setTimeout(() => {
        this.getShoppingcartList()
      }, 1.5 * 1000)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  hide(e) {
    let {
      goodsList
    } = this.data
    let {
      index,
      ind
    } = e.currentTarget.dataset
    if (goodsList[index].list[ind].showDel)
      this.setData({
        [`goodsList[${index}].list[${ind}].showDel`]: false
      })
  },
  start(e) {
    let cell = e.changedTouches[0]
    this.startX = cell.clientX
    this.startY = cell.clientY
  },
  move(e) {
    let {
      startX,
      startY
    } = this
    let {
      index,
      ind
    } = e.currentTarget.dataset
    let {
      clientX,
      clientY
    } = e.changedTouches[0]
    let X = clientX - startX
    let Y = clientY - startY
    let angle = (360 * Math.atan(Y / X)) / (2 * Math.PI)
    let result = clientX < startX
    if (Math.abs(angle) > 30) return
    this.setData({
      [`goodsList[${index}].list[${ind}].showDel`]: result
    })
  }
})