index.js 3.0 KB

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