多媒体信息发布平台
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.

75 lines
2.5 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
  1. import router from './routers'
  2. import store from '@/store'
  3. import Config from '@/settings'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. // import { getToken } from '@/utils/auth' // getToken from cookie
  7. import { buildMenus } from '@/api/system/menu'
  8. import { filterAsyncRouter } from '@/store/modules/permission'
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. // const whiteList = ['/login', '/forgetPassword'] // no redirect whitelist
  11. router.beforeEach((to, from, next) => {
  12. if (to.meta.title) {
  13. document.title = to.meta.title + ' - ' + Config.title
  14. }
  15. // 加载效果
  16. NProgress.start()
  17. next()
  18. NProgress.done()
  19. // if (getToken()) {
  20. // // 已登录且要跳转的页面是登录页
  21. // if (to.path === '/login') {
  22. // next({ path: '/' })
  23. // NProgress.done()
  24. // } else {
  25. // if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  26. // store.dispatch('GetInfo').then(() => { // 拉取user_info
  27. // // 动态路由,拉取菜单
  28. // loadMenus(next, to)
  29. // }).catch(() => {
  30. // store.dispatch('LogOut').then(() => {
  31. // location.reload() // 为了重新实例化vue-router对象 避免bug
  32. // })
  33. // })
  34. // // 登录时未拉取 菜单,在此处拉取
  35. // } else if (store.getters.loadMenus) {
  36. // // 修改成false,防止死循环
  37. // store.dispatch('updateLoadMenus')
  38. // loadMenus(next, to)
  39. // } else {
  40. // next()
  41. // }
  42. // }
  43. // } else {
  44. // /* has no token*/
  45. // if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  46. // next()
  47. // } else {
  48. // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  49. // NProgress.done()
  50. // }
  51. // }
  52. })
  53. export const loadMenus = (next, to) => {
  54. buildMenus().then(res => {
  55. const sdata = JSON.parse(JSON.stringify(res))
  56. const rdata = JSON.parse(JSON.stringify(res))
  57. const sidebarRoutes = filterAsyncRouter(sdata)
  58. const rewriteRoutes = filterAsyncRouter(rdata, true)
  59. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  60. store.dispatch('GenerateRoutes', rewriteRoutes).then(() => { // 存储路由
  61. router.addRoutes(rewriteRoutes) // 动态添加可访问路由表
  62. next({ ...to, replace: true })
  63. })
  64. store.dispatch('SetSidebarRouters', sidebarRoutes)
  65. })
  66. }
  67. router.afterEach(() => {
  68. NProgress.done() // finish progress bar
  69. })