goods.js 2.38 KB
// components/Goods/Goods.js
import {
  centsToyuan
} from '~/utils/format'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: [],
      observer(val, o) {
        if (val) {
          val.forEach(v => {
            v.IMGURL = this.data.IMGURL
            let n = v.goods
            if (n) {
              if (n.activityInfo) {
                n.originalPrice = ''
                n.retailPrice = n.activityInfo.activityPrice
                n.goodsStatistics.saleAmount = n.activityInfo.saleAmount
                if (n.activityInfo.specificationExtraList && n.activityInfo.specificationExtraList.length) {
                  n.retailPrice = n.activityInfo.specificationExtraList[0].retailPrice
                  v.up = true
                }
              } else {
                if (n.originalPrice) n.originalPrice = centsToyuan(n.originalPrice)
                if (n.specificationDetailList && n.specificationDetailList.length) {
                  n.retailPrice = n.specificationDetailList[0].retailPrice
                  v.up = true
                }
              }
              n.retailPrice = Number(centsToyuan(n.retailPrice)).toFixed(2)
              if (n.retailPrice > 9999) {
                n.retailPrice = Number(n.retailPrice / 10000).toFixed(2)
                v.unit = '万'
              }
            }
          })
          this.setData({
            list: val
          })
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMGURL: wx.$imgUrl
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setWidth() {
      wx.p.wxApi.getViewInfo(
        '.Goods',
        res => {
          this.setData({
            previewHeight: res.width * 2
          })
        },
        this
      )
    },
    jump(e) {
      let {
        item
      } = e.currentTarget.dataset
      let {
        id,
        activityInfo
      } = item.classifyName && item.classifyName == '秒杀' ? item : item.goods
      let isActivity = false
      if (activityInfo) {
        id = activityInfo.id
        isActivity = true
      }
      if (item.classifyName && item.classifyName == '秒杀') {
        isActivity = true
      }
      wx.p.router.go('/packageMall/pages/detail/detail', {
        id,
        isActivity,
        notIntercept: true
      })
    }
  },
  lifetimes: {
    attached() {
      this.setWidth()
    }
  }
})