loading.js 5.96 KB
import { checkLoginUserInfo, setRouteData, setLoginInfo, loginModal } from '../lib/index'
import { getSessionId } from '~/utils/cache/storageApi'
import USERAPI from '~/api/usersApi/index'
import { IMG_URL } from '~/utils/constantData'
import md5 from '~/utils/lib/md5'
import { sceneByIdApi } from '~/api/checkProduct.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL
  },

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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const appEnter = wx.getEnterOptionsSync()
    console.log(appEnter, 'appEnter')
    if (appEnter.scene == 1037) {
      if (Object.keys(appEnter.referrerInfo).length) {
        const { extraData } = appEnter.referrerInfo
        if (extraData && Object.keys(extraData).length) this.phoneSmsLogin(extraData)
        else this.init()
      } else this.init()
    } else if ([1047, 1048, 1049].includes(appEnter.scene)) {
      // debugger
      console.log(!this.options.scene, this.options, '!this.options.scene')
      if (!this.options.scene) {
        this.init()
        return
      }
      sceneByIdApi({
        id: decodeURIComponent(appEnter.query.scene).split('=')[1]
      })
        .then(res => {
          const url = res.scene
          const obj = this.arrToObj(url.split('&'))
          if (obj.hasOwnProperty('userName') && obj.hasOwnProperty('password')) {
            wx.$storage.rm('sessionId')
            wx.$storage.rm('userInfo')
            wx.$storage.rm('auto_login_object')
            wx.$storage.rm('posterId')
            wx.$storage.rm('mock_data')
            wx.$storage.rm('car_brand_list')
            wx.$storage.set('firstLogin', 'firstLogin')
            wx.$storage.set('disLikeRecordFlag', true)
            this.accountLogin(obj)
          } else this.init()
        })
        .catch(() => {
          this.init()
        })
    } else this.init()
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },
  arrToObj(list) {
    if (!Array.isArray(list)) {
      return {}
    }
    var obj = {}
    list.forEach(item => {
      let temp = item.split('=')
      obj[temp[0]] = temp[1]
    })
    return obj
  },

  init() {
    wx.nextTick(() => {
      const sessionId = getSessionId()
      //   { url, targetPath = url ? decodeURIComponent(url) : '', reportDate } = this.options
      let path = 'pages.index',
        obj = {}
      // if (targetPath) obj.targetPath = targetPath
      // // 如果是日报跳转进来的
      // if (reportDate) {
      //   obj = {
      //     ...obj,
      //     ...this.options
      //   }
      // }
      console.log(sessionId, 'sessionId');
      if (sessionId) {
        USERAPI.checkSession().then(({ code }) => {
          if (code == '0') {
            // if (targetPath) {
            //   path = setRouteData(targetPath).url
            //   obj = setRouteData(targetPath).obj
            // }
            wx.$router.reLaunch(path, obj)
          } else this.autoLogin(obj)
        })
      } else this.autoLogin(obj)
    })
  },
  autoLogin(obj) {
    const autoLoginObject = wx.$storage.get('auto_login_object')
    if (autoLoginObject) {
      const { noSignInUuid } = autoLoginObject
      // if (Date.now() < failureTime) {
      USERAPI.noSignInLogin({
        noSignInUuid
      }).then(async ({ data, code, message }) => {
        // debugger
        if (code == '0') {
          const { sessionId, } = data
          setLoginInfo(data, sessionId)
        } else {
          try {
            const reload = await loginModal({
              message: message ? message : '登录失败请重试',
              cancelText: '返回',
              confirmText: '确认'
            })
            if (reload) {
              wx.$storage.rm('auto_login_object')
              wx.$storage.rm('car_brand_list')
              wx.$router.reLaunch('login.index', obj)
            }
          } catch (error) {
            console.log(`error`, error);
            wx.$storage.rm('auto_login_object')
            wx.$storage.rm('car_brand_list')
            wx.$router.reLaunch('login.index', obj)
          }
        }
      })
      // } else wx.$router.reLaunch('login.index', obj)
    } else wx.$router.reLaunch('login.index', obj)
  },
  phoneSmsLogin(extraData) {
    USERAPI.phoneSmsLogin({
      mobile: extraData.phone,
      code: extraData.smscode
    })
      .then(({ code, data, message }) => {
        if (code == '0') checkLoginUserInfo(data)
        else {
          wx.$api.showToast(message || '系统异常')
          setTimeout(() => {
            wx.$router.reLaunch('login.index')
          }, 1e3)
        }
      })
      .catch(err => {
        wx.$api.showToast('系统异常')
        setTimeout(() => {
          wx.$router.reLaunch('login.index')
        }, 1e3)
      })
  },
  accountLogin(obj) {
    const params = {
      userName: obj.userName,
      password: md5(obj.password)
    }
    USERAPI.accountLogin(params)
      .then(({ code, data, message }) => {
        this.options = {
          ...this.options,
          ...obj
        }
        console.log(this.options, 'this.options');
        // debugger
        if (code == '0') {
          const { sessionId, loginUserInfoList } = data
          var routeParams = {}
          var list = []
          setLoginInfo(loginUserInfoList[0], sessionId)
        } else {
          wx.$api.showToast(message || '系统异常')
          setTimeout(() => {
            wx.$router.reLaunch('login.index')
          }, 1e3)
        }
      })
      .catch(err => {
        wx.$api.showToast('系统异常')
        setTimeout(() => {
          wx.$router.reLaunch('login.index')
        }, 1e3)
      })
  }
})