permission.js 3.9 KB

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