coupon.js 8.22 KB
// packageMall/pages/coupon/coupon.js
import {
  wxqrcode as QR
} from "~/lib/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shareStaff: {},
    discountType: {
      1: '满减券',
      2: '折扣券',
      3: '代金券',
      4: '兑换券',
      5: '购车代金券',
      6: '商品折扣券',
      7: '会员服务券'
    },
    useMemberType: 0,
    list: [],
    pageNum: 1
  },
  unique(arr) {
    let res = new Set()
    return arr.filter(item => {
      let id = JSON.stringify(item)
      if (res.has(id)) {
        return false
      } else {
        res.add(id)
        return true
      }
    })
  },
  getCouponGoodsPage(res) {
    wx.p.wxApi.showLoading()
    let total = 0
    let {
      list,
      pageNum
    } = this.data
    wx.p.api.shopmallApi
      .getCouponGoodsPage({
        pageNum,
        couponId: res.couponId || res.id,
        merchantId: res.merchantId
      })
      .then(data => {
        console.log(data)
        // let lists = this.unique([...list, ...data.list])
        this.setData({
          list: data.list,
          total
        })
        wx.hideLoading()
      })
  },
  jumpCommodity(e) {
    let {
      item
    } = e.currentTarget.dataset
    // console.log('item',item);
    let {
      id
    } = item
    let isActivity = false
    wx.p.router.go('/packageMall/pages/detail/detail', {
      id,
      isActivity,
      notIntercept: true
    })
  },
  jump(e) {
    let {
      item
    } = e.currentTarget.dataset
    wx.openLocation({
      latitude: item.lat,
      longitude: item.lng,
      name: item.merchantName,
      address: item.merchantAddress
    })
  },
  jumpMap() {
    let {
      couponDetail
    } = this.data
    wx.openLocation({
      latitude: couponDetail.lat,
      longitude: couponDetail.lng,
      name: couponDetail.merchantName,
      address: couponDetail.merchantAddress
    })
  },
  jump() {
    console.log()
    let {
      length
    } = getCurrentPages()
    if (length == 1) wx.p.router.redirectTo('/packageMine/pages/coupon/index/index')
    else wx.p.router.back()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    // 0616添加推荐人
    if (options.shareStaffUuid) {
      this.data.shareStaff = {
        shareStaffUuid: options.shareStaffUuid,
        shareStaffName: options.shareStaffName
      }
      options.item_type = 2
      options.action_type = 1
      options.item_id = options.couponId
      this.data.recommandOptions = options
      // wx.$util.reportData(options, false, false, true)
    }
    // console.log('options',options);
    const {
      userCouponId,
      useMemberType,
      detailInfo
    } = options

    wx.p.wxApi.showLoading('加载中...')
    if (userCouponId) {
      console.log(userCouponId)
      this.setData({
        useMemberType,
        detailInfo
      })

      wx.p.api.couponsApi.getWWCoupon({
        userCouponId
      }).then(res => {
        wx.hideLoading()
        wx.nextTick(() => {
          this.setCouponData(res)
        })
        let code = res.verificationCode.replace(/(.{4})/g, '$1 ')
        this.setData({
          qr: QR.createQrCodeImg(`t=2&c=${res.verificationCode}`),
          verificationCode: code
        })
      })
    }
    if (options && options.couponId) {
      console.log(options)
      wx.p.wxApi.showLoading('加载中...')
      this.checkCoupon(options)
      if (options.activity_id) {
        this.data.activiOptions = options
        // wx.$util.reportData(options, true, false)
      }
    } else {
      let {
        isReceive
      } = this.data
      if (isReceive) this.confirm()
    }
  },
  checkCoupon(options, callBack) {
    wx.p.api.couponsApi.checkCoupon(options).then(couponInfo => {
      console.log(couponInfo)
      if (couponInfo) {
        couponInfo.receiveEndTime = new Date(couponInfo.receiveEndTime).format('yyyy.MM.dd hh:mm:ss')
        couponInfo.receiveStartTime = new Date(couponInfo.receiveStartTime).format('yyyy.MM.dd hh:mm:ss')
        couponInfo.receiveTime = new Date(couponInfo.receiveTime).format('yyyy.MM.dd hh:mm:ss')
      }
      this.setData({
        detailInfo: couponInfo
      })
      wx.p.api.couponsApi.getCouponInfo(options).then(res => {
        this.setCouponData(res)
        this.setData({
          ...options
        })
        if (couponInfo) {
          let code = couponInfo.verificationCode.replace(/(.{4})/g, '$1 ')
          console.log(couponInfo.verificationCode)
          this.setData({
            useMemberType: couponInfo.useMemberType,
            qr: QR.createQrCodeImg(`t=2&c=${couponInfo.verificationCode}`),
            verificationCode: code
          })
        } else {
          if (callBack) callBack(couponInfo)
        }
        wx.hideLoading()
      })
    })
  },
  setCouponData(res) {
    if (res.receiveStartTime) res.receiveStartTime = new Date(res.receiveStartTime).format('yyyy.MM.dd hh:mm:ss')
    if (res.receiveEndTime) res.receiveEndTime = new Date(res.receiveEndTime).format('yyyy.MM.dd hh:mm:ss')
    if (res.receiveTime) res.receiveTime = new Date(res.receiveTime).format('yyyy.MM.dd hh:mm:ss')
    if (res.list) {
      res.list.map(val => (val.verificationTime = new Date(val.verificationTime).format('yyyy.MM.dd hh:mm:ss')))
    }
    if (res.effectiveStartTime) res.effectiveStartTime = res.effectiveStartTime.replace(/-/g, '.')
    if (res.effectiveEndTime) res.effectiveEndTime = res.effectiveEndTime.replace(/-/g, '.')
    if (res.full) res.full = res.full / 100
    if (res.discountType == 3 && res.useChannel == 1) {
      this.getCouponGoodsPage(res)
    }
    console.log(res, '///////////')
    this.setData({
      couponDetail: res
    })
    // setTimeout(() => {
    //   this.setData({
    //     flag: true
    //   })
    // }, 500)
  },
  confirm() {
    let {
      couponId,
      issueName,
      issueDate,
      userCouponId,
      merchantId,
      shareSource
    } = this.data
    if (!couponId && !userCouponId) return
    wx.p.router.intercept().then(response => {
      if (response) {
        this.setData({
          isReceive: true
        })
        return
      }
      let obj = {
        ...this.data.shareStaff
      }
      if (userCouponId) obj.userCouponId = userCouponId
      if (couponId) obj.couponId = couponId
      if (issueName) obj.issueName = issueName
      if (issueDate) obj.issueDate = issueDate
      if (merchantId) obj.merchantId = merchantId
      if (shareSource) obj.shareSource = shareSource
      this.checkCoupon(obj, () => {
        wx.p.api.couponsApi.receiveCoupon(obj).then(({
          code,
          data,
          message
        }) => {
          if (code == '0') {
            // wx.$Track.setTrack({
            //     data,
            //     type: '领取优惠券'
            //   },
            //   7
            // )
            console.log(data)
            if (data.discountType == 3) {
              this.jump()
              return
            }
            this.setData({
              qr: QR.createQrCodeImg(`t=2&c=${data.verificationCode}`),
              verificationCode: data.verificationCode
            })
            wx.p.wxApi.showToast('领取成功')
            this.checkCoupon(obj)
            if (this.data.activiOptions) {
              // wx.$util.reportData(this.data.activiOptions, false, true)
            }
            if (this.data.recommandOptions) {
              let options = this.data.recommandOptions
              options.item_type = 2
              options.action_type = 2
              options.item_id = options.couponId
              // wx.$util.reportData(options, false, false, true)
            }
          } else {
            wx.p.wxApi.showToast(message)
          }
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // wx.$util.customerbehavior(2)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {}
})