h5AuthPhone.js 2.83 KB
// pages/h5AuthPhone/h5AuthPhone.js
import api from '../../api/api.js'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import {
  setConfigInfo
} from '~/store/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    routeObj: {},
    IMG_URL
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow(options) {
    // this.init()
    // console.log(options);
  },
  onLoad(options) {
    console.log(options, '-------');
    this.setData({
      routeObj: options
    })
    this.login()
  },
  async jumpH5(target) {
    const { routeObj } = this.data
    routeObj.authphone = target.phone
    let str = ''
    Object.keys(routeObj).forEach((item, index) => {
      if (!['page', 'h5Page'].includes(item)) {
        str = str + (str ? '&' : '?') + item + '=' + routeObj[item]
      }
    })
    // 获取配置服务数据
    let baseConfigData = await setConfigInfo(app.globalData)
    const url = (baseConfigData.jumpBIUrl || 'https://scrm.glsx.com.cn/glsx-bi-report/#/') + routeObj.h5Page + str
    console.log(url, 'url')
    wx.navigateTo({
      url: `/pages/webView/webView?url=${encodeURIComponent(url)}`
    })
  },

  login() {
    wx.$util.showLoading()
    let _this = this
    wx.login({
      success({
        code
      }) {
        if (code) {
          api
            .wxLogin({
              code,
              appId: wx.getAccountInfoSync().miniProgram.appId
            })
            .then(res => {
              wx.$util.hideLoading()
              if (res.code == '0') {
                _this.setData({
                  loginToken: res.data
                })
              }
            })
        } else {
          wx.$util.showToast('登录失败')
        }
      }
    })
  },

  /**
   * 获取授权手机号
   */
  getPhoneNumber(e) {

    let {
      loginToken
    } = this.data
    let {
      encryptedData,
      iv
    } = e.detail
    if (!encryptedData) return
    wx.$util.showLoading('加载中...')
    api
      .authPhone({
        encryptedData,
        loginToken,
        iv,
        noSignIn: false
      })
      .then(({
        code,
        data,
        message
      }) => {
        wx.hideLoading()
        console.log(data, 'data---')
        if (code == 0) {
          let {
            loginUserInfoList,
            sessionId,
            noSignInUuid,
            needSelectAccount
          } = data
          if (loginUserInfoList.length) {
            this.jumpH5(loginUserInfoList[0])
          } else {
            wx.$router.reLaunch('pages.nothing')
          }
        } else {
          wx.hideLoading()
          wx.showModal({
            title: '温馨提示',
            content: message,
            showCancel: false,
            confirmText: '知道了',
            confirmColor: '#2C66F7'
          })
        }
      })
  },
})