directional.js 6.74 KB
// packageEquity/pages/directional/directional.js
import {
  ssoApi,
  couponsApi,
  shopmallApi
} from '../../../api/api'
import {
  defaultMerchantId
} from '../../../api/environment'
Page({

  data: {
    imgBaseURL: 'https://img02.glsx.com.cn/weapp/resource/equity-h5-new/mini/',
    errorMsg: '',
    phoneNum: '',
    isFocus: false,
    showInput:false
  },
  onbindblur() {
    // this.setData({
    //   isFocus:false
    // })
  },
  clickFocus() {
    this.setData({
      isFocus: true
    })
  },
  deleteInput() {
    this.setData({
      phoneNum: '',
      isFocus: false
    })
  },
  intPhoneNum(e) {
    let value = e.detail.value
    const {
      giftsData
    } = this.data
    let flag = false
    if (giftsData.receiveWay == 1) {
      const mPattern = /^1[34578]\d{9}$/;
      if (!value.match(mPattern)) {
        flag = true
      }
    }
    this.setData({
      phoneNum: e.detail.value,
      errorPhone: flag,
      errorMsg: ''
    })
  },
  toRecivedGifts() {
    const {
      phoneNum,
      errorPhone,
      giftsData
    } = this.data
    if (!phoneNum && giftsData.receiveWay == 1) {
      this.setData({
        errorMsg: '请输入手机号码!'
      })
      return
    }
    if (errorPhone && giftsData.receiveWay == 1) {
      this.setData({
        errorMsg: '请输入正确的手机号码!'
      })
      return
    }
    if ((phoneNum.length < 6 || !phoneNum) && giftsData.receiveWay == 2) {
      this.setData({
        errorMsg: '请输入您车架号后6位领取!'
      })
      return
    }
    // if(giftsData.receivedFlag==2){
    //   wx.$util.showToast('该礼包已被领取')
    //   return
    // }
    this.handlerHttp(phoneNum)
  },
  // 领取礼包接口处理
  handlerHttp(phoneNum) {
    const merchantId = wx.$util.getStorageSync('srcm_shopmall_merchant_id')
    let newMerchantId = merchantId || defaultMerchantId
    const {
      giftsData,
      loginInfo
    } = this.data
    let para = {
      directionalGiftId: giftsData.id,
      nickName: loginInfo.nickName,
      nickUrl: loginInfo.headImgUrl,
      phone: loginInfo.phone,
      receiveCondition: phoneNum,
      merchantId: newMerchantId,
      directionalGiftChildId:this.data.childId
    }
    if(this.data.staff){
      Object.assign(para,this.data.staff)
    }
    wx.$util.showLoading()
    wx.$Track.setTrack({
      ...para,
      type: '领取定向礼包'
    }, 7)
    couponsApi.giftsRecive(para).then(res => {
      wx.$util.showLoading()
      if (res) {
        res.map(item => {
          item.receiveStartTime = new Date(item.receiveStartTime).format('yyyy.MM.dd')
          if (item.effectiveType == 1) {
            item.receiveEndTime = new Date(item.receiveEndTime).format('yyyy.MM.dd hh:mm:ss')
          } else {
            item.receiveEndTime = new Date(item.receiveEndTime).format('yyyy.MM.dd')
          }
          // 这是为了适应详情跳转
          item.couponId = item.id
          item.id = item.userCouponId
          return item
        })
      }
      let temp = JSON.stringify(res)
      wx.navigateTo({
        url: '/packageMine/pages/coupon/index/index?pageFrom=directional&list=' + temp
      })
    })
  },
  login(options) {
    getApp().login(true).then(res => {
      if (res.data) {
        let {
          phone,
          sessionId,
          headImgUrl,
          nickName
        } = res.data
        wx.$authStatus = phone ? false : true
        if (res.data.isLogin) {
          wx.$util.setUserInfo(res.data)
          wx.$util.setSessionId(res.data.sessionId)
        }
        this.setData({
          loginInfo: res.data,
          showAuthBtn: phone ? false : true,
          sessionId,
          showAvatarBtn: headImgUrl ? false : true
        }, () => {
          if (options.scene) { //扫码
            this.getLiveActivityId(decodeURIComponent(options.scene))
          } else { // 短链
            wx.$util.decodeUrlLink(options)
            if (!options.id) {
              return
            }
            if(options.staffUUID){
              const {staffUUID,staffName} = options
              this.data.staff = {
                staffUUID,
                staffName
              }
            }
            if(options.childId){
              this.data.childId = options.childId
            }
            this.getGiftsDetailData(options.id)
          }
        })
      }
    })
  },
  // 授权手机号
  getPhoneNumber({
    detail
  }) {
    delete detail.errMsg
    if (detail.iv && detail.encryptedData) {
      const {
        sessionId
      } = this.data
      if (!sessionId) return
      wx.$util.showLoading()
      detail.sessionId = sessionId
      ssoApi.authUserPhone(detail).then(({
        data,
        code
      }) => {
        wx.hideLoading()
        if (code == '0') {
          wx.$Track.setTrack({},3)
          wx.$authStatus = false
          wx.$util.setUserInfo(data)
          wx.$util.setSessionId(data.sessionId)
          this.data.sessionId = data.sessionId
          this.setData({
            showAuthBtn: false,
            loginInfo: data
          })
          this.toRecivedGifts()
        }
      })
    }
  },
  getUserInfo(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        let {
          loginInfo
        } = this.data
        loginInfo.headImgUrl = res.userInfo.avatarUrl
        loginInfo.nickName = res.userInfo.nickName
        this.setData({
          loginInfo: res.userInfo,
          showAvatarBtn: false
        })
      }
    })
  },
  async onLoad(options) {
    await this.login(options)
  },
  setNaviTitle(title) {
    wx.setNavigationBarTitle({
      title
    })
  },
  // 查询定向礼包详情
  getGiftsDetailData(id) {
    wx.$util.showLoading()
    couponsApi.giftsDetail({
      id,
      sessionId: this.data.sessionId,
      childId:this.data.childId
    }).then(res => {
      wx.hideLoading()
      this.setNaviTitle(res.name)
      this.setData({
        giftsData: res,
        showInput:true
      })
    })
  },
  // 获取scene
  getLiveActivityId(str) {
    console.log(str);
    shopmallApi.getLiveActivityId({
      id: str.split('=')[1]
    }).then(({
      code,
      data,
    }) => {
      if (code == 0) {
        let op = {}
        data.scene.split('&').map(item => {
          const [key, val] = item.split("=")
          op[key] = val
        })
        if (op.merchantId) wx.$util.setStorageSync('srcm_shopmall_merchant_id', op.merchantId)
        if(op.staffUUID){
          const {staffUUID,staffName} = op
          this.data.staff = {
            staffUUID,
            staffName
          }
        }
        if(op.childId){
          this.data.childId = op.childId
        }
        this.getGiftsDetailData(op.id)
      }
    })
  },
})