移动端
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.
 
 

110 line
2.9 KiB

  1. import router from './router'
  2. import store from './store'
  3. import NProgress from 'nprogress'
  4. import 'nprogress/nprogress.css'
  5. import { getToken } from '@/utils/auth'
  6. NProgress.configure({ showSpinner: false })
  7. const whiteList = [
  8. '/index',
  9. '/login',
  10. '/auth-redirect',
  11. '/bind',
  12. '/register',
  13. '/news/index',
  14. '/news/newDetail',
  15. '/notice/index',
  16. '/supply/index',
  17. '/interaction/index',
  18. '/register/index',
  19. '/register/registerType',
  20. '/register/userRegister',
  21. '/register/companyRegister',
  22. '/biddingHall',
  23. '/authenticRight',
  24. '/authenticRight/login',
  25. '/project',
  26. '/policy',
  27. '/policyDetail',
  28. '/attestationDetail',
  29. '/noticeDetail',
  30. '/homestead/login',
  31. '/applicationForm',
  32. '/applicationList',
  33. '/certificateList',
  34. '/certificateSee',
  35. '/informationList',
  36. '/circulationList',
  37. '/circulationAdd',
  38. '/onlineHomeLogin',
  39. ]
  40. router.beforeEach((to, from, next) => {
  41. NProgress.start()
  42. if (getToken()) {
  43. /* has token*/
  44. if (to.path === '/login') {
  45. next({ path: '/' })
  46. NProgress.done()
  47. } else {
  48. if (store.getters.roles.length === 0) {
  49. next()
  50. // 判断当前用户是否已拉取完user_info信息
  51. store.dispatch('GetInfo').then(res => {
  52. // 拉取user_info
  53. const roles = res.roles
  54. // store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
  55. // // 根据roles权限生成可访问的路由表
  56. // router.addRoutes(accessRoutes) // 动态添加可访问路由表
  57. // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  58. // })
  59. next()
  60. }).catch(err => {
  61. store.dispatch('LogOut').then(() => {
  62. console.log(to.path)
  63. // Message.error(err)
  64. if ('/authenticRight/index'.indexOf(to.path) !== -1) {
  65. next({ path: '/authenticRight/login' })
  66. } else if ('/homestead/index'.indexOf(to.path) !== -1) {
  67. next({ path: '/homestead/login' })
  68. } else if (to.path.indexOf('/onlineHome') !== -1) {
  69. next(`/onlineHomeLogin`)
  70. } else {
  71. next({ path: '/' })
  72. }
  73. })
  74. })
  75. } else {
  76. next()
  77. }
  78. }
  79. } else {
  80. console.log(to.path)
  81. // 没有token
  82. if (whiteList.indexOf(to.path) !== -1) {
  83. // 在免登录白名单,直接进入
  84. next()
  85. } else {
  86. if ('/authenticRight/index'.indexOf(to.path) !== -1) {
  87. next(`/authenticRight/login?redirect=${to.fullPath}`)
  88. } else if ('/homestead/index'.indexOf(to.path) !== -1) {
  89. next(`/homestead/login?redirect=${to.fullPath}`)
  90. } else if (to.path.indexOf('/onlineHome') !== -1) {
  91. next(`/onlineHomeLogin`)
  92. } else {
  93. next(`/login?redirect=${to.fullPath}`)
  94. }
  95. // // 否则全部重定向到登录页
  96. //next('/index');
  97. NProgress.done()
  98. }
  99. }
  100. })
  101. router.afterEach(() => {
  102. NProgress.done()
  103. })