index.js 4.1 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. // 还未注册动态路由则先获取
  43. if (!store.state.user.menus) {
  44. store
  45. .dispatch('user/fetchUserInfo')
  46. .then(({ menus, homePath, authoritiesRouter }) => {
  47. menus.forEach((item) => {
  48. //匹配第一层路由名称
  49. if (item.name == '配置管理') {
  50. item.children.push(
  51. {
  52. path: '/bpm/definition',
  53. component: 'bpm/definition',
  54. name: '流程定义',
  55. meta: {
  56. title: '流程定义',
  57. hide: true,
  58. routePath: '/bpm/definition'
  59. }
  60. },
  61. {
  62. path: '/bpm/modelEditor',
  63. component: 'bpm/model/modelEditor',
  64. name: '设计流程',
  65. meta: {
  66. title: '设计流程',
  67. hide: true,
  68. routePath: '/bpm/modelEditor'
  69. }
  70. },
  71. {
  72. path: '/bpm/processInstance',
  73. component: 'bpm/processInstance/detail',
  74. name: '流程详情',
  75. meta: {
  76. title: '流程详情',
  77. hide: true,
  78. routePath: '/bpm/processInstance'
  79. }
  80. }
  81. );
  82. }
  83. });
  84. if (menus) {
  85. router.addRoute(
  86. getMenuRoutes([...menus, ...authoritiesRouter], homePath)
  87. );
  88. next({ ...to, replace: true });
  89. }
  90. console.log(router);
  91. })
  92. .catch((e) => {
  93. console.error(e);
  94. next();
  95. });
  96. } else {
  97. if (routes.length >= router.getRoutes()?.length) {
  98. router.addRoute(
  99. getMenuRoutes([
  100. ...store.state.user.menus,
  101. ...store.state.user.authoritiesRouter,
  102. to.fullPath
  103. ])
  104. );
  105. next({ ...to });
  106. } else {
  107. next();
  108. }
  109. }
  110. } else if (WHITE_LIST.includes(to.path)) {
  111. next();
  112. } else {
  113. next({
  114. path: '/login',
  115. query: to.path === LAYOUT_PATH ? {} : { from: to.path }
  116. });
  117. }
  118. });
  119. router.afterEach((to) => {
  120. if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
  121. setTimeout(() => {
  122. NProgress.done(true);
  123. }, 200);
  124. }
  125. });
  126. router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
  127. const currentUser = getCurrentUser()
  128. if (menus && menus.length > 0) {
  129. router.addRoute(getMenuRoutes([...menus, ...authoritiesRouter], homePath));
  130. if (router.currentRoute.path != (menus[0].redirect || menus[0].path)) {
  131. await router.replace({
  132. path: menus[0].redirect || menus[0].path
  133. });
  134. }
  135. const newToken = await changeRole({
  136. groupId: currentUser.currentGroupId,
  137. roleId: currentUser.currentRoleId
  138. });
  139. setToken(newToken);
  140. setTimeout(() => {
  141. window.location.reload();
  142. }, 100);
  143. }
  144. // next({ ...to, replace: true });
  145. };
  146. export default router;