index.js 2.22 KB
import { IMG_URL } from '~/config';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    goodsId: {
      type: Number,
      observer: function (val) {
        if (val) {
          this.getGoodscouponList(val);
        }
      },
    },
    couponsId:Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL: IMG_URL,
    currentIndex: 0,
    current: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectCoupons(e) {
      const { index } = e.currentTarget.dataset
      this.setData({
        currentIndex: index
      })
    },
    cancel() {
      this.setData({
        show: false
      })
    },
    comfirm() {
      const { couponData, currentIndex } = this.data
      this.triggerEvent('getCoupons', couponData[currentIndex])
      this.cancel()
    },
   async getGoodscouponList(id) {
      const authStatus = await wx.p.router.intercept(false)
      if (authStatus) return
      let currentIndex = 0
      wx.p.api.shopmallApi
        .getEquitycouponList({
          id,
          couponType: 8
        })
        .then(({ code, data, message }) => {
          let list =
            data &&
            data.map(item => {
              item.denomination = wx.p.format.decimal(item.denomination / 100, 2)
              if (item.effectiveEndTime || item.effectiveStartTime) {
                item.effectiveEndTime = new Date(item.effectiveEndTime).format('yyyy.MM.dd')
                item.effectiveStartTime = new Date(item.effectiveStartTime).format('yyyy.MM.dd')
              }
              return item
            })
          if (list.length) {
            if(this.data.couponsId){
              let flag = false
              list.forEach((el,index)=>{
                if(el.couponId==this.data.couponsId){
                  flag = true
                  this.triggerEvent('getCoupons', el)
                  currentIndex = index
                }
              })
              if(!flag){
                this.triggerEvent('getCoupons', list[0])
              }
            }else{
              this.triggerEvent('getCoupons', list[0])
            }
          }
          this.setData({
            couponData: list,
            currentIndex
          })

        })
    },
  }
})