index.js 5.18 KB
// packageActivity/pages/redEnvelope/index/index.js
import {
  jumpAuthMixin
} from '~/mixins/index'
import {
  domain
} from '~/api/env/index'
Page({
  behaviors: [jumpAuthMixin],
  /**
   * 页面的初始数据
   */
  data: {
    count: 3
  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const {
      options
    } = this
    console.log(options)
    if (options.isLogin) this.getRedEnvelopeH5()
    else this.jumpLoading() //直接打开时跳转、防止sessionId失效
  },
  async getRedEnvelopeH5() {
    const res = await wx.p.router.intercept()
    if (!res) {
      const {
        userInfo
      } = this.data
      let {
        id,
        awardInfoId,
        activityId,
        businessId,
        source,
        linkId
      } = this.options
      if (!activityId) {
        activityId = id
        this.options.activityId = id
      }
      if (!userInfo.nickName) return this.getNickNameAndHeadUrl()
      try {
        const {
          sourceType
        } = await wx.p.api.solitaireApi.getActivityRedEnvelopeDetail({
          id: activityId
        })
        /**接龙奖品回显 */
        if (awardInfoId) this.jumpRedEnvelopeH5({
          activityId
        }, () => {
          this.jump()
        })
        else {
          if (linkId) this.jumpRedEnvelopeH5({
            activityId,
            linkId,
            source
          })
          else {
            const {
              code,
              data,
              errId,
              message
            } = await wx.p.api.questionnaireApi.bindRedEnvelopeLink({
              activityId,
              bindingSource: source,
              wechatName: userInfo.nickName,
              businessId
            })
            if (code == 0) {
              if (data.id && data.link) {
                // /**sourceType 0-自研,1-外采 */
                if (sourceType == 1) {
                  this.showRedModel()
                  this.jumpUrl = data.link
                } else this.jumpRedEnvelopeH5({
                  activityId,
                  linkId: data.id,
                  source
                })
              } else throw {
                message,
                errId
              }
            } else throw {
              message,
              errId
            }
          }
        }
      } catch ({
        message,
        errId
      }) {
        if (message) wx.showModal({
          title: '温馨提示',
          content: message,
          confirmText: '知道了',
          showCancel: false,
          confirmColor: '#F13C3D',
          success: function (res) {
            if (res.confirm) {
              if (errId == '202207218' || errId == '202207224') wx.p.router.redirectTo('/packageActivity/pages/redEnvelope/record/record')
              else wx.p.router.switchTab('/pages/tab/mine/index') //错误统一跳转至我的
            }
          }
        })
      }
    }
  },
  jumpRedEnvelopeH5({
    activityId,
    linkId,
    source
  }, callBack) {
    const {
      awardInfoId,
      merchantId,
      recordId
    } = this.options, {
      userInfo: {
        accountId,
        nickName,
        unionId,
        openid,
        phone
      },
      userInfo
    } = this.data, {
      sessionId
    } = wx.p.storage.get()
    let user = encodeURIComponent(JSON.stringify({
        accountId,
        nickName,
        unionId,
        openid,
        phone
      })),
      query = `activityId=${activityId}&merchantId=${merchantId || userInfo.merchantId}&sessionId=${sessionId}&userInfo=${user}`
    if (linkId) query += `&linkId=${linkId}`
    // 绑定来源 0:权益会员卡;1:调查问卷;2:群接龙
    if (source > -1) query += `&source=${source}`
    if (recordId) query += `&recordId=${recordId}`
    if (awardInfoId) query += `&awardInfoId=${awardInfoId}`
    this.linkQuery = query
    if (callBack) callBack()
    else this.showRedModel()
  },
  showRedModel() {
    this.setData({
      show: true
    })
    this.countDown()
  },
  countDown() {
    let {
      count,
      isJump
    } = this.data
    let _this = this
    if (count == 0) {
      !isJump && this.jump()
      if (this.timer) clearTimeout(this.timer)
      return
    }
    this.timer = setTimeout(() => {
      _this.setData({
        count: count - 1
      })
      this.countDown()
    }, 1e3)
  },
  jump() {
    this.setData({
      isJump: true,
      show: false,
    })
    if (this.timer) clearTimeout(this.timer)
    /**jumpUrl 外采链接 */
    let url = this.jumpUrl ? this.jumpUrl : `${domain}/scrm-wx-red-packet/#/redEnvelope?${this.linkQuery}` //测试、预发布、生产
    // url: `${domain}/dj-scrm-wx-work-main-lwk/#/?${this.linkQuery}` //开发环境
    wx.p.router.redirectToH5(url)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  }
})