permission.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import store from '@/store'
  2. import router from '@/router'
  3. import { Message, MessageBox } from 'element-ui'
  4. import cookie from '@/utils/cookie'
  5. import NProgress from 'nprogress';//加载进度条
  6. import 'nprogress/nprogress.css';//加载进度条
  7. // const TenantId = '867735392558919680'
  8. NProgress.configure({
  9. showSpinner: false
  10. });
  11. NProgress.inc(0.2);
  12. router.beforeEach((to, from, next) => {
  13. //保险起见 路由跳转时检查token是否存在
  14. // if (router.history.pending.query.TenantId !== undefined) {
  15. // sessionStorage.TenantId = router.history.pending.query.TenantId
  16. // }
  17. // if(sessionStorage.TenantId == 'undefined'){
  18. // sessionStorage.removeItem("TenantId");
  19. // }
  20. const getToken = cookie.getCookie('token')
  21. // 加载进度条start
  22. NProgress.start();
  23. if (getToken) {
  24. if (to.path === '/login') {
  25. next({ path: '/' })//重定向回主页
  26. // 加载进度条end
  27. } else {
  28. if (store.getters.userInfo.length === 0) {
  29. store.dispatch('GetUserInfo').then(() => {
  30. store.dispatch('GenerateRoutes').then(res => {
  31. router.addRoutes(res)
  32. next({ ...to,replace: true })
  33. })
  34. }).catch(err => {
  35. store.dispatch('outLogin').then(() => {
  36. Message.error(err)
  37. next({ path: '/' })
  38. })
  39. })
  40. } else {
  41. if (to.matched.length === 0) {
  42. Message.error('未找到该页面')
  43. next({ path: '/404', replace: true })
  44. } else {
  45. store.state.navActive = to.path//导航栏状态
  46. store.commit('uploadTabberLists', to)
  47. if(!to.query.TenantId){
  48. to.query.TenantId = sessionStorage.TenantId
  49. next(to)
  50. }else{
  51. next()
  52. }
  53. }
  54. }
  55. }
  56. } else {
  57. if (to.path === '/login') {
  58. // if(!to.query.TenantId){
  59. // sessionStorage.TenantId = TenantId;
  60. // to.query.TenantId = TenantId
  61. // next(to)
  62. // }else{
  63. next()
  64. // }
  65. // 加载进度条end
  66. } else {
  67. let TenantId = 0
  68. if(router.history.current){
  69. TenantId = router.history.current.query.TenantId
  70. }
  71. localStorage.clear()
  72. store.state.tabberLists = []
  73. store.state.routes = []
  74. store.state.allRoutes = []
  75. next({path:'/login',query:{
  76. TenantId: TenantId
  77. }})
  78. Message({
  79. type: 'info',
  80. message: '登录已过期,请重新登录'
  81. });
  82. // 加载进度条end
  83. }
  84. }
  85. })
  86. router.afterEach((to, from) => {
  87. NProgress.done();
  88. })