permission.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. //update-end---author:scott ---date::2022-10-13 for:[jeecg-boot/issues/4091]多级路由缓存问题 #4091--------------
  21. NProgress.start() // start progress bar
  22. if (Vue.ls.get(ACCESS_TOKEN)) {
  23. /* has token */
  24. if (to.path === '/user/login' || to.path === OAUTH2_LOGIN_PAGE_PATH) {
  25. next({ path: INDEX_MAIN_PAGE_PATH })
  26. NProgress.done()
  27. } else {
  28. if (store.getters.permissionList.length === 0) {
  29. store.dispatch('GetPermissionList').then(res => {
  30. const menuData = res.result.menu;
  31. //console.log(res.message)
  32. if (menuData === null || menuData === "" || menuData === undefined) {
  33. return;
  34. }
  35. let constRoutes = [];
  36. constRoutes = generateIndexRouter(menuData);
  37. // 添加主界面路由
  38. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  39. // 根据roles权限生成可访问的路由表
  40. // 动态添加可访问路由表
  41. router.addRoutes(store.getters.addRouters)
  42. const redirect = decodeURIComponent(from.query.redirect || to.path)
  43. if (to.path === redirect) {
  44. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  45. next({ ...to, replace: true })
  46. } else {
  47. // 跳转到目的路由
  48. next({ path: redirect })
  49. }
  50. })
  51. })
  52. .catch(() => {
  53. /* notification.error({
  54. message: '系统提示',
  55. description: '请求用户信息失败,请重试!'
  56. })*/
  57. store.dispatch('Logout').then(() => {
  58. next({ path: '/user/login', query: { redirect: to.fullPath } })
  59. })
  60. })
  61. } else {
  62. next()
  63. }
  64. }
  65. } else {
  66. if (whiteList.indexOf(to.path) !== -1) {
  67. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  68. if (to.path === '/user/login' && isOAuth2AppEnv()) {
  69. next({path: OAUTH2_LOGIN_PAGE_PATH})
  70. } else {
  71. // 在免登录白名单,直接进入
  72. next()
  73. }
  74. NProgress.done()
  75. } else {
  76. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  77. let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
  78. next({ path: path, query: { redirect: to.fullPath } })
  79. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  80. }
  81. }
  82. })
  83. router.afterEach(() => {
  84. NProgress.done() // finish progress bar
  85. })