permission.js 3.0 KB

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