feat: 前端改为轮询模式

This commit is contained in:
zerosaturation 2026-04-08 00:07:08 +08:00
parent e834029f6a
commit 965c37f2ff

View File

@ -91,59 +91,107 @@ const revertProgress = () => {
}, 100); }, 100);
}; };
// API // job_id
let jobId = null;
// API -
const callImageGeneration = async () => { const callImageGeneration = async () => {
try { try {
const res = await imageGenerationApi(generationData); const res = await imageGenerationApi(generationData);
if (res.data && res.data.base_resp && res.data.base_resp.status_code === 0) { if (res.data && res.data.job_id) {
console.log('[GenerationLoading] 图生图成功:', res); jobId = res.data.job_id;
console.log('[GenerationLoading] 任务已创建:', jobId);
// //
const imageUrls = res.data.data?.image_urls || []; pollJobStatus();
} else {
if (imageUrls.length > 0) { uni.showToast({
// base64 title: '创建任务失败',
const processedImages = imageUrls.map(base64Data => { icon: 'none',
// data URL duration: 2000
if (base64Data.startsWith('data:image')) { });
return base64Data; revertProgress();
} }
// data URL } catch (err) {
return `data:image/png;base64,${base64Data}`; console.error('[GenerationLoading] 创建任务失败:', err);
}); uni.showToast({
title: '创建任务失败',
// icon: 'none',
uni.setStorageSync('generated_images', JSON.stringify(processedImages)); duration: 2000
});
// revertProgress();
completeProgress(); }
} else { };
uni.showToast({
title: '未生成图片', //
icon: 'none', const pollJobStatus = () => {
duration: 2000 let pollCount = 0;
}); const maxPolls = 40; // 120 / 3 = 40
revertProgress();
} const poll = async () => {
} else { if (!jobId) return;
// 退
uni.showToast({ try {
title: res.data?.base_resp?.status_msg || '生成失败', const res = await uni.request({
icon: 'none', url: 'http://192.168.110.60:8080' + `/api/v1/assets/mints/image/generation/${jobId}`,
duration: 2000 method: 'GET',
}); header: {
revertProgress(); 'Authorization': `Bearer ${uni.getStorageSync('access_token')}`
} }
} catch (err) { });
console.error('[GenerationLoading] 图生图失败:', err);
uni.showToast({ const data = res.data?.data;
title: '生成失败', if (data) {
icon: 'none', //
duration: 2000 if (data.progress) {
}); progress.value = Math.min(data.progress, 90);
revertProgress(); }
}
if (data.status === 'COMPLETED') {
//
const imageUrls = data.images || [];
if (imageUrls.length > 0) {
uni.setStorageSync('generated_images', JSON.stringify(imageUrls));
completeProgress();
} else {
uni.showToast({ title: '未生成图片', icon: 'none' });
revertProgress();
}
return;
} else if (data.status === 'FAILED') {
//
uni.showToast({
title: data.error_msg || '生成失败',
icon: 'none',
duration: 2000
});
revertProgress();
return;
}
}
pollCount++;
if (pollCount >= maxPolls) {
uni.showToast({ title: '生成超时,请重试', icon: 'none' });
revertProgress();
return;
}
// 3
setTimeout(poll, 3000);
} catch (err) {
console.error('[GenerationLoading] 轮询失败:', err);
pollCount++;
if (pollCount >= maxPolls) {
uni.showToast({ title: '查询失败,请重试', icon: 'none' });
revertProgress();
} else {
setTimeout(poll, 3000);
}
}
};
poll();
}; };
// //