|
@@ -20,22 +20,14 @@ import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
|
|
|
|
|
|
|
|
Vue.use(VueRouter);
|
|
Vue.use(VueRouter);
|
|
|
|
|
|
|
|
-/** 运行时计算 base,避免模块加载时 __POWERED_BY_QIANKUN__ 尚未注入 */
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 乾坤:/page-hr/(同 EOM 的 /page-eos/)
|
|
|
|
|
+ * 独立:/hr/(同 EOM 的 /eos/)
|
|
|
|
|
+ */
|
|
|
export function getAppRouterBase() {
|
|
export function getAppRouterBase() {
|
|
|
- const pageBase = `/page-${SYSTEM_NAME}/`;
|
|
|
|
|
- const shortBase = `/${SYSTEM_NAME}/`;
|
|
|
|
|
- if (window.__POWERED_BY_QIANKUN__) {
|
|
|
|
|
- return pageBase;
|
|
|
|
|
- }
|
|
|
|
|
- try {
|
|
|
|
|
- const path = window.location?.pathname || '';
|
|
|
|
|
- if (path === `/page-${SYSTEM_NAME}` || path.startsWith(pageBase)) {
|
|
|
|
|
- return pageBase;
|
|
|
|
|
- }
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- /* ignore */
|
|
|
|
|
- }
|
|
|
|
|
- return shortBase;
|
|
|
|
|
|
|
+ return window.__POWERED_BY_QIANKUN__
|
|
|
|
|
+ ? `/page-${SYSTEM_NAME}/`
|
|
|
|
|
+ : `/${SYSTEM_NAME}/`;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function createRouter() {
|
|
function createRouter() {
|
|
@@ -51,36 +43,20 @@ function createRouter() {
|
|
|
|
|
|
|
|
const router = createRouter();
|
|
const router = createRouter();
|
|
|
|
|
|
|
|
-/** 动态路由是否已挂到当前 matcher */
|
|
|
|
|
let dynamicRoutesReady = false;
|
|
let dynamicRoutesReady = false;
|
|
|
|
|
|
|
|
-/** 重新登录 / 切角色前清空动态路由 */
|
|
|
|
|
export function resetRouter() {
|
|
export function resetRouter() {
|
|
|
const newRouter = createRouter();
|
|
const newRouter = createRouter();
|
|
|
router.matcher = newRouter.matcher;
|
|
router.matcher = newRouter.matcher;
|
|
|
- // 同步 options.base,避免后续跳转仍用旧 base
|
|
|
|
|
router.options.base = newRouter.options.base;
|
|
router.options.base = newRouter.options.base;
|
|
|
dynamicRoutesReady = false;
|
|
dynamicRoutesReady = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** 乾坤下把本系统菜单写回门户,防止被主应用默认/首个系统菜单覆盖 */
|
|
|
|
|
-function syncMenusToPortal(menus) {
|
|
|
|
|
- if (!window.__POWERED_BY_QIANKUN__ || !menus?.length) return;
|
|
|
|
|
- try {
|
|
|
|
|
- Vue.prototype.$portalStore?.commit('user/setMenus', menus);
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.warn('[page-hr] syncMenusToPortal failed', e);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
|
|
function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
|
|
|
const nextMenus = rewriteLegacyHrMenus(menus || []);
|
|
const nextMenus = rewriteLegacyHrMenus(menus || []);
|
|
|
const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
|
|
const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
|
|
|
store.commit('user/setMenus', nextMenus);
|
|
store.commit('user/setMenus', nextMenus);
|
|
|
store.commit('user/setAuthoritiesRouter', nextAuth);
|
|
store.commit('user/setAuthoritiesRouter', nextAuth);
|
|
|
- // 接口拉到的菜单注册时同步写回门户
|
|
|
|
|
- syncMenusToPortal(nextMenus);
|
|
|
|
|
- // 与 EOM 一致:有菜单才注册
|
|
|
|
|
if (nextMenus.length || nextAuth.length) {
|
|
if (nextMenus.length || nextAuth.length) {
|
|
|
router.addRoute(
|
|
router.addRoute(
|
|
|
getMenuRoutes(
|
|
getMenuRoutes(
|
|
@@ -92,23 +68,19 @@ function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
|
|
|
dynamicRoutesReady = true;
|
|
dynamicRoutesReady = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** 供登录页等外部主动注册动态路由 */
|
|
|
|
|
export function setupDynamicRoutes(menus, homePath, authoritiesRouter = []) {
|
|
export function setupDynamicRoutes(menus, homePath, authoritiesRouter = []) {
|
|
|
resetRouter();
|
|
resetRouter();
|
|
|
registerMenuRoutes(menus, homePath, authoritiesRouter);
|
|
registerMenuRoutes(menus, homePath, authoritiesRouter);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * 路由守卫(对齐 EOM:fetch 后 next({ ...to }),不强制改写地址栏)
|
|
|
|
|
- */
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
router.beforeEach((to, from, next) => {
|
|
|
if (!from.path.includes(REDIRECT_PATH)) {
|
|
if (!from.path.includes(REDIRECT_PATH)) {
|
|
|
NProgress.start();
|
|
NProgress.start();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (getToken()) {
|
|
if (getToken()) {
|
|
|
- // 旧首页别名统一到 HOME_PATH
|
|
|
|
|
const homePath = HOME_PATH || '/home/index';
|
|
const homePath = HOME_PATH || '/home/index';
|
|
|
|
|
+
|
|
|
if (isHomeAliasPath(to.path) && to.path !== homePath) {
|
|
if (isHomeAliasPath(to.path) && to.path !== homePath) {
|
|
|
next({
|
|
next({
|
|
|
path: homePath,
|
|
path: homePath,
|
|
@@ -119,12 +91,11 @@ router.beforeEach((to, from, next) => {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 还未注册动态路由则先获取(与 EOM:menus == null)
|
|
|
|
|
|
|
+ // 与 EOM:menus 未加载时拉取;有菜单才 addRoute
|
|
|
if (store.state.user.menus == null) {
|
|
if (store.state.user.menus == null) {
|
|
|
store
|
|
store
|
|
|
.dispatch('user/fetchUserInfo')
|
|
.dispatch('user/fetchUserInfo')
|
|
|
.then(({ menus, homePath: menuHome, authoritiesRouter }) => {
|
|
.then(({ menus, homePath: menuHome, authoritiesRouter }) => {
|
|
|
- // 与 EOM:有菜单才 addRoute;无菜单也 next,避免卡死
|
|
|
|
|
if (menus?.length || authoritiesRouter?.length) {
|
|
if (menus?.length || authoritiesRouter?.length) {
|
|
|
registerMenuRoutes(
|
|
registerMenuRoutes(
|
|
|
menus || [],
|
|
menus || [],
|
|
@@ -132,20 +103,9 @@ router.beforeEach((to, from, next) => {
|
|
|
authoritiesRouter || [],
|
|
authoritiesRouter || [],
|
|
|
);
|
|
);
|
|
|
} else {
|
|
} else {
|
|
|
- // 乾坤下接口暂无 /page-hr:沿用门户侧栏做路由,但不回写,
|
|
|
|
|
- // 避免把主应用已错误覆盖的「第一个系统菜单」再次固化
|
|
|
|
|
- const portalMenus =
|
|
|
|
|
- Vue.prototype.$portalStore?.state?.user?.menus;
|
|
|
|
|
- if (window.__POWERED_BY_QIANKUN__ && portalMenus?.length) {
|
|
|
|
|
- store.commit(
|
|
|
|
|
- 'user/setMenus',
|
|
|
|
|
- rewriteLegacyHrMenus(portalMenus),
|
|
|
|
|
- );
|
|
|
|
|
- dynamicRoutesReady = true;
|
|
|
|
|
- } else {
|
|
|
|
|
- store.commit('user/setMenus', []);
|
|
|
|
|
- dynamicRoutesReady = true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 标记已尝试,避免死循环;乾坤下侧栏仍由门户管
|
|
|
|
|
+ store.commit('user/setMenus', menus || []);
|
|
|
|
|
+ dynamicRoutesReady = true;
|
|
|
}
|
|
}
|
|
|
next({ ...to, replace: true });
|
|
next({ ...to, replace: true });
|
|
|
})
|
|
})
|
|
@@ -156,7 +116,6 @@ router.beforeEach((to, from, next) => {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 有菜单但动态路由未就绪时补注册
|
|
|
|
|
if (!dynamicRoutesReady && store.state.user.menus?.length) {
|
|
if (!dynamicRoutesReady && store.state.user.menus?.length) {
|
|
|
registerMenuRoutes(
|
|
registerMenuRoutes(
|
|
|
store.state.user.menus,
|
|
store.state.user.menus,
|