getCoupon.js 4.55 KB
// packageShopmall/packageMall/pages/getCoupon/getCoupon.js
import USERAPI from "~/api/usersApi/index";
// import USER from '~/api/usersApi/index'
const navStyle = "background-color: transparent;";
import { IMG_URL } from '~/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMGURL: IMG_URL,
    navStyle,
    showSuccessDialog: false,
    isLogin: false,
    sessionId: '',
    couponList: [],
    count: 3,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.login()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.options, '111options')
    this.getMerchantCouponList()
  },
  navbarReady () {

  },
  login() {
    wx.p.wxApi.showLoading()
    USERAPI.login().then(({
      data
    }) => {
      wx.hideLoading()
      if (data) {
        console.log(data)
        let {
          sessionId
        } = data
        this.setData({
          sessionId,
        })
        if (data.isLogin) {
          this.setData({
            isLogin: true 
          })
        }
      }
    })
  },
  getMerchantCouponList(){
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    wx.p.api.couponsApi.getMerchantCouponsListApi({merchantId,discoverGoodsType:this.options.discoverGoodsType}).then(res => {
        if(res?.length) {
            res.forEach(item =>{
                item.denomination = Number(wx.p.format.centsToyuan(item.denomination||0))
            })
            this.setData({
                couponList : res
            })
        }
      })
  },
  receiveCoupon(){
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    let couponIds = this.data.couponList?.map(item => item.couponId)
    wx.p.wxApi.showLoading()
    wx.p.api.couponsApi.batchGetMerchantCouponsApi({merchantId,couponIds,discoverGoodsType:this.options.discoverGoodsType}).then(res => {
      wx.hideLoading()
        if(res){
            this.setData({
                showSuccessDialog: true
            },()=>{
                this.countDown()
            })
        }
    })
  },
  goPage(){
    clearInterval(this.data.countDownTime)
    this.toBack()
  },
  countDown() {
    let count = this.data.count
    this.data.countDownTime = setInterval(() => {
      if (count == 0) {
        clearInterval(this.data.countDownTime)
        this.toBack()
        return
      }
      count--
      this.setData({
        count
      })
    }, 1 * 1000)
  },
  JumpDiscoverPage(){
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    const channel = this.options.discoverGoodsType==1 ? 5 : 6
    wx.p.router.reLaunch(`/packageDiscovery/pages/index/index?merchantId=${merchantId}&channel=${channel}`, { notIntercept: true })
  },
  toBack() {
    if(this.options.from) {
        wx.p.router.back()
    }else{
        this.JumpDiscoverPage()
    }
  },
  getPhoneNumber({
    detail
  }) {
    if(detail.errMsg.indexOf('fail')>-1){
        if(!this.options.from){
            this.JumpDiscoverPage()
        } //从列表和详情进来的话,如果点击不允许授权,则留在本页面
        return;
    }
    const {
      sessionId
    } = this.data
    if (!sessionId) return
    USERAPI.authUserPhone(detail, sessionId).then(({
      data,
      code,
      message
    }) => {
      wx.hideLoading()
      if (code == '0') {
        wx.p.authStatus = false
        wx.p.util.setUserInfo(data)
        wx.p.util.setSessionId(data.sessionId)
        this.setData({
          sessionId: '',
        })
        const {
          merchantId
        } = wx.p.storage.get('merchant_info')
        wx.p.api.couponsApi.getMerchantCouponsListApi({merchantId,discoverGoodsType:this.options.discoverGoodsType}).then(res => {
            if(res?.length) {
                this.receiveCoupon()
            }
            if(res?.length===0) {
                this.toBack()
            }
        })
      } else wx.p.api.showToast(message)
    })
  },
  // 客户id
  handlerCustomerId() {
    let customerId = wx.p.storage.get('srcm_shopmall_customer_id')
    if (customerId) USERAPI.updataCustomerPhone()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})