permission.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import router from './router'
  2. import {ElMessage} from 'element-plus'
  3. import NProgress from 'nprogress'
  4. import 'nprogress/nprogress.css'
  5. import {getToken} from '@/utils/auth'
  6. import {isHttp, isPathMatch} from '@/utils/validate'
  7. import {isRelogin} from '@/utils/request'
  8. import useUserStore from '@/store/modules/user'
  9. import useSettingsStore from '@/store/modules/settings'
  10. import usePermissionStore from '@/store/modules/permission'
  11. NProgress.configure({showSpinner: false})
  12. const whiteList = ['/login', '/register', '/xjc_index',
  13. '/xjc-integratedmachine/wakeup/index',
  14. '/xjc-integratedmachine/wakeup/rainbow/paint',
  15. '/xjc-integratedmachine/wakeup/rainbow/paint2',
  16. '/xjc-integratedmachine/wakeup/rainbow/paint3',
  17. '/xjc-integratedmachine/cognize/index',
  18. '/xjc-integratedmachine/environment/index',
  19. '/xjc-integratedmachine/decision/index',
  20. '/xjc-integratedmachine/plan/index',
  21. '/xjc-integratedmachine/wakeup/index',
  22. '/xjc-integratedmachine/wakeup/instructions',
  23. '/xjc-integratedmachine/wakeup/recognize',
  24. '/xjc-integratedmachine/wakeup/career_recognize/index',
  25. '/xjc-integratedmachine/wakeup/career_maturity/index',
  26. '/xjc-integratedmachine/wakeup/career_example/index',
  27. '/xjc-integratedmachine/wakeup/career_cinema/index',
  28. '/xjc-integratedmachine/login/login_index',
  29. '/xjc-integratedmachine/login/student_login',
  30. '/xjc-integratedmachine/login/visitor_login',
  31. '/xjc-integratedmachine/login/student_forgetpass'
  32. ]
  33. const isWhiteList = (path) => {
  34. return whiteList.some(pattern => isPathMatch(pattern, path))
  35. }
  36. router.beforeEach((to, from, next) => {
  37. NProgress.start()
  38. if (getToken()) {
  39. to.meta.title && useSettingsStore().setTitle(to.meta.title)
  40. /* has token*/
  41. if (to.path === '/login') {
  42. next({path: '/'})
  43. NProgress.done()
  44. } else if (isWhiteList(to.path)) {
  45. next()
  46. } else {
  47. if (useUserStore().roles.length === 0) {
  48. isRelogin.show = true
  49. // 判断当前用户是否已拉取完user_info信息
  50. useUserStore().getInfo().then(() => {
  51. isRelogin.show = false
  52. usePermissionStore().generateRoutes().then(accessRoutes => {
  53. // 根据roles权限生成可访问的路由表
  54. accessRoutes.forEach(route => {
  55. if (!isHttp(route.path)) {
  56. router.addRoute(route) // 动态添加可访问路由表
  57. }
  58. })
  59. next({...to, replace: true}) // hack方法 确保addRoutes已完成
  60. })
  61. }).catch(err => {
  62. useUserStore().logOut().then(() => {
  63. ElMessage.error(err)
  64. next({path: '/'})
  65. })
  66. })
  67. } else {
  68. next()
  69. }
  70. }
  71. } else {
  72. // 没有token
  73. if (isWhiteList(to.path)) {
  74. // 在免登录白名单,直接进入
  75. next()
  76. } else {
  77. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  78. NProgress.done()
  79. }
  80. }
  81. })
  82. router.afterEach(() => {
  83. NProgress.done()
  84. })