| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /**
- * 路由配置
- */
- import Vue from 'vue';
- import NProgress from 'nprogress';
- import VueRouter from 'vue-router';
- import { WHITE_LIST, REDIRECT_PATH, LAYOUT_PATH } from '@/config/setting';
- import store from '@/store';
- import { getToken } from '@/utils/token-util';
- import { routes, getMenuRoutes } from './routes';
- import { SYSTEM_NAME } from '@/config/setting';
- import { getLoginUser } from '@/api/login';
- Vue.use(VueRouter);
- // const originalPush = VueRouter.prototype.push
- // VueRouter.prototype.push = function push (location) {
- // return originalPush.call(this, location).catch(err => err)
- // }
- const router = new VueRouter({
- base: window.__POWERED_BY_QIANKUN__
- ? `/page-${SYSTEM_NAME}/`
- : `/${SYSTEM_NAME}/`,
- routes,
- mode: 'history',
- scrollBehavior() {
- return { y: 0 };
- }
- });
- /**
- * 路由守卫
- */
- router.beforeEach((to, from, next) => {
- if (!from.path.includes(REDIRECT_PATH)) {
- NProgress.start();
- }
- // 判断是否登录
- if (getToken()) {
- if (!store.state.user.info?.userId) {
- getLoginUser().then((res) => {
- store.commit('user/setUserInfo', res);
- });
- }
- // 还未注册动态路由则先获取
- if (!store.state.user.menus) {
- store
- .dispatch('user/fetchUserInfo')
- .then(({ menus, homePath, authoritiesRouter }) => {
- if (menus) {
- router.addRoute(
- getMenuRoutes([...menus, ...authoritiesRouter], homePath)
- );
- next({ ...to, replace: true });
- }
- })
- .catch((e) => {
- console.error(e);
- next();
- });
- } else {
- // console.log(router.getRoutes(), 'router', routes);
- if (routes.length >= router.getRoutes()?.length) {
- router.addRoute(
- getMenuRoutes([
- ...store.state.user.menus,
- ...store.state.user.authoritiesRouter,
- to.fullPath
- ])
- );
- next({ ...to });
- } else {
- next();
- }
- }
- } else if (WHITE_LIST.includes(to.path)) {
- next();
- } else {
- next({
- path: '/login',
- query: to.path === LAYOUT_PATH ? {} : { from: to.path }
- });
- }
- });
- router.afterEach((to) => {
- if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
- setTimeout(() => {
- NProgress.done(true);
- }, 200);
- }
- });
- export default router;
|