feat: 添加分享结果卡片功能,形成用户增长闭环

- 新增 ShareCardModal 组件,支持餐厅匹配和盲盒契约两种分享卡片
- 卡片包含品牌标识、匹配结果、餐厅/想法详情、二维码
- 使用 html-to-image 生成高清 PNG,支持保存图片和 Web Share API 分享
- 餐厅图片通过 canvas 转 data URL 处理跨域
- 集成到 MatchResult(极速救场)和 BlindBox reveal(周末契约)
This commit is contained in:
2026-02-26 13:50:38 +08:00
parent 14b0aaece4
commit 08eb55ca41
5 changed files with 1018 additions and 49 deletions
+21 -42
View File
@@ -23,6 +23,7 @@ import {
import { Restaurant, MatchType, RunnerUp, SceneType } from "@/types";
import { fireCelebration, playChime } from "@/lib/celebrate";
import { isRegistered } from "@/lib/userId";
import ShareCardModal from "@/components/ShareCardModal";
interface MatchResultProps {
restaurant: Restaurant;
@@ -183,6 +184,7 @@ export default function MatchResult({
}: MatchResultProps) {
const router = useRouter();
const [showRunnerUps, setShowRunnerUps] = useState(false);
const [showShareCard, setShowShareCard] = useState(false);
const [toast, setToast] = useState("");
const celebratedRef = useRef(false);
const historySavedRef = useRef(false);
@@ -223,46 +225,9 @@ export default function MatchResult({
}).catch(() => {});
}, [userId, roomId, restaurant, matchType, userCount]);
const handleShare = useCallback(async () => {
const verb = scene === "drink" ? "喝" : "吃";
const lines = [
isUnanimous
? `🎉 默契度 100%${userCount} 人全员一致选了同一家!`
: `🎉 我们用 NoWhatever 选好了去哪${verb}`,
``,
`📍 ${restaurant.name}`,
restaurant.rating ? `${restaurant.rating}` : "",
restaurant.price && restaurant.price !== "未知" ? `💰 人均${restaurant.price}` : "",
restaurant.address ? `📮 ${restaurant.address}` : "",
``,
isUnanimous ? `✨ 这就是心有灵犀吧~` : "",
].filter(Boolean);
const text = lines.join("\n");
const navUrl = buildNavUrl(restaurant);
const shareData = {
title: `我们选了${restaurant.name}`,
text,
url: navUrl,
};
try {
if (navigator.share && navigator.canShare?.(shareData)) {
await navigator.share(shareData);
return;
}
} catch (e) {
if (e instanceof Error && e.name === "AbortError") return;
}
try {
await navigator.clipboard.writeText(`${text}\n\n${navUrl}`);
showToast("已复制,快去发给朋友吧!");
} catch {
showToast("复制失败,请手动复制");
}
}, [restaurant, showToast, isUnanimous, userCount, scene]);
const handleOpenShareCard = useCallback(() => {
setShowShareCard(true);
}, []);
if (matchType === "no_match") {
return <NoMatchResult onReset={onReset} resetting={resetting} />;
@@ -451,12 +416,12 @@ export default function MatchResult({
)}
<motion.button
onClick={handleShare}
onClick={handleOpenShareCard}
className="flex items-center justify-center gap-2 rounded-full bg-surface px-8 py-3 text-sm font-bold text-gray-300 ring-1 ring-border transition-colors hover:bg-elevated"
whileTap={{ scale: 0.95 }}
>
<Share2 size={15} />
</motion.button>
</motion.div>
@@ -557,6 +522,20 @@ export default function MatchResult({
</motion.div>
</div>
<ShareCardModal
open={showShareCard}
onClose={() => setShowShareCard(false)}
data={{
type: "restaurant",
restaurant,
matchType,
matchLikes,
userCount,
scene,
}}
onToast={showToast}
/>
<AnimatePresence>
{toast && (
<motion.div