routerEach.js 869 B

123456789101112131415161718192021222324252627282930
  1. import router from '../router/index.js'
  2. // const routeList = router.options.routes[0].children
  3. const whiteList = ['/','/invite']
  4. router.beforeEach((to, from, next) => {
  5. let userToken = window.localStorage.getItem('token')
  6. console.log('--------------------------------aaaaaaaaaaaaaaaaaa',userToken)
  7. if (userToken) {
  8. // 已登录用户禁止访问登录页,直接跳转到目标页
  9. if (to.path === '/') {
  10. next({ path: '/cc_list' });
  11. } else {
  12. next(); // 允许访问其他受保护路径
  13. }
  14. } else {
  15. // 如果用户未登录,则判断是否在白名单中,如果在白名单中,则直接进入,否则重定向到登录页
  16. if (whiteList.includes(to.path)) {
  17. next()
  18. } else {
  19. console.log(to.path)
  20. if(to.path === '/login') {
  21. next()
  22. }else {
  23. next('/login')
  24. }
  25. }
  26. }
  27. })