index.js 2.88 KB
import {
  couponsApi
} from "../../api/api"
const baseImgUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/cgacar/'
import {componentsMixin } from '../../mixins/index'
Component({
  behaviors:[componentsMixin],
  properties: {
    requestFlag: {
      type: Boolean,
      observer(n) {
        if (n) {
          this.getCoupons()
        }
      }
    }
  },
  data: {
    list: [],
    IMGURLTemp:baseImgUrl
  },
  pageLifetimes: {
    show: function () {},
    hide: function () {}
  },
  methods: {
    closeModal() {
      this.setData({
        show: false
      })
    },
    submit() {
      let {
        list
      } = this.data;
      let ids = []
      list.forEach(item => {
        ids.push(item.id)
      })
      this.closeModal()
      // this.triggerEvent('onConfirm', ids)
      this.toReceive(ids)
    },
    toUse(e) {
      this.closeModal()
      let {
        item
      } = e.currentTarget.dataset
      // this.triggerEvent('click', target)
      this.toUserCard(item)
    },
    //一键领取优惠券
    toReceive(ids) {
      let para = {
        couponIdList: ids,
      }
      couponsApi.toReciveCard(para).then(res => {
        if (res) {
          // wx.navigateTo({
          //   url: '/packageMine/pages/coupon/success/success'
          // })
          wx.$router.go('packageMine.success')
        }
      })
    },
    // 去使用
    toUserCard(item) {
      let para = {
        couponIdList: [item.id],
      }
      couponsApi.toReciveCard(para).then(res => {
        if (res) {
          let {
            discountType,
            useChannel,
            id
          } = item
          if (discountType == 2 || discountType == 4) {
            wx.$router.go('packageMall.coupon', {
              userCouponId: id,
              useMemberType: 0
            })
            return
          }
          /**线上商品代金券 */
          if (discountType == 3 && useChannel == 1) {
            wx.$router.go('packageMine.coupon.recommend', {
              couponInfo: item
            })
            return
          }
          let temp = JSON.stringify(item)
          wx.navigateTo({
            url: '/packageMine/pages/coupon/detail/detail?item=' + temp
          })
          // wx.$router.go('packageMine.detail', { item: temp })
        }
      })
    },
    /**获取礼券 */
    getCoupons() {
      let couponIds = wx.getStorageSync('lastCouponIds')
      let para = {
        discountTypes: [3],
        lastCouponIds: couponIds,
        directionalFlag:1
      }
      couponsApi.getHomeCardData(para).then(res => {
        let flag = res.list.length ? true : false
        let ids = couponIds || []
        res.list.forEach(item => {
          ids.push(item.id)
        })
        this.setData({
          list: res.list,
          show: flag,
        })
        if (ids.length > 0) {
          wx.setStorageSync('lastCouponIds', ids)
        }
      })
    },
  }
})