From 2cb7653a71bd7d06e205910cf2b6df1ebd0cfd5d Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 29 Dec 2025 01:26:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0gallery=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=BC=BA=E5=A4=B1=E7=9A=84handleEnhance=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(chat)/gallery/page.tsx | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/app/(chat)/gallery/page.tsx b/app/(chat)/gallery/page.tsx index 53d56273d..d70bd7d52 100644 --- a/app/(chat)/gallery/page.tsx +++ b/app/(chat)/gallery/page.tsx @@ -155,6 +155,71 @@ export default function GalleryPage() { } }; + const handleEnhance = async (image: GalleryImage, e: React.MouseEvent) => { + e.stopPropagation(); // 阻止触发图片点击事件 + + if (enhancingImageId === image.id) return; // 防止重复点击 + + setEnhancingImageId(image.id); + + try { + toast.info("正在处理图片变清晰,请稍候..."); + + // 获取原图并转换为Base64 + const response = await fetch(image.url); + const blob = await response.blob(); + const reader = new FileReader(); + + reader.onloadend = async () => { + try { + const base64Data = (reader.result as string).split(",")[1]; + + // 调用变清晰API + const enhanceResponse = await fetch("/api/enhance", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + image_base64: base64Data, + prompt: `图片变清晰处理 - ${image.alt}`, + chatId: image.chatId, + messageId: image.messageId, + }), + }); + + if (!enhanceResponse.ok) { + const error = await enhanceResponse.json(); + throw new Error(error.error || error.detail || "变清晰失败"); + } + + const result = await enhanceResponse.json(); + + // 刷新相册列表以显示新图片 + await fetchImages(pagination.page); + + toast.success(`变清晰完成!处理时间: ${result.processing_time?.toFixed(2) || "未知"}秒`); + } catch (error) { + console.error("[变清晰] 错误:", error); + toast.error(error instanceof Error ? error.message : "变清晰处理失败"); + } finally { + setEnhancingImageId(null); + } + }; + + reader.onerror = () => { + toast.error("图片读取失败"); + setEnhancingImageId(null); + }; + + reader.readAsDataURL(blob); + } catch (error) { + console.error("[变清晰] 错误:", error); + toast.error(error instanceof Error ? error.message : "变清晰处理失败"); + setEnhancingImageId(null); + } + }; + const goToPage = (page: number) => { if (page >= 1 && page <= pagination.totalPages) { fetchImages(page);