index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. } from '@/config/setting';
  14. import store from '@/store';
  15. import { getToken, setToken, getCurrentUser } from '@/utils/token-util';
  16. import { routes, getMenuRoutes } from './routes';
  17. import { changeRole } from '@/api/layout';
  18. Vue.use(VueRouter);
  19. const router = new VueRouter({
  20. // 与 EOM 一致:乾坤 /page-hr/ ,独立 /hr/
  21. base: window.__POWERED_BY_QIANKUN__
  22. ? `/page-${SYSTEM_NAME}/`
  23. : `/${SYSTEM_NAME}/`,
  24. routes,
  25. mode: 'history',
  26. scrollBehavior() {
  27. return { y: 0 };
  28. },
  29. });
  30. export function getAppRouterBase() {
  31. return router.options.base;
  32. }
  33. export function resetRouter() {
  34. const newRouter = new VueRouter({
  35. base: window.__POWERED_BY_QIANKUN__
  36. ? `/page-${SYSTEM_NAME}/`
  37. : `/${SYSTEM_NAME}/`,
  38. routes,
  39. mode: 'history',
  40. scrollBehavior() {
  41. return { y: 0 };
  42. },
  43. });
  44. router.matcher = newRouter.matcher;
  45. router.options.base = newRouter.options.base;
  46. }
  47. /**
  48. * 路由守卫(对齐 EOM)
  49. */
  50. router.beforeEach((to, from, next) => {
  51. if (!from.path.includes(REDIRECT_PATH)) {
  52. NProgress.start();
  53. }
  54. if (getToken()) {
  55. // 还未注册动态路由则先获取
  56. if (!store.state.user.menus) {
  57. store
  58. .dispatch('user/fetchUserInfo')
  59. .then(({ menus, homePath, authoritiesRouter }) => {
  60. if (menus) {
  61. router.addRoute(
  62. getMenuRoutes(
  63. [...menus, ...(authoritiesRouter || [])],
  64. homePath || HOME_PATH,
  65. ),
  66. );
  67. next({ ...to, replace: true });
  68. } else {
  69. // 与 EOM:无菜单时仍放行,避免卡死;侧栏由门户按 /page-hr 维护
  70. next();
  71. }
  72. })
  73. .catch((e) => {
  74. console.error(e);
  75. next();
  76. });
  77. } else {
  78. // 热更新等导致动态路由丢失时补注册
  79. if (routes.length >= router.getRoutes()?.length) {
  80. router.addRoute(
  81. getMenuRoutes(
  82. [
  83. ...store.state.user.menus,
  84. ...(store.state.user.authoritiesRouter || []),
  85. ],
  86. HOME_PATH,
  87. ),
  88. );
  89. next({ ...to });
  90. } else {
  91. next();
  92. }
  93. }
  94. } else if (WHITE_LIST.includes(to.path)) {
  95. next();
  96. } else {
  97. next({
  98. path: '/login',
  99. query: to.path === LAYOUT_PATH ? {} : { from: to.path },
  100. });
  101. }
  102. });
  103. router.afterEach((to) => {
  104. if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
  105. setTimeout(() => {
  106. NProgress.done(true);
  107. }, 200);
  108. }
  109. });
  110. router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
  111. const currentUser = getCurrentUser();
  112. if (menus && menus.length > 0) {
  113. router.addRoute(
  114. getMenuRoutes([...menus, ...(authoritiesRouter || [])], homePath),
  115. );
  116. if (
  117. router.currentRoute.path != (menus[0].redirect || menus[0].path)
  118. ) {
  119. await router.replace({
  120. path: menus[0].redirect || menus[0].path,
  121. });
  122. }
  123. const newToken = await changeRole({
  124. groupId: currentUser.currentGroupId,
  125. roleId: currentUser.currentRoleId,
  126. });
  127. setToken(newToken);
  128. setTimeout(() => {
  129. window.location.reload();
  130. }, 100);
  131. }
  132. };
  133. export function getRouter() {
  134. return router;
  135. }
  136. export default router;