index.js 4.6 KB
// index.js
// 获取应用实例
Page({
  data: {
    tabs: ['店铺优惠券', '权益优惠券'],
    curTabIndex: 0,
    couponsList: [],
    imgBaseURL: 'https://img01.glsx.com.cn/weapp/resource/equity-h5-new/mini/',
    showGiftsModal: false,
    giftsList: [],
    haveCouponsList: []
  },
  jump(e) {
    let {
      item
    } = e.currentTarget.dataset
    let url = '/packageMine/pages/order/detail/detail'
    wx.p.router.go(url, item)
  },
  state(e) {
    let {
      key
    } = e.currentTarget.dataset
    let {
      couponsList,
      haveCouponsList
    } = this.data
    couponsList[key].checkCoupon = !couponsList[key].checkCoupon
    if (!couponsList[key].checkCoupon) {
      couponsList[key].userCouponList = haveCouponsList[key].slice(0, 4)
    } else {
      couponsList[key].userCouponList = haveCouponsList[key]
    }
    this.setData({
      couponsList
    })
  },
  changeTab(e) {
    let {
      showLogin
    } = this.data
    if(showLogin) return
    this.data.haveCouponsList = []
    this.setData({
      curTabIndex: e.detail,
      couponsList: [],
      flag: false
    })
    this.getCouponsList()
  },
  bottomToRefresh(e) {
    let isBottom = e.detail.direction
    if (isBottom == 'bottom') {
      //刷新下一页
    }
  },
  getCouponsList() {
    wx.p.wxApi.showLoading('加载中...')
    let {
      curTabIndex,
      isMerchant
    } = this.data
    let {merchantId} = wx.p.storage.get('merchant_info')
    let para = {
      useMemberType: 0
    }
    if (curTabIndex == 1) para.discountType = 8 //线上优惠券加上这个参数
    if (!isMerchant) para.merchantId = merchantId
    wx.p.api.couponsApi.getCardData(para).then(res => {
      wx.hideLoading()
      let list = res.map(item => {
        item.checkCoupon = false
        item.userCouponList.map((itm, index) => {
          itm.receiveStartTime = new Date(itm.receiveStartTime).format('yyyy.MM.dd')
          if (itm.effectiveType == 1) {
            itm.receiveEndTime = new Date(itm.receiveEndTime).format('yyyy.MM.dd hh:mm:ss')
          } else {
            itm.receiveEndTime = new Date(itm.receiveEndTime).format('yyyy.MM.dd')
          }
          itm.discountIntensity = itm.discountIntensity / 100
          return itm
        })
        this.data.haveCouponsList.push(item.userCouponList)
        return item
      })
      list = res.map(item => {
        if (item.userCouponList.length > 4 && curTabIndex != 1) {
          item.userCouponList = item.userCouponList.slice(0, 4)
        }
        return item
      })
      this.setData({
        couponsList: list,
        haveCouponsList: this.data.haveCouponsList,
        flag: true
      })
    })
  },
  async onLoad(options) {
    const showLogin =!wx.p.isLogin
    this.setData({
      showLogin
    })
    //判断只展示当前门店的
    if (options.isMerchant == 1) {
      this.setData({
        isMerchant: true
      })
    }
    if (options.pageFrom == 'directional') {
      this.setData({
        showGiftsModal: true,
        giftsList: options.list
      })
    }
    this.getCouponsList()
  },
  // ***********定向礼包相关内容***********
  clickClose() {
    this.setData({
      showGiftsModal: false
    })
  },
  goInvolidPage() {
    wx.p.router.go('../usedDetail/usedDetail', {
      isOnline: this.data.curTabIndex
    })
  },
  // 去使用
  toUseGifts(e) {
    let {
      item
    } = e.currentTarget.dataset
    let {
      discountType,
      useChannel,
      id
    } = item
    if (discountType == 2 || discountType == 4) {
      wx.p.router.go('/packageMall/pages/coupon/coupon', {
        userCouponId: id,
        useMemberType: item.useMemberType
      })
      return
    }
    /**线上商品代金券 */
    if (discountType == 3 && useChannel == 1) {
      if (item.useMemberType == 1) {
        wx.p.wxApi.showToast('代金券已使用')
        return
      }
      wx.p.router.go('/packageMine/pages/coupon/recommend/recommend', {
        couponInfo: item
      })
      return
    }
    wx.p.router.go('/packageMine/pages/coupon/detail/detail', {
      item
    })
  },
  toUseConpon(e) {
    const {
      brandName,
      goodsId,
      id,
      goodsLogo,
      appearance,
      goodsName,
      couponId
    } = e.currentTarget.dataset.item
    let path = '/packageEquity/pages/commodity/detail/detail'
    let param = {
      supplier: 'HW',
      brandName,
      id: goodsId,
      couponsId: id,
      couponId
    }
    if (appearance == 2) {
      path = '/packageEquity/pages/commodity/chargeDetail/chargeDetail'
      param.productName = goodsName
      param.icon = goodsLogo
    }
    wx.p.router.go(path, {
      inType: 'game',
      param
    })
  }
})