permissions.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // 首页请求地址动态配置
  64. if (store.state.homePageUrl) {
  65. var pathUrl = bb
  66. next(pathUrl)
  67. } else {
  68. next(accessRoutes[0].children[0].path)
  69. }
  70. }
  71. } catch {
  72. await store.dispatch('user/resetAll')
  73. if (recordRoute)
  74. next({
  75. path: '/login',
  76. query: { redirect: to.path },
  77. replace: true,
  78. })
  79. else next({ path: '/login', replace: true })
  80. }
  81. }
  82. }
  83. } else {
  84. routesWhiteList
  85. // next({ path: '/login', replace: true })
  86. if (routesWhiteList.indexOf(to.path) !== -1) {
  87. next()
  88. } else {
  89. if (recordRoute)
  90. next({ path: '/login', query: { redirect: to.path }, replace: true })
  91. else next({ path: '/login', replace: true })
  92. }
  93. }
  94. })
  95. router.afterEach((to) => {
  96. document.title = getPageTitle(to.meta.title)
  97. })