permission.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import router from './router'
  2. // import Layout from '@/layout'
  3. import {
  4. getToken
  5. // deleteToken
  6. } from './utils/auth'
  7. // import store from './store'
  8. import Vue from 'vue'
  9. // 路由拦截
  10. router.beforeEach((to, from, next) => {
  11. // debugger
  12. // 无需登录无需获取动态路由
  13. if (to?.meta?.noLogin) {
  14. next()
  15. return
  16. }
  17. // 前往登陆页面判断
  18. if (to.path === '/login-local') {
  19. if (!getToken()) {
  20. next()
  21. return
  22. }
  23. // onFilterRoutes(to, next)
  24. next('/')
  25. return
  26. }
  27. if (to.path === '/login') {
  28. next('/home')
  29. // return
  30. }
  31. if (!getToken()) {
  32. next('/login-local')
  33. return
  34. }
  35. next()
  36. // onFilterRoutes(to, next)
  37. })
  38. router.afterEach((to, from) => {
  39. document.title = to.meta.title ?? Vue.prototype.$DEFAULT_TITLE
  40. })
  41. // 路由过滤和跳转
  42. // function onFilterRoutes (to, next) {
  43. // // 判断是否刷新页面重新加载路由
  44. // if (!store.getters.refresh) {
  45. // next()
  46. // return
  47. // }
  48. // // if (store.getters.roles && store.getters.roles.length) {
  49. // // initRoutes(to, next, store.getters.roles)
  50. // // return
  51. // // }
  52. // store.dispatch('menu/getMenu2').then(res => {
  53. // initRoutes(to, next, res)
  54. // })
  55. // // .catch(err => {
  56. // // deleteToken()
  57. // // next({ ...to, replace: true })
  58. // // // next('/login-local')
  59. // // Vue.prototype.$snackbar.error(err)
  60. // // })
  61. // }
  62. // function initRoutes (to, next, e) {
  63. // // debugger
  64. // store.commit('menu/SET_REFRESH', false) // 关闭更新
  65. // // 清除路由
  66. // resetRouter()
  67. // const routes = mapASyncRoutes(e)
  68. // // 路由替换
  69. // // debugger
  70. // // if (!routes.length) {
  71. // // deleteToken()
  72. // // // next('/login-local')
  73. // // next({ ...to, replace: true })
  74. // // return
  75. // // }
  76. // router.addRoute({ path: '/', redirect: '/home' })
  77. // routes.forEach(item => {
  78. // console.log(item)
  79. // router.addRoute(item)
  80. // })
  81. // router.addRoute({ path: '*', redirect: '/404', hidden: true })
  82. // // const hasHome = routes.find(_e => _e.name === 'home')
  83. // // router.addRoute({ path: '/', redirect: hasHome ? '/home' : chooseFirst(routes[0]) })
  84. // // router.addRoute({ path: '*', redirect: '/404', hidden: true })
  85. // console.log(router.getRoutes()) // 检查新路由是否已添加
  86. // debugger
  87. // next({ ...to, replace: true })
  88. // }
  89. // function chooseFirst (item) {
  90. // if (item.children && item.children.length > 0 && item.component.name === 'layoutIndex') {
  91. // return chooseFirst(item.children[0])
  92. // }
  93. // return item.path
  94. // }
  95. // 递归路由 转换为组件对象和路径
  96. // function mapASyncRoutes (data) {
  97. // return data.map(item => {
  98. // if (!item.redirect) {
  99. // item.redirect = undefined
  100. // }
  101. // item.component = item.component === 'Layout' ? Layout : loadView(item.component)
  102. // if (item.children && item.children.length > 0) {
  103. // item.children = mapASyncRoutes(item.children)
  104. // }
  105. // return item
  106. // })
  107. // }
  108. // // 路由插件
  109. // function loadView (view) {
  110. // return !view ? { render: (c) => c('router-view') } : () => import(`@/views/${view}`)
  111. // }