feat: 前端改为轮询模式
This commit is contained in:
parent
e834029f6a
commit
965c37f2ff
@ -91,54 +91,31 @@ 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();
|
||||||
|
|
||||||
if (imageUrls.length > 0) {
|
|
||||||
// 将base64图片数据转换为可用的格式
|
|
||||||
const processedImages = imageUrls.map(base64Data => {
|
|
||||||
// 如果已经是完整的data URL,直接返回
|
|
||||||
if (base64Data.startsWith('data:image')) {
|
|
||||||
return base64Data;
|
|
||||||
}
|
|
||||||
// 否则添加data URL前缀
|
|
||||||
return `data:image/png;base64,${base64Data}`;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 存储图片数据
|
|
||||||
uni.setStorageSync('generated_images', JSON.stringify(processedImages));
|
|
||||||
|
|
||||||
// 成功,完成进度
|
|
||||||
completeProgress();
|
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '未生成图片',
|
title: '创建任务失败',
|
||||||
icon: 'none',
|
|
||||||
duration: 2000
|
|
||||||
});
|
|
||||||
revertProgress();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 失败,回退进度
|
|
||||||
uni.showToast({
|
|
||||||
title: res.data?.base_resp?.status_msg || '生成失败',
|
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
duration: 2000
|
duration: 2000
|
||||||
});
|
});
|
||||||
revertProgress();
|
revertProgress();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[GenerationLoading] 图生图失败:', err);
|
console.error('[GenerationLoading] 创建任务失败:', err);
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '生成失败',
|
title: '创建任务失败',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
duration: 2000
|
duration: 2000
|
||||||
});
|
});
|
||||||
@ -146,6 +123,77 @@ const callImageGeneration = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 轮询任务状态
|
||||||
|
const pollJobStatus = () => {
|
||||||
|
let pollCount = 0;
|
||||||
|
const maxPolls = 40; // 120秒 / 3秒 = 40次
|
||||||
|
|
||||||
|
const poll = async () => {
|
||||||
|
if (!jobId) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await uni.request({
|
||||||
|
url: 'http://192.168.110.60:8080' + `/api/v1/assets/mints/image/generation/${jobId}`,
|
||||||
|
method: 'GET',
|
||||||
|
header: {
|
||||||
|
'Authorization': `Bearer ${uni.getStorageSync('access_token')}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = res.data?.data;
|
||||||
|
if (data) {
|
||||||
|
// 更新进度
|
||||||
|
if (data.progress) {
|
||||||
|
progress.value = Math.min(data.progress, 90);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
};
|
||||||
|
|
||||||
// 处理成功
|
// 处理成功
|
||||||
const handleSuccess = () => {
|
const handleSuccess = () => {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user