"use client"; import { useState, useRef, useEffect, type FormEvent, type ChangeEvent } from "react"; import { Link } from "@/i18n/navigation"; import { getPayEnv, redirectToPay, getDeviceFromEnv, } from "@/app/lib/payment"; import { usePayStatusPoll } from "@/app/lib/payment/usePayStatusPoll"; import AuthModal from "@/app/components/AuthModal"; import { apiFetch, fetchAuthMe } from "@/app/lib/api-client"; import { cities } from "@/app/data/cities"; const genderOptions = ["男", "女"]; const singleOptions = ["单身", "恋爱中", "已婚"]; const lookingForOptions = [ { key: "friends", label: "🤝 交友" }, { key: "dating", label: "🌹 约会" }, { key: "partner", label: "👥 搭档" }, { key: "roommate", label: "🏠 室友" }, { key: "cofounder", label: "🚀 合伙人" }, { key: "explore", label: "🍜 探店" }, ]; const educationOptions = ["高中及以下", "大专", "本科", "硕士", "博士"]; const graduationYears = Array.from({ length: 41 }, (_, i) => String(2026 - i)); interface FormData { nickname: string; gender: string; single: string; profession: string; intro: string; wechat: string; education: string; graduationYear: string; phone: string; location: string; citySlug: string; } interface MediaInfo { preview: string; url: string; type: "image" | "video"; } export default function JoinPage() { const [form, setForm] = useState({ nickname: "", gender: "", single: "", profession: "", intro: "", wechat: "", education: "", graduationYear: "", phone: "", location: "", citySlug: "", }); const [lookingFor, setLookingFor] = useState(["friends", "explore"]); const [media, setMedia] = useState(null); const [uploading, setUploading] = useState(false); const fileRef = useRef(null); const [submitted, setSubmitted] = useState(false); const [isVipSubmit, setIsVipSubmit] = useState(false); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const [payRedirecting, setPayRedirecting] = useState(false); const [authOpen, setAuthOpen] = useState(false); const [pendingSubmit, setPendingSubmit] = useState(false); const [isAlreadyRecorded, setIsAlreadyRecorded] = useState(false); useEffect(() => { if (typeof window !== "undefined" && new URLSearchParams(window.location.search).get("paid") === "1") { setSubmitted(true); return; } const checkExisting = async () => { try { const meData = await fetchAuthMe(); if (!meData?.user) return; const statusData = await apiFetch<{ hasRecord?: boolean; isComplete?: boolean }>( "/api/join/status", { cache: "no-store" } ); if (statusData?.hasRecord && statusData?.isComplete) { setIsAlreadyRecorded(true); setSubmitted(true); } } catch { // Keep the form usable when the API proxy/backend is temporarily unavailable. } }; checkExisting(); }, []); // 轮询到支付成功时跳转到 /join/paid,携带 order_id 供 complete-order 使用 usePayStatusPoll((orderId) => { if (typeof window === "undefined") return; const path = window.location.pathname; const paidPath = path.replace(/\/join\/?$/, "") + "/join/paid"; const url = orderId ? `${paidPath}?order_id=${encodeURIComponent(orderId)}` : paidPath; window.location.replace(url); }); const set = (key: keyof FormData, value: string) => setForm((prev) => ({ ...prev, [key]: value })); const doSubmit = async () => { setError(null); setSubmitting(true); try { const data = await apiFetch<{ ok?: boolean; user_id?: string; error?: string }>("/api/join", { method: "POST", body: JSON.stringify({ ...form, media: media?.url, lookingFor, }), }); if (!data?.ok) { throw new Error(data?.error || "提交失败,请稍后重试"); } const userId = data.user_id; if (!userId) { setSubmitted(true); return; } // 再次确认 VIP,防止 handleAuthSuccess 等路径绕过 const vipData2 = await apiFetch<{ vip?: boolean }>("/api/vip/check", { cache: "no-store" }); if (vipData2?.vip) { setIsVipSubmit(true); setSubmitted(true); setSubmitting(false); return; } setSubmitting(false); setPayRedirecting(true); const returnUrl = `${window.location.origin}${window.location.pathname}/paid`; const env = getPayEnv(); const channel = "wxpay"; const device = getDeviceFromEnv(env); redirectToPay({ user_id: userId, return_url: returnUrl, channel, device, useJoinConfig: true, }); return; } catch (err) { setError(err instanceof Error ? err.message : "网络错误,请重试"); } finally { setSubmitting(false); } }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); const meData = await fetchAuthMe(); if (!meData?.user) { setPendingSubmit(true); setAuthOpen(true); return; } const vipData = await apiFetch<{ vip?: boolean }>("/api/vip/check", { cache: "no-store" }); if (vipData?.vip) { setIsVipSubmit(true); } await doSubmit(); }; const handleAuthSuccess = () => { setAuthOpen(false); if (pendingSubmit) { setPendingSubmit(false); doSubmit(); } }; if (submitted) { return (
✅

{isAlreadyRecorded ? "资料已录入" : isVipSubmit ? "提交成功" : "支付成功"}

{isAlreadyRecorded ? "您的报名信息已录入,无需重复提交" : isVipSubmit ? "感谢您的参与" : "感谢您的支持"}

← 返回首页
); } return (
🌍
游牧中国 · 数字游民社区

🌍 数字游民社区报名

{"💼 结识志同道合的伙伴\n📍 线下聚会 + 线上分享\n🤝 互助成长,资源共享"}

set("nickname", e.target.value)} required className="form-input" />
set("profession", e.target.value)} required className="form-input" />
set("wechat", e.target.value)} required className="form-input" /> { const v = e.target.value.replace(/\D/g, "").slice(0, 11); set("phone", v); }} required className="form-input" />
{lookingForOptions.map((option) => { const active = lookingFor.includes(option.key); return ( ); })}