permission.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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','/user/transfer-local','/errorPage','http://192.168.0.111:8088/jeecg-boot/sys/feishu/url','/user/FStransfer'] // 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. localStorage.setItem("roleCode",res.result.roleCode)
  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. // 在免登录白名单,直接进入
  60. next()
  61. } else {
  62. next({path: '/user/login', query: {redirect: to.fullPath}})
  63. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  64. }
  65. }
  66. })
  67. router.afterEach(() => {
  68. NProgress.done() // finish progress bar
  69. })