"use client"; import { type ChangeEvent, useRef, useState } from "react"; import styles from "../vip.module.css"; type PromoProofModalProps = { open: boolean; onClose: () => void; }; type RecognizeResult = { ok: boolean; nickname?: string; avatar_image_base64?: string; avatar_mime?: string; confidence?: { nickname?: number; avatar?: number; overall?: number; }; message?: string; error?: string; }; export function PromoProofModal({ open, onClose }: PromoProofModalProps) { const fileInputRef = useRef(null); const [confirmed, setConfirmed] = useState(false); const [uploading, setUploading] = useState(false); const [result, setResult] = useState(null); const [error, setError] = useState(""); if (!open) return null; const onPickFile = () => { fileInputRef.current?.click(); }; const onFileChange = async (event: ChangeEvent) => { const file = event.target.files?.[0]; if (!file) return; setError(""); setResult(null); setUploading(true); try { const form = new FormData(); form.append("image", file); const res = await fetch("/api/promo/recognize-discount", { method: "POST", body: form, }); const data = (await res.json().catch(() => ({}))) as RecognizeResult; if (!res.ok || !data?.ok) { setError(data?.error || data?.message || "识别失败,请换一张清晰截图再试"); } else { setResult(data); } } catch (e) { setError(e instanceof Error ? e.message : "上传失败,请稍后重试"); } finally { setUploading(false); event.target.value = ""; } }; const avatarSrc = result?.avatar_image_base64 && result?.avatar_mime ? `data:${result.avatar_mime};base64,${result.avatar_image_base64}` : null; return (
限时优惠任务

推荐任意公众号文章,立减 50 元

在手机里打开任意一篇公众号文章,点击文末推荐(心形 icon),然后上传截图。系统将识别 头像 + 昵称 + 心形推荐痕迹 作为优惠凭证。

示例参考:页面左下角「异地世界」上方那一行(头像 / Eric / 爱心)。

{!confirmed ? (
) : (

支持 jpg/png/webp,建议宽度大于 720px 且文字清晰

{error ?

{error}

: null} {result?.ok ? (

识别结果

{avatarSrc ? ( 识别头像 ) : (
无头像
)}

昵称

{result.nickname || "未识别"}

) : null}
)}
); }