index.js 3.1 KB

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