index.js 5.65 KB
import cartApi from '~/api/module/cart.js'
import NP from "number-precision";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    count: 0,
    goodsList: [],
    shoppingcartFlag: true
  },
  goodsActionReady(e) {
    this.setData({
      padding_bottom: e.detail
    })
  },
  changeCount(e) {
    let count = e.detail
    let { userInfo } = this.data
    let {
      index,
      item
    } = e.currentTarget.dataset
    if (item.sellQty == count) return
    let para = {
      goodsId: item.goodsId,
      goodsSource: item?.goodsSource,
      scoreSource: userInfo?.channelSource,
      merchantId: userInfo?.merchantId,
      specificationData: item?.specificationData
    }
    this.setData({
      [`goodsList[${index}].sellQty`]: count
    }, () => {
      this.cheackAll()
      wx.p.wxApi.showLoading()
      // 调用添加购物车接口
      if (count > item.sellQty) {
        para.sellQty = NP.minus(count, item.sellQty) // 修改后数量减去之前数量
        cartApi.addToCart(para).then(() => {
          wx.hideLoading()
        })
      } else { // 调用减少物车商品接口
        para.sellQty = NP.minus(item.sellQty, count)
        cartApi.updateGoodsFormCart(para).then(res => {
          wx.hideLoading()
        })
      }
    })
  },
  choose(e) {
    let {
      index,
      item
    } = e.currentTarget.dataset
    let {
      goodsList
    } = this.data
    if (goodsList[index].showDel) {
      this.hide(e)
      return
    }
    if (item.flag) return
    this.setData({
      [`goodsList[${index}].active`]: !goodsList[index].active
    })
    this.cheackAll()
  },
  cheackAll() {
    let count = 0
    let sum = 0
    let {
      goodsList
    } = this.data
    try {
      goodsList?.map(e => {
        if (!e.active || e.flag) throw false
      })
    } catch (error) {
    }
    goodsList?.map(e => {
      if (e.active) {
        count += e.sellQty
        sum += e.redeemPoints * e.sellQty
      }
    })
    this.setData({
      count,
      sum: sum == 0 ? 0 : Number(sum),
      allActive: goodsList?.filter(item => item.flag == null).length == goodsList?.filter(val => val.active == true && val.flag == null)?.length
    })
  },
  chooseAll(e) {
    let {
      goodsList,
      allActive
    } = this.data
    try {
      goodsList?.map(val => {
        if (!val.active || val.flag) throw false
      })
    } catch (error) {
      goodsList?.map(val => {
        if (!val.flag) val.active = true
      })
    }
    if (allActive) {
      goodsList?.map((val, index) => {
        val.active = false
      })
    }
    this.setData({
      goodsList,
      allActive: !allActive
    })
    this.cheackAll()
  },
  checkOut() {
    let {
      goodsList
    } = this.data
    let data = []
    goodsList?.map(e => (e.active ? data.push(e) : ''))
    let params = {
      goodsList: data
    }
    wx.p.router.go('/packageIndex/pages/confirmOrder/index', params)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.p.storage.get('userInfo')
    this.setData({userInfo})
    this.getShoppingcartList()
  },
  getShoppingcartList() {
    wx.p.wxApi.showLoading()
    let userInfo = wx.p.storage.get('userInfo')
    let para = {
      scoreSource: userInfo?.channelSource,
      merchantId: userInfo?.merchantId
    }
    cartApi.getCartList(para)
      .then(data => {
        this.setData({
          goodsList: data?.goodsList || [],
          shoppingcartFlag: true,
          count: 0,
          sum: 0,
          allActive: false
        })
        wx.hideLoading()
      })
      .catch(() => {
        this.setData({
          shoppingcartFlag: true
        })
      })
  },
  del(e) {
    wx.p.wxApi.showLoading()
    let {
      target
    } = e.target.dataset
    let { userInfo } = this.data
    // 删除调用减少商品数量接口
    let para = {
      goodsId: target.goodsId,
      sellQty: target.sellQty,
      goodsSource: target?.goodsSource,
      scoreSource: userInfo?.channelSource,
      merchantId: userInfo?.merchantId,
      specificationData: target.specificationData
    }
    wx.p.wxApi.showLoading()
    cartApi.updateGoodsFormCart(para).then(res => {
      wx.hideLoading()
      wx.p.wxApi.showToast('删除成功')
      setTimeout(() => {
        this.getShoppingcartList()
      }, 0.5 * 1000)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShoppingcartList()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getShoppingcartList()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  hide(e) {
    let {
      goodsList
    } = this.data
    let {
      index
    } = e.currentTarget.dataset
    if (goodsList[index].showDel)
      this.setData({
        [`goodsList[${index}].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}].showDel`]: result
    })
  }
})