permission.js 2.9 KB

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