loading.js 3.77 KB
import {
  setRouteData,
  setLoginInfo,
  loginModal,
  selectAccountLogin
} from '../lib/index'
import {
  getSessionId,
} from '~/utils/cache/storageApi'
import USERAPI from '~/api/usersApi/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.init()
    this.drectLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },
  init() {
    const sessionId = getSessionId(),
      {
        url,
        targetPath = url ? decodeURIComponent(url) : ''
      } = this.options
    wx.nextTick(() => {
      let path = '/pages/tab/index/index',
        obj = {}
      if (targetPath) obj.targetPath = targetPath
      console.log(obj,'objobj')
      if (sessionId) {
        USERAPI.checkSession().then(({
          code
        }) => {
          if (code == '0') {
            if (targetPath) {
              path = setRouteData(targetPath).url
              obj = setRouteData(targetPath).obj
            }
            console.log(path,'pathpath')
            wx.$router.reLaunch(path, obj)
          }
           else wx.$router.reLaunch('/pages/authentication/login/login', obj)
        })
      }
       else wx.$router.reLaunch('/pages/authentication/login/login', obj)
    })
  },
  autoLogin(obj) {
    const autoLoginObject = wx.$storage.get('auto_login_object')
    if (autoLoginObject) {
      const {
        failureTime,
        userName,
        password
      } = autoLoginObject
      if (Date.now() < failureTime) {
        USERAPI.accountLogin({
          userName,
          password
        }).then(async ({
          data,
          code,
          message
        }) => {
          if (code == '0') {
            setLoginInfo(data, data.sessionId)
          } else {
            try {
              const reload = await loginModal({
                message: message || '登录失败请重试',
                cancelText: '返回'
              })
              if (reload) this.onShow()
            } catch (error) {
              wx.$router.reLaunch('login.index', obj)
            }
          }
        })
      } else wx.$router.reLaunch('login.index', obj)
    } else wx.$router.reLaunch('login.index', obj)
  },
  // 判断是否是从其他小程序跳转过来
  drectLogin(){
    const params = wx.getEnterOptionsSync()
    console.log(params);
    if(params.referrerInfo.extraData&&params.referrerInfo.extraData.inType=='direct'){
      const {phone,smscode} = params.referrerInfo.extraData
      this.freeHandleLogin(phone,smscode)
    }else{
      this.init()
    }
  },
  // 免登录操作流程
  freeHandleLogin(phone,smscode){
    const para = {
      mobile: phone,
      code: smscode
    }
    USERAPI.phoneSmsLogin(para).then(({
      code,
      data,
      message,
      errId
    }) => {
      if (code == '0') {
        this.checkLoginUserInfo(data)
      }else{
        wx.$api.showToast(message || '系统异常')
      }
    }).catch(err => {
      wx.$api.showToast('系统异常')
    })
  },
  checkLoginUserInfo({
    loginUserInfoList,
    sessionId
  }) {
    if (loginUserInfoList.length > 1) wx.$router.reLaunch('/pages/authentication/accountItems/accountItems', {
      sessionId,
      ...this.options
    })
    /**只有一个账号 */
    else selectAccountLogin({
      id: loginUserInfoList[0].uuid,
      sessionId
    })
  },
})