permission.js 3.5 KB

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