diff --git a/app/api/watermark-remove/route.ts b/app/api/watermark-remove/route.ts index 4d0b2eaef..5a659b168 100644 --- a/app/api/watermark-remove/route.ts +++ b/app/api/watermark-remove/route.ts @@ -28,7 +28,7 @@ async function getWatermarkRemoveConfig() { // 轮询获取处理结果 async function pollForResult(taskId: string, apiKey: string): Promise<{ success: boolean; imageUrl?: string; error?: string }> { - const maxAttempts = 30; // 最多轮询30次(30秒) + const maxAttempts = 180; // 最多轮询180次(约3分钟) const pollInterval = 1000; // 每次间隔1秒 for (let attempt = 0; attempt < maxAttempts; attempt++) { @@ -54,19 +54,25 @@ async function pollForResult(taskId: string, apiKey: string): Promise<{ success: const result = await response.json(); console.log(`[WatermarkRemove] 轮询结果 (attempt ${attempt + 1}):`, JSON.stringify(result).substring(0, 200)); + const state = result.data?.state; + const stateDetail = result.data?.state_detail; + // 检查任务状态 - if (result.data?.state === 1) { + if (state === 1) { // 任务完成 - PicWish API 返回 "file" 字段 const imageUrl = result.data?.file || result.data?.image; if (imageUrl) { return { success: true, imageUrl }; } return { success: false, error: "处理完成但未返回图片URL" }; - } else if (result.data?.state === -1 || result.data?.state < 0) { + } else if (typeof state === "number" && state < 0) { // 任务失败 return { success: false, error: result.data?.error || "去水印处理失败" }; + } else if (state === 4 || state === 0) { + // 明确处理中状态,继续轮询 + console.log(`[WatermarkRemove] 任务处理中: state=${state}, detail=${stateDetail || "N/A"}`); } - // state === 0 表示处理中,继续轮询 + // 其他状态默认继续轮询 await new Promise(resolve => setTimeout(resolve, pollInterval)); } catch (error) { @@ -75,7 +81,7 @@ async function pollForResult(taskId: string, apiKey: string): Promise<{ success: } } - return { success: false, error: "处理超时,请稍后重试" }; + return { success: false, error: "去水印任务处理超时(服务端仍在处理中),请稍后重试" }; } export async function POST(request: NextRequest) {