fix: 修复 Hash 路由跳转丢失部署基路径

- 修复懒加载分包失败后跳转到站点根路径的问题

- 统一管理端和用户中心新窗口路由解析

- 增加部署基路径与 Hash 路由回归测试
This commit is contained in:
2026-08-31 18:47:54 +08:00
parent 263f5f4b8b
commit 3e34b65bcb
11 changed files with 83 additions and 23 deletions

View File

@@ -1,17 +1,20 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { openWindow } from '../window';
import { openRouteInNewWindow, openWindow } from '../window';
describe('openWindow', () => {
// 保存原始的 window.open 函数
let originalOpen: typeof window.open;
let originalUrl: string;
beforeEach(() => {
originalOpen = window.open;
originalUrl = window.location.href;
});
afterEach(() => {
window.open = originalOpen;
window.history.replaceState({}, '', originalUrl);
});
it('should call window.open with correct arguments', () => {
@@ -30,4 +33,17 @@ describe('openWindow', () => {
'noopener=yes,noreferrer=yes',
);
});
it('should preserve a router-resolved base and hash route', () => {
window.history.replaceState({}, '', '/flow/#/dashboard');
window.open = vi.fn();
openRouteInNewWindow('#/ai/workflow');
expect(window.open).toHaveBeenCalledWith(
`${window.location.origin}/flow/#/ai/workflow`,
'_blank',
'noopener=yes,noreferrer=yes',
);
});
});

View File

@@ -25,13 +25,10 @@ function openWindow(url: string, options: OpenWindowOptions = {}): void {
/**
* 在新窗口中打开路由。
* @param path
* @param href Vue Router 解析后的地址
*/
function openRouteInNewWindow(path: string) {
const { hash, origin } = location;
const fullPath = path.startsWith('/') ? path : `/${path}`;
const url = `${origin}${hash && !fullPath.startsWith('/#') ? '/#' : ''}${fullPath}`;
openWindow(url, { target: '_blank' });
function openRouteInNewWindow(href: string) {
openWindow(new URL(href, location.href).href, { target: '_blank' });
}
export { openRouteInNewWindow, openWindow };

View File

@@ -47,7 +47,7 @@ export function useTabs() {
}
async function openTabInNewWindow(tab?: RouteLocationNormalized) {
await tabbarStore.openTabInNewWindow(tab || route);
await tabbarStore.openTabInNewWindow(tab || route, router);
}
async function closeTabByKey(key: string) {

View File

@@ -307,9 +307,11 @@ export const useTabbarStore = defineStore('core-tabbar', {
/**
* @zh_CN 新窗口打开标签页
* @param tab
* @param router
*/
async openTabInNewWindow(tab: TabDefinition) {
openRouteInNewWindow(tab.fullPath || tab.path);
async openTabInNewWindow(tab: TabDefinition, router: Router) {
const href = router.resolve(tab.fullPath || tab.path).href;
openRouteInNewWindow(href);
},
/**