refactor: 兑换机制改为一图章一奖品并引入库存
- 废弃 RedemptionRule(集 N 换 1),新增 Prize 表与 Stamp 1:1 关联 - Redemption 记录直接绑定到 stampId + prizeId + prizeName 快照 - 兑换事务用 updateMany + stock>0 条件作乐观锁 - 兑换后保留 Collection 记录,图章持续彩色点亮并标记"已兑换" - 用户端入口改为点击已收集图章弹出兑换,库存为 0 时按钮禁用 - 管理后台删除 /admin/rules,奖品编辑嵌入 StampForm Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import type { StampWithStatus, RedemptionRuleInfo, RedemptionRecord } from "@stamp/shared";
|
||||
import type { StampWithStatus, RedemptionRecord } from "@stamp/shared";
|
||||
import { apiFetch } from "../lib/api";
|
||||
import { useAuth } from "../lib/auth";
|
||||
import StampGrid from "../components/StampGrid";
|
||||
@@ -11,27 +11,25 @@ export default function AlbumPage() {
|
||||
const navigate = useNavigate();
|
||||
const { user, isLoading: authLoading } = useAuth();
|
||||
const [stamps, setStamps] = useState<StampWithStatus[]>([]);
|
||||
const [rules, setRules] = useState<RedemptionRuleInfo[]>([]);
|
||||
const [history, setHistory] = useState<RedemptionRecord[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [showRedeem, setShowRedeem] = useState(false);
|
||||
const [selectedStampId, setSelectedStampId] = useState<string | null>(null);
|
||||
const [showRegister, setShowRegister] = useState(false);
|
||||
|
||||
const collectedCount = stamps.filter((s) => s.collected).length;
|
||||
const selectedStamp = selectedStampId ? stamps.find((s) => s.id === selectedStampId) ?? null : null;
|
||||
|
||||
const fetchData = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [stampsData, rulesData] = await Promise.all([
|
||||
apiFetch<StampWithStatus[]>("/stamps"),
|
||||
apiFetch<RedemptionRuleInfo[]>("/redemption/rules"),
|
||||
]);
|
||||
const stampsData = await apiFetch<StampWithStatus[]>("/stamps");
|
||||
setStamps(stampsData);
|
||||
setRules(rulesData);
|
||||
|
||||
if (user) {
|
||||
const historyData = await apiFetch<RedemptionRecord[]>("/redemption/history");
|
||||
setHistory(historyData);
|
||||
} else {
|
||||
setHistory([]);
|
||||
}
|
||||
} catch {
|
||||
// Stamps endpoint works without auth
|
||||
@@ -44,20 +42,21 @@ export default function AlbumPage() {
|
||||
if (!authLoading) fetchData();
|
||||
}, [authLoading, user]);
|
||||
|
||||
const handleRedeem = async (ruleId: string) => {
|
||||
const handleRedeem = async (stampId: string) => {
|
||||
await apiFetch("/redemption/redeem", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ ruleId }),
|
||||
body: JSON.stringify({ stampId }),
|
||||
});
|
||||
await fetchData();
|
||||
};
|
||||
|
||||
const handleRedeemClick = () => {
|
||||
const handleStampClick = (stamp: StampWithStatus) => {
|
||||
if (!user) {
|
||||
setShowRegister(true);
|
||||
return;
|
||||
}
|
||||
setShowRedeem(true);
|
||||
if (!stamp.collected) return;
|
||||
setSelectedStampId(stamp.id);
|
||||
};
|
||||
|
||||
if (loading || authLoading) {
|
||||
@@ -108,40 +107,18 @@ export default function AlbumPage() {
|
||||
style={{ width: stamps.length ? `${(collectedCount / stamps.length) * 100}%` : "0%" }}
|
||||
/>
|
||||
</div>
|
||||
{collectedCount > 0 && (
|
||||
<p className="mt-3 text-[11px] text-[var(--text-muted)] leading-relaxed">
|
||||
点击已点亮的图章,即可兑换对应奖品
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Stamp Grid */}
|
||||
<div className="px-4 pb-6">
|
||||
<StampGrid stamps={stamps} />
|
||||
<StampGrid stamps={stamps} onStampClick={handleStampClick} />
|
||||
</div>
|
||||
|
||||
{/* Redeem Section */}
|
||||
{rules.length > 0 && (() => {
|
||||
const availableCount = rules.filter((r) => collectedCount >= r.threshold).length;
|
||||
const canRedeem = availableCount > 0;
|
||||
return (
|
||||
<div className="px-6 pb-6">
|
||||
<button
|
||||
onClick={handleRedeemClick}
|
||||
disabled={!canRedeem}
|
||||
className="w-full py-3.5 rounded-xl text-sm font-medium transition-all disabled:cursor-not-allowed flex items-center justify-center gap-2"
|
||||
style={{
|
||||
backgroundColor: canRedeem ? "var(--jade)" : "var(--border-muted)",
|
||||
color: canRedeem ? "white" : "var(--text-muted)",
|
||||
boxShadow: canRedeem ? "0 2px 8px rgba(45,106,79,0.25)" : "none",
|
||||
}}
|
||||
>
|
||||
<span>{canRedeem ? "兑换奖品" : "继续收集以解锁奖品"}</span>
|
||||
{canRedeem && (
|
||||
<span className="text-xs px-2 py-0.5 rounded-full bg-white/20">
|
||||
{availableCount} 个可兑换
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})()}
|
||||
|
||||
{/* Redemption History */}
|
||||
{history.length > 0 && (
|
||||
<div className="px-6 pb-8">
|
||||
@@ -149,13 +126,13 @@ export default function AlbumPage() {
|
||||
<div className="space-y-2">
|
||||
{history.map((r) => (
|
||||
<div key={r.id} className="flex items-center justify-between py-2 px-3 bg-white/60 rounded-lg">
|
||||
<div>
|
||||
<p className="text-sm text-[var(--text-primary)]">{r.ruleName}</p>
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm text-[var(--text-primary)] truncate">{r.prizeName}</p>
|
||||
<p className="text-xs text-[var(--text-muted)]">
|
||||
{new Date(r.redeemedAt).toLocaleDateString("zh-CN")}
|
||||
{r.stampName} · {new Date(r.redeemedAt).toLocaleDateString("zh-CN")}
|
||||
</p>
|
||||
</div>
|
||||
<span className="text-xs text-[var(--jade)]">已兑换</span>
|
||||
<span className="text-xs text-[var(--jade)] shrink-0 ml-3">已兑换</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
@@ -163,12 +140,11 @@ export default function AlbumPage() {
|
||||
)}
|
||||
|
||||
{/* Modals */}
|
||||
{showRedeem && (
|
||||
{selectedStamp && (
|
||||
<RedeemModal
|
||||
rules={rules}
|
||||
collectedCount={collectedCount}
|
||||
stamp={selectedStamp}
|
||||
onRedeem={handleRedeem}
|
||||
onClose={() => setShowRedeem(false)}
|
||||
onClose={() => setSelectedStampId(null)}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user