33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
/**
|
|
* features/chat/shipping/shippingDownload.ts
|
|
* 用途:工作台发货 Excel 浏览器端下载(触发 a[download])。
|
|
* 依赖:shipping.api.workspaceShippingExportUrl、shared/utils/auth。
|
|
* 逻辑:useChatShippingSendHandler 在用户请求下载 Excel 时调用。
|
|
*/
|
|
import { workspaceShippingExportUrl } from '@/features/chat/api/shipping.api';
|
|
import { getAuthHeaders } from '@/shared/utils/auth';
|
|
|
|
export async function downloadWorkspaceShippingExcelFile(taskId: string): Promise<void> {
|
|
const url = workspaceShippingExportUrl(taskId);
|
|
const res = await fetch(url, { headers: getAuthHeaders() });
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => ({}));
|
|
throw new Error((body as { error?: string; message?: string })?.error || (body as { message?: string })?.message || res.statusText);
|
|
}
|
|
const disposition = res.headers.get('content-disposition');
|
|
const blob = await res.blob();
|
|
const objectUrl = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = objectUrl;
|
|
const rawName = disposition?.match(/filename\*?=(?:UTF-8'')?["']?([^;"'\n]+)/)?.[1] || `${taskId}.xlsx`;
|
|
try {
|
|
a.download = decodeURIComponent(rawName);
|
|
} catch {
|
|
a.download = rawName;
|
|
}
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(objectUrl);
|
|
}
|