index.js 2.8 KB

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