"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { tryCompleteOrder, applyCompleteSuccess } from "@/app/lib/complete-order"; import { clearPendingOrder } from "@/app/hooks/useCompleteOrderPolling"; const WECHAT_STORAGE_KEY = "salon_join_wechat"; export default function JoinPaidPage() { const [completeStatus, setCompleteStatus] = useState<"pending" | "success" | "error">("pending"); const [errorDetail, setErrorDetail] = useState(null); const [countdown, setCountdown] = useState(null); const [wechatId, setWechatId] = useState(null); useEffect(() => { let cancelled = false; const run = async () => { const urlOrderId = typeof window !== "undefined" ? new URLSearchParams(window.location.search).get("order_id") || "" : ""; const cookie = document.cookie.split(";").find((c) => c.trim().startsWith("join_pay_order=")); const rawValue = cookie?.split("=")[1]?.trim(); const storageValue = (() => { try { return localStorage.getItem("join_pay_order") || ""; } catch { return ""; } })(); const decoded = rawValue ? decodeURIComponent(rawValue) : storageValue; const orderId = urlOrderId || decoded.split("|")[0]?.trim() || ""; if (!orderId) { clearPendingOrder(); window.location.replace("/join"); return; } const maxClientRetries = 5; const retryDelays = [800, 1500, 2500, 4000, 6000]; let lastError = ""; for (let i = 0; i <= maxClientRetries; i++) { if (cancelled) return; try { const result = await tryCompleteOrder(orderId); if (result.ok) { if (result.token && result.record) { applyCompleteSuccess({ token: result.token, record: result.record }); } else if (result.record) { applyCompleteSuccess({ token: "", record: result.record }); } clearPendingOrder(); if (!cancelled) { setCompleteStatus("success"); if (result.record?.wechatId) setWechatId(result.record.wechatId); } return; } lastError = result.error || "unknown"; } catch (e) { lastError = e instanceof Error ? e.message : "network error"; } if (i < maxClientRetries) { await new Promise((r) => setTimeout(r, retryDelays[i])); } } if (!cancelled) { setCompleteStatus("error"); setErrorDetail(lastError); } }; run(); return () => { cancelled = true; }; }, []); useEffect(() => { try { const stored = localStorage.getItem(WECHAT_STORAGE_KEY)?.trim(); if (stored) setWechatId(stored); } catch { /* ignore */ } }, []); const COUNTDOWN_SECONDS = 5; useEffect(() => { if (completeStatus !== "success") return; setCountdown(COUNTDOWN_SECONDS); const timer = setInterval(() => { setCountdown((prev) => { if (prev == null || prev <= 1) { clearInterval(timer); window.location.replace("/join?paid=1"); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(timer); }, [completeStatus]); return (
✅

{completeStatus === "pending" ? "处理中…" : completeStatus === "success" ? "支付成功" : "处理异常"}

{completeStatus === "success" ? countdown != null ? `${countdown} 秒后跳转报名成功页` : "感谢您的支持" : completeStatus === "error" ? "请稍后刷新页面重试" : "正在确认支付状态…"}

{wechatId && (

微信号: {wechatId}

)} {completeStatus === "error" && errorDetail && (

{errorDetail}

)} {completeStatus === "error" && ( )} {completeStatus === "pending" ? ( ← 查看报名状态 ) : ( ← 查看报名状态 )}
); }