permission.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 { getAccessToken } from '@/utils/auth'
  7. import { isRelogin } from '@/utils/request'
  8. NProgress.configure({ showSpinner: false })
  9. // const menus = [
  10. // {
  11. // "id": 1,
  12. // "parentId": 0,
  13. // "name": "任务管理",
  14. // "path": "/task",
  15. // "component": null,
  16. // "componentName": null,
  17. // "icon": "cascader",
  18. // "visible": true,
  19. // "keepAlive": true,
  20. // "alwaysShow": true,
  21. // "children": [
  22. // {
  23. // "id": 1201,
  24. // "parentId": 1200,
  25. // "name": "我的流程",
  26. // "path": "my",
  27. // "component": "bpm/processInstance/index",
  28. // "componentName": "BpmProcessInstance",
  29. // "icon": "people",
  30. // "visible": true,
  31. // "keepAlive": true,
  32. // "alwaysShow": true,
  33. // "children": null
  34. // },
  35. // {
  36. // "id": 1207,
  37. // "parentId": 1200,
  38. // "name": "待办任务",
  39. // "path": "todo",
  40. // "component": "bpm/task/todo/index",
  41. // "componentName": "BpmTodoTask",
  42. // "icon": "eye-open",
  43. // "visible": true,
  44. // "keepAlive": true,
  45. // "alwaysShow": true,
  46. // "children": null
  47. // },
  48. // {
  49. // "id": 1208,
  50. // "parentId": 1200,
  51. // "name": "已办任务",
  52. // "path": "done",
  53. // "component": "bpm/task/done/index",
  54. // "componentName": "BpmDoneTask",
  55. // "icon": "eye",
  56. // "visible": true,
  57. // "keepAlive": true,
  58. // "alwaysShow": true,
  59. // "children": null
  60. // }
  61. // ]
  62. // }
  63. // ]
  64. // 增加三方登陆 update by 芋艿
  65. const whiteList = ['/login','/register','/password/forget','/password/reset','/social-login', '/auth-redirect', '/bind', '/oauthLogin/gitee']
  66. router.beforeEach((to, from, next) => {
  67. NProgress.start()
  68. if (getAccessToken()) {
  69. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  70. /* has token*/
  71. if (to.path === '/login') {
  72. next({ path: '/' })
  73. NProgress.done()
  74. } else {
  75. if (store.getters.roles.length === 0) {
  76. isRelogin.show = true
  77. // 获取字典数据 add by 芋艿
  78. store.dispatch('dict/loadDictDatas')
  79. // 判断当前用户是否已拉取完 user_info 信息
  80. store.dispatch('GetInfo').then(userInfo => {
  81. console.log('用户信息',userInfo);
  82. isRelogin.show = false
  83. if(localStorage.getItem('new')){
  84. var path = localStorage.getItem('new')
  85. if (path == to.path) { //动态路由页面的刷新事件
  86. //触发 GenerateRoutes 事件时,将 menus 菜单树传递进去
  87. //获取菜单数据
  88. let menus = localStorage.getItem('menus')
  89. store.dispatch('GenerateRoutes', JSON.parse(menus)).then(accessRoutes => {
  90. // 根据 roles 权限生成可访问的路由表
  91. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  92. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  93. })
  94. }
  95. }
  96. next({ ...to, replace: true })
  97. }).catch(err => {
  98. store.dispatch('LogOut').then(() => {
  99. Message.error(err)
  100. next({ path: '/' })
  101. })
  102. })
  103. } else {
  104. next()
  105. }
  106. }
  107. } else {
  108. // 没有token
  109. if (whiteList.indexOf(to.path) !== -1) {
  110. // 在免登录白名单,直接进入
  111. next()
  112. } else {
  113. const redirect = encodeURIComponent(to.fullPath) // 编码 URI,保证参数跳转回去后,可以继续带上
  114. next(`/login?redirect=${redirect}`) // 否则全部重定向到登录页
  115. NProgress.done()
  116. }
  117. }
  118. })
  119. router.afterEach((to, from) => {
  120. localStorage.setItem("new",to.path)
  121. NProgress.done()
  122. })