permissions.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author chuzhixin 1204505056@qq.com
  3. * @description 路由守卫,目前两种模式:all模式与intelligence模式
  4. */
  5. import router from '@/router'
  6. import store from '@/store'
  7. import getPageTitle from '@/utils/pageTitle'
  8. // import * as api from '@/api/publicList'
  9. import {
  10. authentication,
  11. loginInterception,
  12. recordRoute,
  13. routesWhiteList,
  14. } from '@/config'
  15. router.beforeEach(async (to, from, next) => {
  16. let hasToken = store.getters['user/accessToken']
  17. if (!loginInterception) hasToken = true
  18. // console.log(localStorage.getItem('accessToken'))
  19. // console.log(localStorage.getItem('accessToken'))
  20. if (hasToken) {
  21. if (to.path === '/login') {
  22. next({ path: '/' })
  23. } else {
  24. const hasRoles =
  25. store.getters['acl/admin'] ||
  26. store.getters['acl/role'].length > 0 ||
  27. store.getters['acl/ability'].length > 0
  28. if (hasRoles) {
  29. // console.log('to.matched')
  30. // console.log(to.matched)
  31. if (to.matched.length === 0) {
  32. // alert('没有匹配')
  33. next('/404') // 判断此跳转路由的来源路由是否存在,存在的情况跳转到来源路由,否则跳转到404页面
  34. }
  35. next()
  36. } else {
  37. store.commit('getMiddleConfig')
  38. store.commit('getLabelIcon')
  39. try {
  40. if (loginInterception) {
  41. await store.dispatch('user/getUserInfo')
  42. } else {
  43. //loginInterception为false(关闭登录拦截时)时,创建虚拟角色
  44. await store.dispatch('user/setVirtualRoles')
  45. }
  46. let accessRoutes = []
  47. if (authentication === 'intelligence') {
  48. accessRoutes = await store.dispatch('routes/setRoutes')
  49. } else if (authentication === 'all') {
  50. accessRoutes = await store.dispatch('routes/setAllRoutes')
  51. }
  52. accessRoutes.forEach((item) => {
  53. router.addRoute(item)
  54. })
  55. console.log('accessRoutes')
  56. console.log(accessRoutes)
  57. // alert('判断是否显示中间页的middleStatus值' + store.state.middleStatus)
  58. // alert(store.state.firstMiddleState)
  59. if (store.state.middleStatus == 1 && store.state.firstMiddleState == true) {
  60. next('/middle')
  61. } else {
  62. var aa = (store.state.homePageUrl).slice(7)
  63. var bb = aa.slice(0, -6)
  64. // 首页请求地址动态配置
  65. if (store.state.homePageUrl) {
  66. var pathUrl = bb
  67. next(pathUrl)
  68. } else {
  69. next(accessRoutes[0].children[0].path)
  70. }
  71. }
  72. } catch {
  73. await store.dispatch('user/resetAll')
  74. if (recordRoute)
  75. next({
  76. path: '/login',
  77. query: { redirect: to.path },
  78. replace: true,
  79. })
  80. else next({ path: '/login', replace: true })
  81. }
  82. }
  83. }
  84. } else {
  85. routesWhiteList
  86. // next({ path: '/login', replace: true })
  87. if (routesWhiteList.indexOf(to.path) !== -1) {
  88. next()
  89. } else {
  90. if (recordRoute)
  91. next({ path: '/login', query: { redirect: to.path }, replace: true })
  92. else next({ path: '/login', replace: true })
  93. }
  94. }
  95. })
  96. router.afterEach((to) => {
  97. document.title = getPageTitle(to.meta.title)
  98. })