action.js 2.06 KB
import NP from 'number-precision'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    carList: {
      type: Array,
      observer: function (val) {
        if (val && val.length) {
          this.computePrice(val)
        }
      }
    },
    total: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    count: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toShowDetailPopup() {
      this.triggerEvent('show')
    },
    toBuyCar() {
      wx.$router.go('/packageShopmall/packageMall/pages/shoppingCart/shoppingCart')
    },
    checkOut() {
      let {
        list,
        payeeMerchantName,
        payeeMerchantId
      } = this.data.carList[0]
      let _temp_source = {}
      if(list.length==1){
        _temp_source.discoverGoodsSource = list[0].goodsSource
      }
      if(list.length>1){
        this.toBuyCar()
        return
      }
      wx.$router.go('/packageDiscovery/pages/pay/index', {
        goodsList: list,
        merchantName: payeeMerchantName,
        deliveryType: 0,
        merchantId: payeeMerchantId,
        isCarOwner: true,
        ..._temp_source
      })
    },
    computePrice(arr) {
      let total = 0,
        count = 0,
        list = [],
        payeeMerchantName = '',
        payeeMerchantId=''
      if (arr.length) {
        payeeMerchantName = arr[0].payeeMerchantName
        payeeMerchantId = arr[0].payeeMerchantId
        arr.forEach(el => {
          el.list.forEach(ele => {
            count = count + ele.goodsNumber
            list.push(ele)
          })
        })
        total = list.reduce((acc, cur) => NP.plus(acc,NP.times(cur.activityPrice,cur.goodsNumber)), 0);
      }

      wx.$eventbus.emit('MoneyDetail', {
        list,
        total,
        payeeMerchantName,
        payeeMerchantId
      })
      this.setData({
        count,
        // total,
      })
    }
  },
  lifetimes: {
    attached() {
      wx.$eventbus.on('getBuyCarList', (res) => {
        this.computePrice(res || [])
      })
    },
    detached: function () {
      wx.$eventbus.off('getBuyCarList')
    }

  },
})