集成后台重构版本
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.

104 lines
3.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import router from './routers'
  2. import store from '@/store'
  3. import Config from '@/settings'
  4. // 导入进度条
  5. import NProgress from 'nprogress'
  6. import 'nprogress/nprogress.css'
  7. import { getToken } from '@/utils/auth'
  8. import { filterAsyncRouter } from '@/store/modules/permission'
  9. import { buildMenus } from '@/api/system/menu'
  10. NProgress.configure({
  11. showSpinner: false
  12. })
  13. // 重新定向白名单
  14. const whiteList = ['/login']
  15. // 路由守卫,路由跳转前执行钩子函数
  16. // to:目标路由对象
  17. // from:当前路由
  18. // next:放行或重载
  19. router.beforeEach((to, from, next) => {
  20. // 如果路由标题设置则显示配置标题和目标菜单标题
  21. if (to.meta.title) {
  22. document.title = to.meta.title + '-' + Config.title
  23. }
  24. // 加载效果
  25. NProgress.start()
  26. // 如果已登录过且要跳转的页面是登录页,执行下一次路由守卫
  27. if (getToken()) {
  28. if (to.path === '/login') {
  29. next({
  30. path: '/'
  31. })
  32. NProgress.done()
  33. } else {
  34. // 保存在store 中路由不为空则放行,判断用户是否拉取用户信息
  35. if (store.getters.roles.length === 0) {
  36. // 拉取user_info
  37. store.dispatch('GetInfo').then(() => {
  38. // 加载动态路由,拉取菜单
  39. loadMenus(next, to)
  40. })
  41. .catch(() => {
  42. store.dispatch('LogOut').then(() => {
  43. // 重新实例化路由对象 避免bug
  44. location.reload()
  45. })
  46. })
  47. // 登录时未拉取菜单,在此拉取
  48. } else if (store.getters.loadMenus) {
  49. // 防止死循环
  50. store.dispatch('updateLoadMenus')
  51. loadMenus(next, to)
  52. } else {
  53. next()
  54. }
  55. }
  56. } else {
  57. // hsa no token
  58. // 在免登录白名单,直接进入
  59. if (whiteList.indexOf(to.path) !== -1) {
  60. next()
  61. } else {
  62. // 否则全部重定向到登录页面
  63. next(`/login?redirect=${to.fullPath}`)
  64. // next()
  65. NProgress.done()
  66. }
  67. }
  68. NProgress.done()
  69. })
  70. export const loadMenus = (next, to) => {
  71. buildMenus().then(res => {
  72. const sdata = JSON.parse(JSON.stringify(res))
  73. const rdata = JSON.parse(JSON.stringify(res))
  74. const sidebarRoutes = filterAsyncRouter(sdata)
  75. const rewriteRoutes = filterAsyncRouter(rdata, true)
  76. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  77. // 存储路由
  78. store.dispatch('GenerateRoutes', rewriteRoutes).then(() => {
  79. /*
  80. * 在addRoutes()之后第一次访问被添加的路由会白屏
  81. * 这是因为刚刚addRoutes()就立刻访问被添加的路由
  82. * 然而此时addRoutes()没有执行结束
  83. * 因而找不到刚刚被添加的路由导致白屏
  84. */
  85. router.addRoutes(rewriteRoutes)
  86. /*
  87. * 确保addRoutes()时动态添加的路由已经被完全加载上去
  88. * replace: true只是一个设置信息
  89. * 告诉VUE本次操作后
  90. * 不能通过浏览器后退按钮返回前一个路由
  91. */
  92. next({ ...to, replace: true })
  93. })
  94. store.dispatch('SetSidebarRouters', sidebarRoutes)
  95. })
  96. }
  97. // 进度条
  98. router.afterEach(() => {
  99. NProgress.done()
  100. })