permission.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import warterMark from './warterMark'
  10. import { mapGetters, mapMutations, mapActions } from 'vuex'
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. 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','/create-launch'] // no redirect whitelist
  13. router.beforeEach((to, from, next) => {
  14. NProgress.start() // start progress bar
  15. //if (whiteList.indexOf(to.path) !== -1) {
  16. // 在免登录白名单,直接进入
  17. // next()
  18. //}
  19. if (Vue.ls.get(ACCESS_TOKEN)) {
  20. /* has token */
  21. if (to.path === '/user/login') {
  22. warterMark.out()
  23. next({ path: '/dashboard/workplace' })
  24. NProgress.done()
  25. } else {
  26. // warterMark.set(store.getters.userInfo.roleName)
  27. if (store.getters.permissionList.length === 0) {
  28. store.dispatch('GetPermissionList').then(res => {
  29. const menuData = res.result.menu;
  30. localStorage.setItem("roleCode", res.result.roleCode)
  31. if (menuData === null || menuData === "" || menuData === undefined) {
  32. return;
  33. }
  34. let constRoutes = [];
  35. constRoutes = generateIndexRouter(menuData);
  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. }
  49. })
  50. })
  51. .catch(() => {
  52. /* notification.error({
  53. message: '系统提示',
  54. description: '请求用户信息失败,请重试!'
  55. })*/
  56. warterMark.out()
  57. store.dispatch('Logout').then(() => {
  58. next({ path: '/user/login', query: { redirect: to.fullPath } })
  59. })
  60. })
  61. } else {
  62. warterMark.set(store.getters.userInfo.realname)
  63. next()
  64. }
  65. }
  66. } else {
  67. if (whiteList.indexOf(to.path) !== -1) {
  68. // 在免登录白名单,直接进入
  69. // warterMark.set(store.getters.userInfo.roleName)
  70. next()
  71. } else {
  72. warterMark.out()
  73. next({ path: '/user/login', query: { redirect: to.fullPath } })
  74. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  75. }
  76. }
  77. })
  78. router.afterEach(() => {
  79. NProgress.done() // finish progress bar
  80. })