华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import { getToken } from '@/utils/auth'
  7. import { isPathMatch } from '@/utils/validate'
  8. import { isRelogin } from '@/utils/request'
  9. import { public_saveSimpleLog } from '@/api/business/public/public'
  10. import { getMenuNameEn } from '@/utils/menu'
  11. NProgress.configure({ showSpinner: false })
  12. const whiteList = ['/login', '/lblh']
  13. const isWhiteList = (path) => {
  14. return whiteList.some((pattern) => isPathMatch(pattern, path))
  15. }
  16. router.beforeEach((to, from, next) => {
  17. NProgress.start()
  18. if (getToken()) {
  19. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  20. /* has token*/
  21. if (to.path === '/login') {
  22. next({ path: '/' })
  23. NProgress.done()
  24. } else if (isWhiteList(to.path)) {
  25. next()
  26. } else {
  27. if (store.getters.roles.length === 0) {
  28. isRelogin.show = true
  29. //业务菜单角标
  30. store
  31. .dispatch('GetMenuCount')
  32. .then(() => {})
  33. .catch((error) => {
  34. console.log('GetMenuCount出错:' + (error.msg || ''))
  35. })
  36. // 判断当前用户是否已拉取完user_info信息
  37. store
  38. .dispatch('GetInfo')
  39. .then(() => {
  40. isRelogin.show = false
  41. store.dispatch('GenerateRoutes').then((accessRoutes) => {
  42. // 根据roles权限生成可访问的路由表
  43. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  44. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  45. })
  46. })
  47. .catch((err) => {
  48. store.dispatch('LogOut').then(() => {
  49. Message.error(err)
  50. next({ path: '/' })
  51. })
  52. })
  53. } else {
  54. //业务菜单角标
  55. store
  56. .dispatch('GetMenuCount')
  57. .then(() => {})
  58. .catch((error) => {
  59. console.log('GetMenuCount出错:' + (error.msg || ''))
  60. })
  61. next()
  62. }
  63. }
  64. } else {
  65. // 没有token
  66. if (isWhiteList(to.path)) {
  67. // 在免登录白名单,直接进入
  68. next()
  69. } else {
  70. next(`/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页
  71. NProgress.done()
  72. }
  73. }
  74. })
  75. router.afterEach((to) => {
  76. if (to.meta.title && to.path != '/login') {
  77. public_saveSimpleLog({
  78. nameEn: getMenuNameEn(to.meta.title),
  79. name: to.meta.title,
  80. jcmc: '进入页面',
  81. jcmcEn: 'Enter Page'
  82. })
  83. }
  84. NProgress.done()
  85. })