fix: 添加gallery页面缺失的handleEnhance函数实现
This commit is contained in:
parent
05ea65964b
commit
2cb7653a71
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user