digital.js 8.13 KB
import {
  ssoApi,
  entmicroApi,
  shopmallApi,
  couponsApi
} from '../../../../api/api'
const statusList = ['满减券', '折扣券', '代金券', '兑换券', '购车代金券', '商品折扣券', '会员服务']
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTip: false,
    tabs: ['门店企微', '救援热线'],
    curTabIndex: 0,
    tipList: [{
      index: 0,
      tip: ['业务问题解答', '业务问题解答']
    }, {
      index: 1,
      tip: ['专属权益提醒', '海量优惠活动']
    }],
    haveCoupons: true,
    noResult: true,
    hotLineLs: [],
    pageStatus: 0 // 0正常 1成功 -1失败
  },
  changeTab(e) {
    const { index } = e.currentTarget.dataset
    this.setData({
      curTabIndex: index
    })
  },
  getAddresInfo() {
    let that = this
    let inval = setInterval(() => {
      const addressObj = wx.getStorageSync('address_info')
      if (addressObj) {
        clearInterval(inval)
        that.intvalLocation()
        that.setData({
          address: that.getChangeStr(addressObj.address, 21),
          addressObj
        })
        that.getPageData()
        return
      } else {
        if (that.data.isHttp) return
        that.getPageData()
        that.data.isHttp = true
      }
    }, 1000)
  },
  // getMerchantName() {
  //   let that = this
  //   let inval = setInterval(() => {
  //     const merchant_info = wx.getStorageSync('merchant_info')
  //     if (merchant_info) {
  //       clearInterval(inval)
  //       that.setData({
  //         merchant_info
  //       })
  //     }
  //   }, 1000)
  // },
  intvalLocation() {
    let count = 0
    let intval = setInterval(() => {
      count++
      if (count > 2) {
        clearInterval(intval)
      }
      this.authSuccess()
    }, 30 * 1000)
  },
  callPhone(e) {
    const { phone } = e.currentTarget.dataset
    if (!phone) return
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  longtap() {
    this.data.control = true
  },
  async onLoad(options) {
    if (options && options.scene) {
      this.getLiveActivityId(decodeURIComponent(options.scene))
    }
    // this.getMerchantName()
  },
  toRefresh(options, address) {
    if (address) {
      options.location = address
    }
    if(!this.data.phone)return
    entmicroApi.toFreshCode(options).then(res => {
      this.setData({
        codeImg: res || ''
      })
    })
  },
  getPageData() {
    const { addressObj, phone, nickName, merchantId, pageStatus } = this.data
    let para = {
      location: addressObj ? addressObj.address : '',
      customerPhone: phone,
      customerNikeName: nickName,
      merchantId: merchantId
    }
    wx.$util.showLoading('加载中...')
    this.toRefresh(para)
    if (pageStatus == -1) return wx.hideLoading()
    entmicroApi.getDigitalPageInfo(para).then(res => {
      wx.hideLoading()
      const { merchantName, rescueHotline, isAddWechat, rescueCardId, couponId } = res
      this.checkDetail(couponId)
      this.setData({
        setstoreName: this.getChangeStr(merchantName, 14),
        hotLineLs: rescueHotline || [],
        noResult: rescueHotline && rescueHotline.length ? false : true,
        pageStatus: phone ? isAddWechat : -1,
        haveCoupons: couponId ? true : false,
        params: para,
        rescueCardId
      })
    })
  },
  checkDetail(couponId) {
    if (!couponId) return
    couponsApi.getDigitalCoupons({ couponId }).then(res => {
      const { denomination, couponName, discountType } = res
      let coupons = {
        couponName: this.getChangeStr(couponName, 6),
        discountTypeName: statusList[discountType - 1],
        denomination
      }
      this.setData({
        coupons
      })
    })
  },
  toRegetData(address) {
    const { rescueCardId, params } = this.data
    if (!address) return
    if (!rescueCardId) return
    let para = Object.assign(params || {}, { location: address, rescueCardId })
    entmicroApi.toRegetDigitalData(para).then(res => { })
  },
  toReLocation() {
    if (!this.data.addressObj) {
      this.setData({
        showAuth: true
      })
      return
    }
    this.openMap()
  },
  openMap() {
    const {
      addressObj,
    } = this.data
    this.data.control = false
    wx.openLocation({
      latitude: parseFloat(addressObj.location.lat),
      longitude: parseFloat(addressObj.location.lng),
      address: addressObj.address
    })
  },
  /**授权失败 */
  authFail() {
    this.setData({
      showAuth: true
    })
  },
  /**授权成功 */
  authSuccess() {
    wx.$Track.getAddressInfo().then((res) => {
      this.toRegetData(res.address)
      this.toRefresh(this.data.params, res.address)
    })
  },
  onUnload() {
    clearInterval(this.data.intval)
  },
  /**手机号授权 */
  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.$authStatus = false
          wx.$util.setUserInfo(data)
          wx.$util.setSessionId(data.sessionId)
          this.setData({
            pageStatus: 0,
            isHttp: false,
            phone: data.phone || '',
            nickName: data.nickName || ''
          }, () => {
            this.getAddresInfo()
          })
        }
      })
    }
  },
  /**登录 */
  async login() {
    wx.$util.showLoading('登录中')
    getApp().login(true).then(res => {
      wx.hideLoading()
      if (res.data) {
        let { phone, sessionId, nickName,merchantName } = res.data
        wx.$authStatus = phone ? false : true
        wx.$util.setUserInfo(res.data)
        wx.$util.setSessionId(res.data.sessionId)
        this.setData({
          pageStatus: phone ? 0 : -1,
          sessionId,
          phone,
          nickName,
          showLogin: phone ? false : true,
          showTip: phone&&!nickName ? true : false,
          merchantName
        },()=>{
          this.getAddresInfo()
        })
      }
    })
  },
  loginCallBack(data) {
    if (data) {
      this.setData({
        pageStatus: 0,
        isHttp: false,
        phone: data.phone || '',
        nickName: data.nickName || '',
        showTip: data.nickName ? false : true
      }, () => {
        this.getAddresInfo()
      })
    }
  },
  // 获取scene
  getLiveActivityId(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)
        this.setData({ merchantId: op.merchantId })
        this.login()
      }
    })
  },
  getChangeStr(str, len) {
    if (!str) return ''
    let end = str.len > len ? '...' : ''
    let result = str.substr(0, len) + end
    return result
  },
  closeModal() {
    this.setData({
      showTip: false
    })
  },
  toauthAvatar(e) {
    if(this.data.click)return
    let { confirm } = e.currentTarget.dataset
    let that = this
    if (confirm) {
      wx.getUserProfile({
        desc: '是否授权用户昵称&头像',
        success(res) {
          that.data.nickName = res.userInfo.nickName
          wx.$util.showLoading()
          that.getAddresInfo()
          that.uploadAvatar(res)
        },
        fail() {
          wx.$util.showLoading()
          that.getAddresInfo()
        }
      })
      this.data.click = true
    }
    
    this.setData({
      showTip: false
    })
  },
  // 上传头像到后台存储
  uploadAvatar(data) {
    if (data) {
      ssoApi.authUserInfo({ ...data }).then((code) => {
        if (code == '0') {
          getApp().login().then(({
            data
          }) => {
            wx.$util.setStorageSync("srcm_shopmall_user_info", data);
          })
        }
      })
    }
  },
  onShow() {
    if (this.data.control) {
      let that = this
      wx.$util.showLoading()
      setTimeout(() => {
        that.getPageData()
      }, 5 * 1000)
    }
  }
})