feat: 1:资源列表下载所有添加批量单个下载以及批量下载zip
This commit is contained in:
@@ -300,9 +300,77 @@ const OrderResourcesModal: React.FC<OrderResourcesModalProps> = ({ visible, reco
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ ZIP 打包失败:`, error);
|
||||
message.error('打包下载失败,请重试');
|
||||
removeDownloadProgress('batch_all');
|
||||
} finally {
|
||||
|
||||
// 判断错误类型并给出提示
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
if (errorMessage.includes('CORS') || errorMessage.includes('Failed to fetch')) {
|
||||
message.error('ZIP打包失败:跨域问题,请使用"分别下载"功能');
|
||||
} else {
|
||||
message.error('打包下载失败,文件可能过大,建议使用"分别下载"');
|
||||
}
|
||||
setBatchDownloading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 批量分别下载(备用方案)- 使用 iframe 避免 CORS
|
||||
const handleBatchDownloadSeparately = async () => {
|
||||
if (!record?.orderNo) {
|
||||
return;
|
||||
}
|
||||
|
||||
const orderNo = record.orderNo;
|
||||
console.log(`📦 开始分别下载: ${orderNo}`);
|
||||
message.info('正在分别下载所有文件,请查看浏览器下载列表');
|
||||
|
||||
try {
|
||||
let successCount = 0;
|
||||
let failureCount = 0;
|
||||
|
||||
// 获取所有文件的下载URL
|
||||
for (let i = 0; i < resources.length; i++) {
|
||||
const file = resources[i];
|
||||
try {
|
||||
if (!file.fileId) {
|
||||
failureCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const downloadUrl = await ordersApi.getFileDownloadUrl(file.fileId);
|
||||
if (downloadUrl) {
|
||||
// 使用 iframe 触发下载
|
||||
setTimeout(() => {
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.style.display = 'none';
|
||||
iframe.src = downloadUrl;
|
||||
document.body.appendChild(iframe);
|
||||
|
||||
// 5秒后移除 iframe
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(iframe);
|
||||
}, 5000);
|
||||
}, i * 100); // 每个文件间隔 100ms
|
||||
|
||||
successCount++;
|
||||
console.log(`✅ 触发下载: ${file.fileId}`);
|
||||
} else {
|
||||
failureCount++;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`⚠️ 获取下载链接失败: ${file.fileId}`, error);
|
||||
failureCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟显示完成消息
|
||||
setTimeout(() => {
|
||||
message.success(`✅ 分别下载已启动!成功: ${successCount} 个,失败: ${failureCount} 个`);
|
||||
setBatchDownloading(false);
|
||||
}, resources.length * 100 + 500);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ 分别下载失败:`, error);
|
||||
message.error('下载失败,请重试');
|
||||
setBatchDownloading(false);
|
||||
}
|
||||
};
|
||||
@@ -627,13 +695,23 @@ const OrderResourcesModal: React.FC<OrderResourcesModalProps> = ({ visible, reco
|
||||
关闭
|
||||
</Button>,
|
||||
<Button
|
||||
key="batch-download"
|
||||
key="separate-download"
|
||||
type="primary"
|
||||
icon={<DownloadOutlined />}
|
||||
loading={batchDownloading}
|
||||
disabled={resources.length === 0 || loading || batchDownloading}
|
||||
onClick={handleBatchDownloadSeparately}
|
||||
title="分别下载所有文件(推荐用于大文件)"
|
||||
>
|
||||
{batchDownloading ? '下载中...' : '分别下载'}
|
||||
</Button>,
|
||||
<Button
|
||||
key="zip-download"
|
||||
icon={<FileZipOutlined />}
|
||||
loading={batchDownloading}
|
||||
disabled={resources.length === 0 || loading || batchDownloading}
|
||||
onClick={handleBatchDownload}
|
||||
title={batchDownloading ? `正在打包下载中...` : '批量下载为ZIP'}
|
||||
title="打包为ZIP下载(适合小文件,大文件可能导致浏览器崩溃)"
|
||||
>
|
||||
{batchDownloading ? (() => {
|
||||
const batchProgress = Array.from(downloadingFiles.values()).find(
|
||||
@@ -642,8 +720,8 @@ const OrderResourcesModal: React.FC<OrderResourcesModalProps> = ({ visible, reco
|
||||
if (batchProgress) {
|
||||
return `打包中... ${batchProgress.progress}%`;
|
||||
}
|
||||
return `打包中... ${batchDownloadProgress}%`;
|
||||
})() : '批量下载'}
|
||||
return `打包中...`;
|
||||
})() : '打包ZIP'}
|
||||
</Button>,
|
||||
]}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user