index.js 1.25 KB
import {
  setSessionId,
  setUserInfo
} from '~/utils/cache/storageApi'
import USERAPI from '~/api/usersApi/index'
const setRouteData = targetPath => {
  let [url, query] = targetPath.split('?'),
    obj = {}
  if (query) {
    const [key, val] = query.split('=')
    obj = {
      [key]: val
    }
  }
  return {
    url,
    obj
  }
}
/**选择账号登录 */
const selectAccountLogin = ({
  id,
  sessionId
}) => {
  USERAPI.selectAccountLogin({
    id,
    sessionId
  }).then(({
    code,
    data,
    message
  }) => {
    if (code == '0') {
      console.log('选择账号登录')
      setLoginInfo(data.loginUserInfoList[0], data.sessionId)
    } else wx.$api.showToast(message || '系统异常')
  })
}
/**设置登录信息 */
const setLoginInfo = (userInfo, sessionId) => {
  setUserInfo(userInfo)
  setSessionId(sessionId)
  loginSuccess()
}
/**成功跳转 */
const loginSuccess = () => {
  const {
    options: {
      targetPath
    }
  } = wx.$router.getPageInstance()
  console.log(targetPath)
  
  if (targetPath) {
    const {
      url,
      obj
    } = setRouteData(targetPath)
    wx.$router.reLaunch(url, obj)
  } else wx.$router.go('/pages/tab/index/index')
}

module.exports = {
  selectAccountLogin,
  loginSuccess,
  setRouteData,
  setLoginInfo
}