permission.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import router from './router'
  2. import { store } from '@/store/index'
  3. import getPageTitle from '@/utils/page'
  4. let asyncRouterFlag = 0
  5. const whiteList = ['login']
  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({ path: '/layout/dashboard' })
  14. } else {
  15. next()
  16. }
  17. } else {
  18. // 不在白名单中并且已经登陆的时候
  19. if (token) {
  20. // 添加flag防止多次获取动态路由和栈溢出
  21. if (!asyncRouterFlag) {
  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. next()
  29. }
  30. }
  31. // 不在白名单中并且未登陆的时候
  32. if (!token) {
  33. next({
  34. name: "login",
  35. query: {
  36. redirect: document.location.hash
  37. }
  38. })
  39. }
  40. }
  41. })