couponPack.js 2.9 KB
// pages/couponPack/couponPack.js
const app = getApp()
const urls = app.utils.judgeEnv()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // tab切换 
    currentTab: 1,
    couponList: [],
    userId: '-',
    phone: '-',
    showWarn: false,
    showWays: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.currentPage = 1
    this.pageSize = 20
    this.isLogin()
  },
  swichNav(e){
    this.setData({
      currentTab: e.target.dataset.current,
      couponList: []
    });
    this.init()
  },
  /** 
   * 滑动切换tab 
   */
  bindChange: function(e) {
    this.setData({
      currentTab: e.detail.current,
      couponList: []
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if(this.isTakePhoto){
      this.isTakePhoto = false
      return
    }
    this.currentPage = 1
    this.setData({
      couponList: []
    })
    this.init()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      couponList: []
    })
    this.currentPage = 1
    this.init()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if(this.hasMore){
      this.currentPage += 1
      this.init()
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
  isLogin(){
    this.setData({
      userId: wx.getStorageSync('nick').userId,
      phone: wx.getStorageSync('nick').phone
    })
  },
  init(){
    app.api.getMyCoupons({
      isAvailable: this.data.currentTab,
      currentPage: this.currentPage,
      pageSize: this.pageSize
    }).then(res => {
      res.data.datalist.map(item => {
        item.validEndTime_label = app.utils.format(new Date(item.validEndTime), "yyyy.MM.dd")
        item.validStartTime_label = app.utils.format(new Date(item.validStartTime), "yyyy.MM.dd")
        return item
      })
      this.setData({
        couponList: this.data.couponList.concat(res.data.datalist)
      })
      if(res.data.items > this.data.couponList.length){
        this.hasMore = true
      }else{
        this.hasMore = false
      }
    })
  },
  takePhoto(e){
    this.isTakePhoto = true
  },
  scan(){
    wx.scanCode({
      success (res) {
        // success
        var url = res.result
        console.log(url)
        if (url.includes(urls.ywymCode)){//一物一码核销
          let code = app.utils.getQueryString(url, 'r')
          wx.navigateTo({
            url: `/pages/ywymReward/index?code=${code}`
          })
          return
        }
      }
    })
  }
})