index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * 路由配置
  3. */
  4. import Vue from 'vue';
  5. import NProgress from 'nprogress';
  6. import VueRouter from 'vue-router';
  7. import { WHITE_LIST, REDIRECT_PATH, LAYOUT_PATH } from '@/config/setting';
  8. import store from '@/store';
  9. import { getToken, setToken } from '@/utils/token-util';
  10. import { routes, getMenuRoutes } from './routes';
  11. import { SYSTEM_NAME } from '@/config/setting';
  12. import { getLoginUser } from '@/api/login';
  13. import { changeRole } from '@/api/layout/index';
  14. Vue.use(VueRouter);
  15. const router = new VueRouter({
  16. base: window.__POWERED_BY_QIANKUN__
  17. ? `/page-${SYSTEM_NAME}/`
  18. : `/${SYSTEM_NAME}/`,
  19. routes,
  20. mode: 'history',
  21. scrollBehavior() {
  22. return { y: 0 };
  23. }
  24. });
  25. /**
  26. * 路由守卫
  27. */
  28. router.beforeEach((to, from, next) => {
  29. console.log(store.state.user);
  30. console.log(to);
  31. console.log(from);
  32. if (!from.path.includes(REDIRECT_PATH)) {
  33. NProgress.start();
  34. }
  35. // 判断是否登录
  36. if (getToken()) {
  37. if (!store.state.user.info?.userId) {
  38. getLoginUser().then((res) => {
  39. store.commit('user/setUserInfo', res);
  40. });
  41. }
  42. console.log(!store.state.user.menus);
  43. // 还未注册动态路由则先获取
  44. if (!store.state.user.menus) {
  45. store
  46. .dispatch('user/fetchUserInfo')
  47. .then(({ menus, homePath, authoritiesRouter }) => {
  48. menus.forEach((item) => {
  49. //匹配第一层路由名称
  50. if (item.name == '配置管理') {
  51. item.children.push(
  52. {
  53. path: '/bpm/definition',
  54. component: 'bpm/definition',
  55. name: '流程定义',
  56. meta: { title: '流程定义', hide: true, routePath: '/bpm/definition' }
  57. },
  58. {
  59. path: '/bpm/modelEditor',
  60. component: 'bpm/model/modelEditor',
  61. name: '设计流程',
  62. meta: { title: '设计流程', hide: true, routePath: '/bpm/modelEditor' }
  63. },
  64. {
  65. path: '/bpm/processInstance',
  66. component: 'bpm/processInstance/detail',
  67. name: '流程详情',
  68. meta: { title: '流程详情', hide: true, routePath: '/bpm/processInstance' }
  69. }
  70. );
  71. }
  72. });
  73. if (menus) {
  74. router.addRoute(
  75. getMenuRoutes([...menus, ...authoritiesRouter], homePath)
  76. );
  77. next({ ...to, replace: true });
  78. }
  79. console.log(router);
  80. })
  81. .catch((e) => {
  82. console.error(e);
  83. next();
  84. });
  85. } else {
  86. // console.log(router.getRoutes(), 'router', routes);
  87. if (routes.length >= router.getRoutes()?.length) {
  88. router.addRoute(
  89. getMenuRoutes([
  90. ...store.state.user.menus,
  91. ...store.state.user.authoritiesRouter,
  92. to.fullPath
  93. ])
  94. );
  95. next({ ...to });
  96. } else {
  97. next();
  98. }
  99. }
  100. } else if (WHITE_LIST.includes(to.path)) {
  101. next();
  102. } else {
  103. next({
  104. path: '/login',
  105. query: to.path === LAYOUT_PATH ? {} : { from: to.path }
  106. });
  107. }
  108. });
  109. router.afterEach((to) => {
  110. if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
  111. setTimeout(() => {
  112. NProgress.done(true);
  113. }, 200);
  114. }
  115. });
  116. router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
  117. const currentUser = JSON.parse(sessionStorage['currentUser']);
  118. if (menus && menus.length > 0) {
  119. router.addRoute(
  120. getMenuRoutes([...menus, ...authoritiesRouter], homePath)
  121. );
  122. if (router.currentRoute.path != (menus[0].redirect || menus[0].path)) {
  123. await router.replace({
  124. path: menus[0].redirect || menus[0].path,
  125. })
  126. }
  127. const newToken = await changeRole({ groupId: currentUser.currentGroupId, roleId: currentUser.currentRoleId })
  128. setToken(newToken)
  129. setTimeout(() => {
  130. window.location.reload()
  131. }, 100);
  132. }
  133. // next({ ...to, replace: true });
  134. };
  135. export default router;