refactor(ecom-dmb): 导出服务重构,ExportServiceImpl拆分为独立服务类,抽取ExportFilenameUtils统一文件名构建

This commit is contained in:
小蟑螂
2026-04-28 14:02:29 +08:00
parent 455f10beed
commit 57fe400dee
21 changed files with 979 additions and 831 deletions
@@ -14,6 +14,26 @@ function formatToken(token: null | string) {
return token ? `Bearer ${token}` : null;
}
/** 从 Content-Disposition 解析文件名(与后端 attachment; filename="..." 对齐) */
function parseFilenameFromContentDisposition(
contentDisposition: string | null,
): string | null {
if (!contentDisposition) return null;
const utf8Match = /filename\*=UTF-8''([^;\s]+)/i.exec(contentDisposition);
if (utf8Match?.[1]) {
try {
return decodeURIComponent(utf8Match[1].replace(/"/g, ''));
} catch {
return utf8Match[1].replace(/"/g, '');
}
}
const quoted = /filename="([^"]+)"/i.exec(contentDisposition);
if (quoted?.[1]) return quoted[1];
const plain = /filename=([^;\s]+)/i.exec(contentDisposition);
if (plain?.[1]) return plain[1].replace(/^"|"$/g, '');
return null;
}
export async function exportByPost(
url: string,
params: Record<string, unknown>,
@@ -55,11 +75,16 @@ export async function exportByPost(
return;
}
const headerName =
parseFilenameFromContentDisposition(
response.headers.get('Content-Disposition'),
) || filename;
// 触发浏览器下载
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = filename;
link.download = headerName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);