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
@@ -149,13 +149,13 @@ export namespace ShopVO {
salesAmountTrend?: TrendData;
/** 单量趋势分析 */
orderCountTrend?: TrendData;
/** 多商品数据横向对比(含合计行) */
/** 多商品数据横向汇总(含合计行) */
productOverview?: ProductOverview;
/** 品类多维度数据对比(含合计行) */
/** 品类多维度数据汇总(含合计行) */
categoryOverview?: CategoryOverview;
}
/** 商品对比数据 */
/** 商品汇总数据 */
export interface ProductComparison {
/** 商品ID */
productId: number;
@@ -193,7 +193,7 @@ export namespace ShopVO {
roi?: number;
}
/** 品类对比数据 */
/** 品类汇总数据 */
export interface CategoryComparison {
/** 品类ID */
categoryId: number;
@@ -249,7 +249,7 @@ export namespace ShopVO {
export interface ProductOverview {
/** 合计行 */
total: ProductComparison;
/** 各商品对比列表 */
/** 各商品汇总列表 */
products: ProductComparison[];
}
@@ -257,7 +257,7 @@ export namespace ShopVO {
export interface CategoryOverview {
/** 合计行 */
total: CategoryComparison;
/** 各品类对比列表 */
/** 各品类汇总列表 */
categories: CategoryComparison[];
}
}
@@ -23,9 +23,9 @@ export namespace CompanyOverviewVO {
shopSalesRatios?: ShopSalesRatio[];
/** 店铺效率象限(ROI vs 净利) */
shopEfficiencyQuadrants?: ShopEfficiencyQuadrant[];
/** 多店铺数据横向对比 */
/** 多店铺数据横向汇总 */
shopComparisons?: ShopOverview;
/** 多品类数据横向对比 */
/** 多品类数据横向汇总 */
categoryComparisons?: CategoryOverview;
/** 平台维度概览(包含合计行和平台列表) */
platformSummary?: PlatformOverview;
@@ -69,7 +69,7 @@ export namespace CompanyOverviewVO {
quadrant: number;
}
/** 店铺对比数据(16维度) */
/** 店铺汇总数据(16维度) */
export interface ShopComparison {
/** 店铺ID */
shopId: number;
@@ -115,7 +115,7 @@ export namespace CompanyOverviewVO {
roi?: number;
}
/** 品类对比数据 */
/** 品类汇总数据 */
export interface CategoryComparison {
/** 品类ID(未分配为null) */
categoryId?: number;
@@ -207,7 +207,7 @@ export namespace CompanyOverviewVO {
export interface ShopOverview {
/** 合计行 */
total: ShopComparison;
/** 各店铺对比列表 */
/** 各店铺汇总列表 */
shops: ShopComparison[];
}
@@ -215,7 +215,7 @@ export namespace CompanyOverviewVO {
export interface CategoryOverview {
/** 合计行 */
total: CategoryComparison;
/** 各品类对比列表 */
/** 各品类汇总列表 */
categories: CategoryComparison[];
}
}
@@ -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);
@@ -69,7 +69,12 @@ const handleCategoryExport = async () => {
if (selectedExportCategoryIds.value.length > 0) {
Object.assign(params, { categoryIds: selectedExportCategoryIds.value });
}
await exportByPost('/ec/category-overview/export', params, '品类多维度经营明细.xlsx');
const [cStart, cEnd] = dateRange.value;
await exportByPost(
'/ec/category-overview/export',
params,
`品类概览_${cStart.format('YYYYMMDD')}-${cEnd.format('YYYYMMDD')}.xlsx`,
);
};
// 税率(公司/店铺概览共用持久化)
@@ -386,7 +391,7 @@ const handleExportCompanyOverview = async () => {
: undefined;
}
const filename = `公司概览_${startDate.format('YYYYMMDD')}_${endDate.format('YYYYMMDD')}.xlsx`;
const filename = `公司概览_${startDate.format('YYYYMMDD')}-${endDate.format('YYYYMMDD')}.xlsx`;
await exportByPost(
'/ec/store-overview/export',
{
@@ -427,7 +432,7 @@ const handlePlatformExport = async () => {
try {
exportLoading.value = true;
const [startDate, endDate] = dateRange.value;
const filename = `平台维度汇总_${startDate.format('YYYYMMDD')}_${endDate.format('YYYYMMDD')}.xlsx`;
const filename = `平台维度汇总_${startDate.format('YYYYMMDD')}-${endDate.format('YYYYMMDD')}.xlsx`;
await exportByPost(
'/ec/platform-overview/export',
{
@@ -232,7 +232,7 @@ const handleExportShopOverview = async () => {
exportLoading.value = true;
const shopName = overviewData.value?.shopName || `店铺${shopId.value}`;
const [startDate, endDate] = dateRange.value;
const filename = `${shopName}_概览_${startDate.format('YYYYMMDD')}_${endDate.format('YYYYMMDD')}.xlsx`;
const filename = `${shopName}_概览_${startDate.format('YYYYMMDD')}-${endDate.format('YYYYMMDD')}.xlsx`;
await exportByPost(
'/ec/shop/export-overview',
{
@@ -270,7 +270,12 @@ const handleCategoryExport = async () => {
if (selectedExportCategoryIds.value.length > 0) {
params.categoryIds = selectedExportCategoryIds.value;
}
await exportByPost('/ec/shop/export-category', params, '门店品类汇总.xlsx');
const [catStart, catEnd] = dateRange.value;
await exportByPost(
'/ec/shop/export-category',
params,
`门店品类汇总_${catStart.format('YYYYMMDD')}-${catEnd.format('YYYYMMDD')}.xlsx`,
);
} finally {
categoryExportLoading.value = false;
}