permission.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 } from '@/store/mutation-types'
  8. import { generateIndexRouter } from "@/utils/util"
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['/user/login', '/user/register', '/user/register-result','/user/alteration','/activiti/commencementSchedule','/automaticLogin','/redirect/login','/redirect/login/try','/index'] // no redirect whitelist
  11. router.beforeEach((to, from, next) => {
  12. NProgress.start() // start progress bar
  13. if (Vue.ls.get(ACCESS_TOKEN)) {
  14. /* has token */
  15. if (to.path === '/user/login') {
  16. next({ path: '/dashboard/workplace' })
  17. NProgress.done()
  18. } else {
  19. if (store.getters.permissionList.length === 0) {
  20. store.dispatch('GetPermissionList').then(res => {
  21. const menuData = res.result.menu;
  22. console.log(res.message)
  23. if (menuData === null || menuData === "" || menuData === undefined) {
  24. return;
  25. }
  26. let constRoutes = [];
  27. constRoutes = generateIndexRouter(menuData);
  28. // 添加主界面路由
  29. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  30. // 根据roles权限生成可访问的路由表
  31. // 动态添加可访问路由表
  32. router.addRoutes(store.getters.addRouters)
  33. const redirect = decodeURIComponent(from.query.redirect || to.path)
  34. if (to.path === redirect) {
  35. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  36. next({ ...to, replace: true })
  37. } else {
  38. // 跳转到目的路由
  39. next({ path: redirect })
  40. }
  41. })
  42. })
  43. .catch(() => {
  44. /* notification.error({
  45. message: '系统提示',
  46. description: '请求用户信息失败,请重试!'
  47. })*/
  48. store.dispatch('Logout').then(() => {
  49. next({ path: '/user/login', query: { redirect: to.fullPath } })
  50. })
  51. })
  52. } else {
  53. next()
  54. }
  55. }
  56. } else {
  57. if (window.location.href.indexOf("/activiti/check/")>-1){
  58. next()
  59. }else{
  60. if (whiteList.indexOf(to.path) !== -1) {
  61. // 在免登录白名单,直接进入
  62. next()
  63. } else {
  64. next({ path: '/user/login', query: { redirect: to.fullPath } })
  65. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  66. }
  67. }
  68. }
  69. })
  70. router.afterEach(() => {
  71. NProgress.done() // finish progress bar
  72. })