You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

  1. import router from './router'
  2. import { store } from '@/store'
  3. import getPageTitle from '@/utils/page'
  4. let asyncRouterFlag = 0
  5. const whiteList = ['Login', 'Init']
  6. router.beforeEach(async(to, from, next) => {
  7. const token = store.getters['user/token']
  8. // 在白名单中的判断情况
  9. // 修改网页标签名称
  10. document.title = getPageTitle(to.meta.title)
  11. if (whiteList.indexOf(to.name) > -1) {
  12. if (token) {
  13. next({ name: store.getters['user/userInfo'].authority.defaultRouter })
  14. } else {
  15. next()
  16. }
  17. } else {
  18. // 不在白名单中并且已经登陆的时候
  19. if (token) {
  20. // 添加flag防止多次获取动态路由和栈溢出
  21. if (!asyncRouterFlag && store.getters['router/asyncRouters'].length === 0) {
  22. asyncRouterFlag++
  23. await store.dispatch('router/SetAsyncRouter')
  24. const asyncRouters = store.getters['router/asyncRouters']
  25. router.addRoutes(asyncRouters)
  26. next({ ...to, replace: true })
  27. } else {
  28. if (to.matched.length) {
  29. next()
  30. } else {
  31. next({ path: '/layout/404' })
  32. }
  33. }
  34. }
  35. // 不在白名单中并且未登陆的时候
  36. if (!token) {
  37. next({
  38. name: 'Login',
  39. query: {
  40. redirect: document.location.hash
  41. }
  42. })
  43. }
  44. }
  45. })