permission.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import notification from 'ant-design-vue/es/notification'
  7. import { ACCESS_TOKEN,INDEX_MAIN_PAGE_PATH, OAUTH2_LOGIN_PAGE_PATH } from '@/store/mutation-types'
  8. import { generateIndexRouter, isOAuth2AppEnv } from '@/utils/util'
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['/user/login', '/user/register', '/user/register-result','/user/alteration'] // no redirect whitelist
  11. whiteList.push(OAUTH2_LOGIN_PAGE_PATH)
  12. router.beforeEach((to, from, next) => {
  13. //update-begin---author:scott ---date:2022-10-13 for:[jeecg-boot/issues/4091]多级路由缓存问题 #4091-----------
  14. //解决三级菜单无法缓存问题
  15. //参考: https://blog.csdn.net/qq_37322135/article/details/126013301
  16. //参考: https://blog.csdn.net/cwin8951/article/details/106644118
  17. // if (to.matched && to.matched.length>3) {
  18. // to.matched.splice(2, to.matched.length - 3)
  19. // }
  20. // 修改框架:点击三级菜单的不自动折叠
  21. if (to.matched && to.matched.length>4) {
  22. to.matched.splice(3, to.matched.length - 4)
  23. }
  24. //update-end---author:scott ---date::2022-10-13 for:[jeecg-boot/issues/4091]多级路由缓存问题 #4091--------------
  25. NProgress.start() // start progress bar
  26. if (Vue.ls.get(ACCESS_TOKEN)) {
  27. /* has token */
  28. if (to.path === '/user/login' || to.path === OAUTH2_LOGIN_PAGE_PATH) {
  29. next({ path: INDEX_MAIN_PAGE_PATH })
  30. NProgress.done()
  31. } else {
  32. if (store.getters.permissionList.length === 0) {
  33. store.dispatch('GetPermissionList').then(res => {
  34. const menuData = res.result.menu;
  35. //console.log(res.message)
  36. if (menuData === null || menuData === "" || menuData === undefined) {
  37. return;
  38. }
  39. let constRoutes = [];
  40. constRoutes = generateIndexRouter(menuData);
  41. // 添加主界面路由
  42. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  43. // 根据roles权限生成可访问的路由表
  44. // 动态添加可访问路由表
  45. router.addRoutes(store.getters.addRouters)
  46. const redirect = decodeURIComponent(from.query.redirect || to.path)
  47. if (to.path === redirect) {
  48. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  49. next({ ...to, replace: true })
  50. } else {
  51. // 跳转到目的路由
  52. next({ path: redirect })
  53. }
  54. })
  55. })
  56. .catch(() => {
  57. /* notification.error({
  58. message: '系统提示',
  59. description: '请求用户信息失败,请重试!'
  60. })*/
  61. store.dispatch('Logout').then(() => {
  62. next({ path: '/user/login', query: { redirect: to.fullPath } })
  63. })
  64. })
  65. } else {
  66. next()
  67. }
  68. }
  69. } else {
  70. if (whiteList.indexOf(to.path) !== -1) {
  71. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  72. if (to.path === '/user/login' && isOAuth2AppEnv()) {
  73. next({path: OAUTH2_LOGIN_PAGE_PATH})
  74. } else {
  75. // 在免登录白名单,直接进入
  76. next()
  77. }
  78. NProgress.done()
  79. } else {
  80. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  81. let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
  82. next({ path: path, query: { redirect: to.fullPath } })
  83. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  84. }
  85. }
  86. })
  87. router.afterEach(() => {
  88. NProgress.done() // finish progress bar
  89. })