guoshilong
2023-11-13 54849757852f6ab40eb17afbd03d1d839b60a38d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { ELocalStorageKey, ERouterName } from './types/enums'
import router from '/@/router/index'
 
const whiteList = ['/' + ERouterName.LOGIN] // 设置白名单
 
router.beforeEach(async (to, from, next) => {
  // 确定用户是否已登录过,存在Token
  const hasToken = localStorage.getItem(ELocalStorageKey.Token)
 
  if (hasToken) {
    if (to.path === '/' + ERouterName.LOGIN) {
      // 如果已登录,请重定向到主页
      next({ path: '/' + ERouterName.PROJECT_LIST })
    } else {
      try {
        next() // // 如果不传参数就会重新执行路由拦截,重新进到这里
      } catch (error) {
        next(`/${ERouterName.LOGIN}?redirect=${to.path}`)
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/${ERouterName.LOGIN}?redirect=${to.path}`)
    }
  }
})