| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /**
- * 路由配置(对齐 EOM)
- */
- import Vue from 'vue';
- import NProgress from 'nprogress';
- import VueRouter from 'vue-router';
- import {
- WHITE_LIST,
- REDIRECT_PATH,
- LAYOUT_PATH,
- SYSTEM_NAME,
- HOME_PATH,
- isHomeAliasPath,
- } from '@/config/setting';
- import store from '@/store';
- import { getToken, setToken, getCurrentUser } from '@/utils/token-util';
- import { routes, getMenuRoutes } from './routes';
- import { changeRole } from '@/api/layout';
- import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
- Vue.use(VueRouter);
- /**
- * 乾坤:/page-hr/(同 EOM 的 /page-eos/)
- * 独立:/hr/(同 EOM 的 /eos/)
- */
- export function getAppRouterBase() {
- return window.__POWERED_BY_QIANKUN__
- ? `/page-${SYSTEM_NAME}/`
- : `/${SYSTEM_NAME}/`;
- }
- function createRouter() {
- return new VueRouter({
- base: getAppRouterBase(),
- routes,
- mode: 'history',
- scrollBehavior() {
- return { y: 0 };
- },
- });
- }
- const router = createRouter();
- let dynamicRoutesReady = false;
- export function resetRouter() {
- const newRouter = createRouter();
- router.matcher = newRouter.matcher;
- router.options.base = newRouter.options.base;
- dynamicRoutesReady = false;
- }
- function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
- const nextMenus = rewriteLegacyHrMenus(menus || []);
- const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
- store.commit('user/setMenus', nextMenus);
- store.commit('user/setAuthoritiesRouter', nextAuth);
- if (nextMenus.length || nextAuth.length) {
- router.addRoute(
- getMenuRoutes(
- [...nextMenus, ...nextAuth],
- homePath || HOME_PATH || '/home/index',
- ),
- );
- }
- dynamicRoutesReady = true;
- }
- export function setupDynamicRoutes(menus, homePath, authoritiesRouter = []) {
- resetRouter();
- registerMenuRoutes(menus, homePath, authoritiesRouter);
- }
- router.beforeEach((to, from, next) => {
- if (!from.path.includes(REDIRECT_PATH)) {
- NProgress.start();
- }
- if (getToken()) {
- const homePath = HOME_PATH || '/home/index';
- if (isHomeAliasPath(to.path) && to.path !== homePath) {
- next({
- path: homePath,
- query: to.query,
- hash: to.hash,
- replace: true,
- });
- return;
- }
- // 与 EOM:menus 未加载时拉取;有菜单才 addRoute
- if (store.state.user.menus == null) {
- store
- .dispatch('user/fetchUserInfo')
- .then(({ menus, homePath: menuHome, authoritiesRouter }) => {
- if (menus?.length || authoritiesRouter?.length) {
- registerMenuRoutes(
- menus || [],
- menuHome || homePath,
- authoritiesRouter || [],
- );
- } else {
- // 标记已尝试,避免死循环;乾坤下侧栏仍由门户管
- store.commit('user/setMenus', menus || []);
- dynamicRoutesReady = true;
- }
- next({ ...to, replace: true });
- })
- .catch((e) => {
- console.error(e);
- next();
- });
- return;
- }
- if (!dynamicRoutesReady && store.state.user.menus?.length) {
- registerMenuRoutes(
- store.state.user.menus,
- homePath,
- store.state.user.authoritiesRouter || [],
- );
- next({ ...to, replace: true });
- return;
- }
- next();
- return;
- }
- if (WHITE_LIST.includes(to.path)) {
- next();
- return;
- }
- 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);
- }
- });
- router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
- const currentUser = getCurrentUser();
- if (menus?.length) {
- resetRouter();
- registerMenuRoutes(menus, homePath, authoritiesRouter);
- const target = HOME_PATH || menus[0].redirect || menus[0].path;
- if (router.currentRoute.path !== target) {
- await router.replace({ path: target });
- }
- const newToken = await changeRole({
- groupId: currentUser.currentGroupId,
- roleId: currentUser.currentRoleId,
- });
- setToken(newToken);
- setTimeout(() => {
- window.location.reload();
- }, 100);
- }
- };
- export function getRouter() {
- return router;
- }
- export default router;
|