单点登录

88 阅读1分钟

点击跳转,增加token参数,链接获取token

  1. 点击跳转
const url = `${url}?token=${sessionStorage.getItem('ACCESS_TOKEN')}`
window.open(url)
  1. 链接获取token, 地址隐藏token,做到无感

router 前置守卫处理

router.beforeEach((to, from, next) => {
  // 单点登录 token传过来,默认登录
  if (to.query.token) {
    sessionStorage.setItem('loginToken', to.query.token as string)
    setLoginStatus(true)
    const token_arr = to.fullPath.split('?token')
    to.fullPath = token_arr[0] // 地址隐藏掉 token
  }
  const isLogin = getLoginStatus()
  if (!isLogin && !to.meta.notLogin) {
    next({
      path: '/login',
      query: { redirectUrl: to.path || '/' },
    })
  } else {
    next()
  }
})