This commit is contained in:
eric
2026-03-10 08:31:24 -05:00
parent 095a7fb260
commit 053482e9f3
4 changed files with 77 additions and 38 deletions

View File

@@ -1,16 +1,30 @@
"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { usePayStatusPoll } from "@/app/lib/usePayStatusPoll";
const COOKIE_NAME = "nomadvip_pay_order";
function getCookie(name: string): string | null {
if (typeof document === "undefined") return null;
const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`));
return match ? decodeURIComponent(match[1]) : null;
}
function parseUserIdFromOrderId(orderId: string): string {
if (!orderId?.includes("_order_")) return "";
const prefix = orderId.split("_order_")[0];
const parts = prefix.split("_");
return parts.length >= 2 ? parts.slice(0, -1).join("_") : "";
}
function SuccessWithRedirect() {
const router = useRouter();
useEffect(() => {
const t = setTimeout(() => router.replace("/"), 2000);
const t = setTimeout(() => {
window.location.href = "/";
}, 2000);
return () => clearTimeout(t);
}, [router]);
}, []);
return (
<div className="flex min-h-screen flex-col items-center justify-center bg-[#f0f4f8] px-4">
<div className="w-full max-w-md rounded-2xl bg-white p-10 text-center shadow-lg">
@@ -20,12 +34,13 @@ function SuccessWithRedirect() {
<h2 className="text-2xl font-bold text-slate-800">支付成功</h2>
<p className="mt-3 text-slate-500">感谢您的支持,VIP 权益已开通</p>
<p className="mt-2 text-xs text-slate-400">2 秒后自动返回首页</p>
<Link
<a
href="/"
onClick={(e) => { e.preventDefault(); window.location.href = "/"; }}
className="mt-8 inline-flex items-center gap-2 rounded-full bg-sky-500 px-8 py-3 font-semibold text-white transition-colors hover:bg-sky-600"
>
← 立即返回首页
</Link>
</a>
</div>
</div>
);
@@ -35,8 +50,19 @@ export default function PaidPage() {
const [status, setStatus] = useState<"pending" | "success" | "fail">("pending");
const [fromUrl, setFromUrl] = useState<string | null>(null);
const router = useRouter();
usePayStatusPoll(() => setStatus("success"), () => router.replace("/"));
usePayStatusPoll((orderId) => {
const uid = parseUserIdFromOrderId(orderId || "");
const raw = getCookie(COOKIE_NAME);
const oid = raw?.split("|")[0]?.trim() || orderId || "";
const userId = uid || parseUserIdFromOrderId(oid);
if (userId) {
try {
localStorage.setItem("paidType", "vip");
localStorage.setItem("userName", userId);
} catch {}
}
setStatus("success");
}, () => { window.location.href = "/"; });
useEffect(() => {
const params = new URLSearchParams(window.location.search);
@@ -78,12 +104,13 @@ export default function PaidPage() {
</div>
<h2 className="text-2xl font-bold text-slate-800">支付未完成</h2>
<p className="mt-3 text-slate-500">支付已取消或未成功,请重试</p>
<Link
<a
href="/"
onClick={(e) => { e.preventDefault(); window.location.href = "/"; }}
className="mt-8 inline-flex items-center gap-2 rounded-full border-2 border-slate-300 px-8 py-3 font-semibold text-slate-700 transition-colors hover:border-slate-400 hover:bg-slate-50"
>
← 返回首页
</Link>
</a>
</div>
</div>
);