permissions.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. try {
  39. if (loginInterception) {
  40. await store.dispatch('user/getUserInfo')
  41. } else {
  42. //loginInterception为false(关闭登录拦截时)时,创建虚拟角色
  43. await store.dispatch('user/setVirtualRoles')
  44. }
  45. let accessRoutes = []
  46. if (authentication === 'intelligence') {
  47. accessRoutes = await store.dispatch('routes/setRoutes')
  48. } else if (authentication === 'all') {
  49. accessRoutes = await store.dispatch('routes/setAllRoutes')
  50. }
  51. accessRoutes.forEach((item) => {
  52. router.addRoute(item)
  53. })
  54. console.log('accessRoutes')
  55. console.log(accessRoutes)
  56. // alert('判断是否显示中间页的middleStatus值' + store.state.middleStatus)
  57. // alert(store.state.firstMiddleState)
  58. if (store.state.middleStatus == 1 && store.state.firstMiddleState == true) {
  59. next('/middle')
  60. } else {
  61. var aa = (store.state.homePageUrl).slice(7)
  62. var bb = aa.slice(0, -6)
  63. // alert(bb)
  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. })