index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 originalPush = VueRouter.prototype.push
  16. // VueRouter.prototype.push = function push (location) {
  17. // return originalPush.call(this, location).catch(err => err)
  18. // }
  19. const router = new VueRouter({
  20. base: window.__POWERED_BY_QIANKUN__
  21. ? `/page-${SYSTEM_NAME}/`
  22. : `/${SYSTEM_NAME}/`,
  23. routes,
  24. mode: 'history',
  25. scrollBehavior () {
  26. return { y: 0 };
  27. }
  28. });
  29. /**
  30. * 路由守卫
  31. */
  32. router.beforeEach((to, from, next) => {
  33. if (!from.path.includes(REDIRECT_PATH)) {
  34. NProgress.start();
  35. }
  36. // 判断是否登录
  37. if (getToken()) {
  38. if (!store.state.user.info?.userId) {
  39. getLoginUser().then((res) => {
  40. store.commit('user/setUserInfo', res);
  41. });
  42. }
  43. // 还未注册动态路由则先获取
  44. if (!store.state.user.menus) {
  45. store
  46. .dispatch('user/fetchUserInfo')
  47. .then(({ menus, homePath, authoritiesRouter }) => {
  48. if (menus) {
  49. router.addRoute(
  50. getMenuRoutes([...menus, ...authoritiesRouter], homePath)
  51. );
  52. next({ ...to, replace: true });
  53. }
  54. })
  55. .catch((e) => {
  56. console.error(e);
  57. next();
  58. });
  59. } else {
  60. // console.log(router.getRoutes(), 'router', routes);
  61. if (routes.length >= router.getRoutes()?.length) {
  62. router.addRoute(
  63. getMenuRoutes([
  64. ...store.state.user.menus,
  65. ...store.state.user.authoritiesRouter,
  66. to.fullPath
  67. ])
  68. );
  69. next({ ...to });
  70. } else {
  71. next();
  72. }
  73. }
  74. } else if (WHITE_LIST.includes(to.path)) {
  75. next();
  76. } else {
  77. next({
  78. path: '/login',
  79. query: to.path === LAYOUT_PATH ? {} : { from: to.path }
  80. });
  81. }
  82. });
  83. router.afterEach((to) => {
  84. if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
  85. setTimeout(() => {
  86. NProgress.done(true);
  87. }, 200);
  88. }
  89. });
  90. router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
  91. const currentUser = JSON.parse(sessionStorage['currentUser']);
  92. if (menus && menus.length > 0) {
  93. router.addRoute(
  94. getMenuRoutes([...menus, ...authoritiesRouter], homePath)
  95. );
  96. if (router.currentRoute.path != (menus[0].redirect || menus[0].path)) {
  97. await router.replace({
  98. path: menus[0].redirect || menus[0].path,
  99. })
  100. }
  101. const newToken = await changeRole({ groupId: currentUser.currentGroupId, roleId: currentUser.currentRoleId })
  102. setToken(newToken)
  103. setTimeout(() => {
  104. window.location.reload()
  105. }, 100);
  106. }
  107. // next({ ...to, replace: true });
  108. };
  109. export default router;