permission.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. console.log('wyh222')
  50. next({ path: '/firstIndex' })
  51. }
  52. })
  53. })
  54. .catch(() => {
  55. /* notification.error({
  56. message: '系统提示',
  57. description: '请求用户信息失败,请重试!'
  58. })*/
  59. store.dispatch('Logout').then(() => {
  60. next({ path: '/user/login', query: { redirect: to.fullPath } })
  61. })
  62. })
  63. } else {
  64. next()
  65. }
  66. }
  67. } else {
  68. if (whiteList.indexOf(to.path) !== -1) {
  69. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  70. if (to.path === '/user/login' && isOAuth2AppEnv()) {
  71. next({path: OAUTH2_LOGIN_PAGE_PATH})
  72. } else {
  73. // 在免登录白名单,直接进入
  74. next()
  75. }
  76. NProgress.done()
  77. } else {
  78. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  79. let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
  80. next({ path: path, query: { redirect: to.fullPath } })
  81. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  82. }
  83. }
  84. })
  85. router.afterEach(() => {
  86. NProgress.done() // finish progress bar
  87. })