Update storage.js via upload script - 2026-10-15 11:17:02
This commit is contained in:
parent
bcda9c2ef6
commit
e97a96530c
36
storage.js
36
storage.js
|
|
@ -135,10 +135,13 @@ export async function parseCatalogFiles(files, hints) {
|
|||
if (hints.brand) fd.append('hintBrand', hints.brand);
|
||||
if (hints.type) fd.append('hintType', hints.type);
|
||||
}
|
||||
const res = await fetch('/api/ai/parse-catalog', {
|
||||
method: 'POST',
|
||||
body: fd,
|
||||
});
|
||||
// async=1:后端立刻返回 jobId,然后这里每 3 秒查一次结果,避免长请求被代理掐断(Failed to fetch)
|
||||
let res;
|
||||
try {
|
||||
res = await fetch('/api/ai/parse-catalog?async=1', { method: 'POST', body: fd });
|
||||
} catch (e) {
|
||||
throw new Error('上传失败:网络连接中断(如果刚在电脑上改过这个文件,请重新选择文件再试)');
|
||||
}
|
||||
let data;
|
||||
try {
|
||||
data = await res.json();
|
||||
|
|
@ -148,7 +151,30 @@ export async function parseCatalogFiles(files, hints) {
|
|||
throw new Error(`parse failed (HTTP ${res.status},响应内容不是JSON,可能是Nginx上传大小/超时限制拦截了请求)`);
|
||||
}
|
||||
if (!res.ok) throw new Error(data.error || `parse failed (HTTP ${res.status})`);
|
||||
return data; // { products, ocrTextLength }
|
||||
if (!data.jobId) return data; // 老版本后端:直接返回结果
|
||||
return pollAiJob(data.jobId);
|
||||
}
|
||||
|
||||
async function pollAiJob(jobId) {
|
||||
const deadline = Date.now() + 15 * 60 * 1000;
|
||||
let netFails = 0;
|
||||
while (Date.now() < deadline) {
|
||||
await new Promise((r) => setTimeout(r, 3000));
|
||||
let job;
|
||||
try {
|
||||
const r = await fetch(`/api/ai/jobs/${encodeURIComponent(jobId)}`);
|
||||
job = await r.json();
|
||||
if (!r.ok) throw Object.assign(new Error(job.error || `HTTP ${r.status}`), { fatal: true });
|
||||
netFails = 0;
|
||||
} catch (e) {
|
||||
if (e.fatal) throw e;
|
||||
if (++netFails >= 10) throw new Error('查询导入结果失败:网络连接不稳定,请稍后重试');
|
||||
continue; // 偶尔一次网络抖动不要紧,下一轮再查
|
||||
}
|
||||
if (job.status === 'done') return job.data; // { products, ocrTextLength, truncated }
|
||||
if (job.status === 'error') throw new Error((job.data && job.data.error) || 'AI导入失败');
|
||||
}
|
||||
throw new Error('AI导入超过15分钟没有完成,请重试');
|
||||
}
|
||||
|
||||
// 把一批中文词汇翻译成目标语言(en/fr/ru/es/ar),返回 {原词: 译文} 的映射。
|
||||
|
|
|
|||
Loading…
Reference in New Issue