管理系统PC端
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.
 
 
 
 

76 lines
2.2 KiB

  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import { getToken } from '@/utils/auth'
  7. import { isPathMatch } from '@/utils/validate'
  8. import { isRelogin } from '@/utils/request'
  9. NProgress.configure({ showSpinner: false })
  10. const whiteList = ['/login', '/register', '/app/login', '/app/list', '/app/edit', '/app/map']
  11. const isWhiteList = (path) => {
  12. return whiteList.some(pattern => isPathMatch(pattern, path))
  13. }
  14. router.beforeEach((to, from, next) => {
  15. NProgress.start()
  16. if (store.getters.systemConfigList.length == 0) {
  17. store.dispatch('systemTitle').then(() => {
  18. })
  19. }
  20. if (getToken()) {
  21. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  22. /* has token*/
  23. if (to.path === '/login') {
  24. next({ path: '/' })
  25. NProgress.done()
  26. } else if (isWhiteList(to.path)) {
  27. next()
  28. } else {
  29. if (store.getters.roles.length === 0) {
  30. isRelogin.show = true
  31. // 判断当前用户是否已拉取完user_info信息
  32. store.dispatch('GetInfo').then(() => {
  33. isRelogin.show = false
  34. store.dispatch('GenerateRoutes').then(accessRoutes => {
  35. // 根据roles权限生成可访问的路由表
  36. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  37. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  38. })
  39. }).catch(err => {
  40. store.dispatch('LogOut').then(() => {
  41. Message.error(err)
  42. next({ path: '/' })
  43. })
  44. })
  45. } else {
  46. next()
  47. }
  48. }
  49. } else {
  50. // 没有token
  51. console.log(to.fullPath)
  52. if (isWhiteList(to.path)) {
  53. // 在免登录白名单,直接进入
  54. next()
  55. } else {
  56. if (to.path.indexOf('/app/') > -1){
  57. next(`/app/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页
  58. }else{
  59. next(`/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页
  60. }
  61. NProgress.done()
  62. }
  63. }
  64. })
  65. router.afterEach(() => {
  66. NProgress.done()
  67. })