index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * 路由配置(对齐 EOM)
  3. */
  4. import Vue from 'vue';
  5. import NProgress from 'nprogress';
  6. import VueRouter from 'vue-router';
  7. import {
  8. WHITE_LIST,
  9. REDIRECT_PATH,
  10. LAYOUT_PATH,
  11. SYSTEM_NAME,
  12. HOME_PATH,
  13. isHomeAliasPath,
  14. } from '@/config/setting';
  15. import store from '@/store';
  16. import { getToken, setToken, getCurrentUser } from '@/utils/token-util';
  17. import { routes, getMenuRoutes } from './routes';
  18. import { changeRole } from '@/api/layout';
  19. import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
  20. Vue.use(VueRouter);
  21. /**
  22. * 乾坤:/page-hr/(同 EOM 的 /page-eos/)
  23. * 独立:/hr/(同 EOM 的 /eos/)
  24. */
  25. export function getAppRouterBase() {
  26. return window.__POWERED_BY_QIANKUN__
  27. ? `/page-${SYSTEM_NAME}/`
  28. : `/${SYSTEM_NAME}/`;
  29. }
  30. function createRouter() {
  31. return new VueRouter({
  32. base: getAppRouterBase(),
  33. routes,
  34. mode: 'history',
  35. scrollBehavior() {
  36. return { y: 0 };
  37. },
  38. });
  39. }
  40. const router = createRouter();
  41. let dynamicRoutesReady = false;
  42. export function resetRouter() {
  43. const newRouter = createRouter();
  44. router.matcher = newRouter.matcher;
  45. router.options.base = newRouter.options.base;
  46. dynamicRoutesReady = false;
  47. }
  48. function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
  49. const nextMenus = rewriteLegacyHrMenus(menus || []);
  50. const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
  51. store.commit('user/setMenus', nextMenus);
  52. store.commit('user/setAuthoritiesRouter', nextAuth);
  53. if (nextMenus.length || nextAuth.length) {
  54. router.addRoute(
  55. getMenuRoutes(
  56. [...nextMenus, ...nextAuth],
  57. homePath || HOME_PATH || '/home/index',
  58. ),
  59. );
  60. }
  61. dynamicRoutesReady = true;
  62. }
  63. export function setupDynamicRoutes(menus, homePath, authoritiesRouter = []) {
  64. resetRouter();
  65. registerMenuRoutes(menus, homePath, authoritiesRouter);
  66. }
  67. router.beforeEach((to, from, next) => {
  68. if (!from.path.includes(REDIRECT_PATH)) {
  69. NProgress.start();
  70. }
  71. if (getToken()) {
  72. const homePath = HOME_PATH || '/home/index';
  73. if (isHomeAliasPath(to.path) && to.path !== homePath) {
  74. next({
  75. path: homePath,
  76. query: to.query,
  77. hash: to.hash,
  78. replace: true,
  79. });
  80. return;
  81. }
  82. // 与 EOM:menus 未加载时拉取;有菜单才 addRoute
  83. if (store.state.user.menus == null) {
  84. store
  85. .dispatch('user/fetchUserInfo')
  86. .then(({ menus, homePath: menuHome, authoritiesRouter }) => {
  87. if (menus?.length || authoritiesRouter?.length) {
  88. registerMenuRoutes(
  89. menus || [],
  90. menuHome || homePath,
  91. authoritiesRouter || [],
  92. );
  93. } else {
  94. // 标记已尝试,避免死循环;乾坤下侧栏仍由门户管
  95. store.commit('user/setMenus', menus || []);
  96. dynamicRoutesReady = true;
  97. }
  98. next({ ...to, replace: true });
  99. })
  100. .catch((e) => {
  101. console.error(e);
  102. next();
  103. });
  104. return;
  105. }
  106. if (!dynamicRoutesReady && store.state.user.menus?.length) {
  107. registerMenuRoutes(
  108. store.state.user.menus,
  109. homePath,
  110. store.state.user.authoritiesRouter || [],
  111. );
  112. next({ ...to, replace: true });
  113. return;
  114. }
  115. next();
  116. return;
  117. }
  118. if (WHITE_LIST.includes(to.path)) {
  119. next();
  120. return;
  121. }
  122. next({
  123. path: '/login',
  124. query: to.path === LAYOUT_PATH ? {} : { from: to.path },
  125. });
  126. });
  127. router.afterEach((to) => {
  128. if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
  129. setTimeout(() => {
  130. NProgress.done(true);
  131. }, 200);
  132. }
  133. });
  134. router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
  135. const currentUser = getCurrentUser();
  136. if (menus?.length) {
  137. resetRouter();
  138. registerMenuRoutes(menus, homePath, authoritiesRouter);
  139. const target = HOME_PATH || menus[0].redirect || menus[0].path;
  140. if (router.currentRoute.path !== target) {
  141. await router.replace({ path: target });
  142. }
  143. const newToken = await changeRole({
  144. groupId: currentUser.currentGroupId,
  145. roleId: currentUser.currentRoleId,
  146. });
  147. setToken(newToken);
  148. setTimeout(() => {
  149. window.location.reload();
  150. }, 100);
  151. }
  152. };
  153. export function getRouter() {
  154. return router;
  155. }
  156. export default router;