index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, getCurrentUser } 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, 999);
  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: {
  57. title: '流程定义',
  58. hide: true,
  59. routePath: '/bpm/definition'
  60. }
  61. },
  62. {
  63. path: '/bpm/modelEditor',
  64. component: 'bpm/model/modelEditor',
  65. name: '设计流程',
  66. meta: {
  67. title: '设计流程',
  68. hide: true,
  69. routePath: '/bpm/modelEditor'
  70. }
  71. },
  72. {
  73. path: '/bpm/processInstance',
  74. component: 'bpm/processInstance/detail',
  75. name: '流程详情',
  76. meta: {
  77. title: '流程详情',
  78. hide: true,
  79. routePath: '/bpm/processInstance'
  80. }
  81. }
  82. );
  83. }
  84. });
  85. if (menus) {
  86. router.addRoute(
  87. getMenuRoutes([...menus, ...authoritiesRouter], homePath)
  88. );
  89. next({ ...to, replace: true });
  90. }
  91. console.log(router);
  92. })
  93. .catch((e) => {
  94. console.error(e);
  95. next();
  96. });
  97. } else {
  98. if (routes.length >= router.getRoutes()?.length) {
  99. router.addRoute(
  100. getMenuRoutes([
  101. ...store.state.user.menus,
  102. ...store.state.user.authoritiesRouter,
  103. to.fullPath
  104. ])
  105. );
  106. next({ ...to });
  107. } else {
  108. next();
  109. }
  110. }
  111. } else if (WHITE_LIST.includes(to.path)) {
  112. next();
  113. } else {
  114. next({
  115. path: '/login',
  116. query: to.path === LAYOUT_PATH ? {} : { from: to.path }
  117. });
  118. }
  119. });
  120. router.afterEach((to) => {
  121. if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
  122. setTimeout(() => {
  123. NProgress.done(true);
  124. }, 200);
  125. }
  126. });
  127. router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
  128. const currentUser = getCurrentUser()
  129. if (menus && menus.length > 0) {
  130. router.addRoute(getMenuRoutes([...menus, ...authoritiesRouter], homePath));
  131. if (router.currentRoute.path != (menus[0].redirect || menus[0].path)) {
  132. await router.replace({
  133. path: menus[0].redirect || menus[0].path
  134. });
  135. }
  136. const newToken = await changeRole({
  137. groupId: currentUser.currentGroupId,
  138. roleId: currentUser.currentRoleId
  139. });
  140. setToken(newToken);
  141. setTimeout(() => {
  142. window.location.reload();
  143. }, 100);
  144. }
  145. // next({ ...to, replace: true });
  146. };
  147. export default router;