退出系统再登录重定向到之前退出时的页面

141 阅读1分钟
  loginOut() {
  //将当前的路由地址传过去
       loginOut(undefined, this.$route.path);
    },
export function loginOut(msg,redirect) {
  // redirect为需要重定向的地址
  Alert(msg ? msg : '退出中...')
  ssoLogout().then(result => {
    if (!redirectToLogin(redirect)) {
      router.push({path: '/login?redirect=' + encodeURIComponent(redirect)})
    }
  })
}

export const ssoLogout = function () {
  return new Promise((resolve, reject) => {
    loginHttp.logout().then(() => {
      clearLoginInfo()
      resolve()
    }).catch(() => {
      clearLoginInfo()
      resolve()
    })
  })
}

/**
 * 清除登录信息
 */
export function clearLoginInfo() {
  store.commit('user/updateToken', '')
  store.commit('user/updateName', '')
  store.commit('user/updateUsername', '')
  store.commit('user/updateMobilePhone', '')
  store.commit('user/updateInfo', {})
  store.commit('common/updateInitAuthority', false)
  localStorage.removeItem('hb_token')
  localStorage.removeItem('hb_username')
  localStorage.removeItem('hb_name')
  localStorage.removeItem('hb_userId')
  localStorage.removeItem('hb_isCountyRule')
}

//重定向
export function redirectToLogin(redirect) {
  let href = window.location.href
  let index = href.indexOf('?token')
  if (index < 0) {
    index = href.indexOf('&token')
  }
  if (index >= 0) {
    let hostIndex = href.indexOf('/#/')
    href = href.substring(0,hostIndex)
    // href = href.substr(0, index)
    if (href.charAt(href.length - 1) === '/') {
      href += '#/login'
      href = '#/login?redirect=' + encodeURIComponent(redirect)
    } else {
      href += '/#/login'
      href = '/#/login?redirect=' + encodeURIComponent(redirect)
    }
    window.setTimeout(() => {
      window.location.href = href
    }, 1500)
    return true
  } else {
    return false
  }
}