优化文件上传进度计算逻辑,调整上传速度显示,简化上传完成后的状态处理。
This commit is contained in:
+15
-15
@@ -1030,25 +1030,30 @@ export default function FilesUpload() {
|
|||||||
setUploadStage("uploading");
|
setUploadStage("uploading");
|
||||||
setUploadProgress(0);
|
setUploadProgress(0);
|
||||||
|
|
||||||
// 计算总大小并开启与旧逻辑一致的模拟进度
|
// 计算总大小并开启与旧逻辑一致的模拟进度(按时间推进到 95%)
|
||||||
const totalSize = filesForProgress.reduce((sum, f) => sum + (f?.size || 0), 0);
|
const totalSize = filesForProgress.reduce((sum, f) => sum + (f?.size || 0), 0);
|
||||||
let uploadedSize = 0;
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
let lastUploadedSize = 0;
|
|
||||||
let lastUpdateTime = startTime;
|
let lastUpdateTime = startTime;
|
||||||
|
let lastRatio = 0;
|
||||||
|
const estimatedUploadTime = Math.max(
|
||||||
|
(totalSize / (1024 * 1024)) / 3 * 1000, // 3MB/s 估算
|
||||||
|
1000
|
||||||
|
);
|
||||||
|
|
||||||
if (uploadProgressIntervalRef.current) {
|
if (uploadProgressIntervalRef.current) {
|
||||||
clearInterval(uploadProgressIntervalRef.current);
|
clearInterval(uploadProgressIntervalRef.current);
|
||||||
}
|
}
|
||||||
uploadProgressIntervalRef.current = setInterval(() => {
|
uploadProgressIntervalRef.current = setInterval(() => {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const elapsed = (now - lastUpdateTime) / 1000;
|
const deltaSec = (now - lastUpdateTime) / 1000;
|
||||||
const speed = elapsed > 0 ? (uploadedSize - lastUploadedSize) / elapsed : 0; // bytes/s
|
const ratio = Math.min((now - startTime) / estimatedUploadTime, 0.95);
|
||||||
lastUploadedSize = uploadedSize;
|
// 计算瞬时速度(基于比例变化)
|
||||||
|
const deltaRatio = Math.max(ratio - lastRatio, 0);
|
||||||
|
const bytesPerSec = deltaSec > 0 ? (totalSize * deltaRatio) / deltaSec : 0;
|
||||||
|
lastRatio = ratio;
|
||||||
lastUpdateTime = now;
|
lastUpdateTime = now;
|
||||||
setUploadSpeed(`${formatFileSize(speed)}/s`);
|
setUploadSpeed(`${formatFileSize(bytesPerSec)}/s`);
|
||||||
const progress = Math.min((uploadedSize / Math.max(totalSize, 1)) * 100, 95);
|
setUploadProgress(parseFloat((ratio * 100).toFixed(2)));
|
||||||
setUploadProgress(parseFloat(progress.toFixed(2)));
|
|
||||||
}, 200);
|
}, 200);
|
||||||
|
|
||||||
// 转二进制
|
// 转二进制
|
||||||
@@ -1073,9 +1078,6 @@ export default function FilesUpload() {
|
|||||||
if (!uploadResp.result) throw new Error('主文件上传失败');
|
if (!uploadResp.result) throw new Error('主文件上传失败');
|
||||||
const documentId = uploadResp.result.id;
|
const documentId = uploadResp.result.id;
|
||||||
|
|
||||||
// 模拟:主文件完成后将 uploadedSize 直接置为总大小的 95%
|
|
||||||
uploadedSize = Math.max(totalSize * 0.95, uploadResp.result.file_size || totalSize * 0.5);
|
|
||||||
|
|
||||||
// 可选:模板上传
|
// 可选:模板上传
|
||||||
if (templateFiles && templateFiles.length > 0) {
|
if (templateFiles && templateFiles.length > 0) {
|
||||||
const tpl = templateFiles[0];
|
const tpl = templateFiles[0];
|
||||||
@@ -1088,8 +1090,6 @@ export default function FilesUpload() {
|
|||||||
if ('error' in tplResult && tplResult.error) {
|
if ('error' in tplResult && tplResult.error) {
|
||||||
throw new Error(tplResult.error);
|
throw new Error(tplResult.error);
|
||||||
}
|
}
|
||||||
// 模板算作少量额外上传
|
|
||||||
uploadedSize = totalSize * 0.98;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 完成:清理进度定时器并置满
|
// 完成:清理进度定时器并置满
|
||||||
@@ -1100,7 +1100,7 @@ export default function FilesUpload() {
|
|||||||
setUploadProgress(100);
|
setUploadProgress(100);
|
||||||
setUploadSpeed('完成');
|
setUploadSpeed('完成');
|
||||||
|
|
||||||
toastService.success('上传成功,已触发处理');
|
toastService.success('上传成功');
|
||||||
// 刷新队列
|
// 刷新队列
|
||||||
await filterDocuments(reviewType);
|
await filterDocuments(reviewType);
|
||||||
setUploadStage("processing");
|
setUploadStage("processing");
|
||||||
|
|||||||
Reference in New Issue
Block a user