permission.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import user from '@/store/modules/user'
  5. import NProgress from 'nprogress' // progress bar
  6. import 'nprogress/nprogress.css' // progress bar style
  7. import notification from 'ant-design-vue/es/notification'
  8. import {ACCESS_TOKEN, INDEX_MAIN_PAGE_PATH, OAUTH2_LOGIN_PAGE_PATH} from '@/store/mutation-types'
  9. import {generateIndexRouter, isOAuth2AppEnv} from '@/utils/util'
  10. NProgress.configure({showSpinner: false}) // NProgress Configuration
  11. const whiteList = ['/user/login', '/user/register', '/user/register-result', '/user/alteration', '/mainMapback', "/tj/sjtjModule"] // no redirect whitelist
  12. whiteList.push(OAUTH2_LOGIN_PAGE_PATH)
  13. router.beforeEach((to, from, next) => {
  14. NProgress.start() // start progress bar
  15. // alert(to.path)
  16. if (Vue.ls.get(ACCESS_TOKEN)) {
  17. /* has token */
  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. console.log("GetPermissionList", res.result);
  25. const menuData = res.result.menu;
  26. //console.log(res.message)
  27. if (menuData === null || menuData === "" || menuData === undefined) {
  28. return;
  29. }
  30. let constRoutes = [];
  31. constRoutes = generateIndexRouter(menuData);
  32. const auth = res.result.auth;
  33. if (auth.length > 0) {
  34. store.commit('setMenu', auth);
  35. }
  36. // 添加主界面路由
  37. store.dispatch('UpdateAppRouter', {constRoutes}).then(() => {
  38. // 根据roles权限生成可访问的路由表
  39. // 动态添加可访问路由表
  40. router.addRoutes(store.getters.addRouters)
  41. const redirect = decodeURIComponent(from.query.redirect || to.path)
  42. if (to.path === redirect) {
  43. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  44. next({...to, replace: true})
  45. } else {
  46. // 跳转到目的路由
  47. // next({path: redirect})
  48. if (JSON.stringify(constRoutes).includes(redirect)) {
  49. next({
  50. path: redirect
  51. })
  52. } else {
  53. next({
  54. path: '/dashboard/analysis'
  55. })
  56. }
  57. }
  58. })
  59. })
  60. .catch(() => {
  61. /* notification.error({
  62. message: '系统提示',
  63. description: '请求用户信息失败,请重试!'
  64. })*/
  65. store.dispatch('Logout').then(() => {
  66. next({path: '/user/login', query: {redirect: to.fullPath}})
  67. })
  68. })
  69. } else {
  70. next()
  71. }
  72. }
  73. } else {
  74. if (sessionStorage.getItem('loginTicket')) {
  75. user.actions.ValidateLogin(JSON.parse(sessionStorage.getItem('loginTicket')).access_token).then(res=>{
  76. if(res.code=='200'){
  77. next()
  78. }else{
  79. console.log("出错了")
  80. }
  81. }).catch(err=>{
  82. console.log(err)
  83. });
  84. }else{
  85. if (whiteList.indexOf(to.path) !== -1) {
  86. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  87. if (to.path === '/user/login' && isOAuth2AppEnv()) {
  88. next({path: OAUTH2_LOGIN_PAGE_PATH})
  89. } else {
  90. // 在免登录白名单,直接进入
  91. next()
  92. }
  93. NProgress.done()
  94. } else {
  95. // alert(whiteList)
  96. // alert(to.path)
  97. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  98. let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
  99. next({path: path, query: {redirect: to.fullPath}})
  100. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  101. }
  102. }
  103. }
  104. })
  105. router.afterEach(() => {
  106. NProgress.done() // finish progress bar
  107. })