You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.2 KiB

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