"use client"; import { useEffect, useState, Suspense } from "react"; import { FiArrowLeft, FiCheckCircle } from "react-icons/fi"; import toast from "react-hot-toast"; import { useSearchParams } from "next/navigation"; // 创建一个客户端组件来处理搜索参数 function SuccessContent() { const [closeAttempted, setCloseAttempted] = useState(false); const [autoNavigationAttempted, setAutoNavigationAttempted] = useState(false); const searchParams = useSearchParams(); const redirectUrl = searchParams.get("redirect_url"); const hasHistory = typeof window !== "undefined" && window.history.length > 1; // 处理重定向 useEffect(() => { if (redirectUrl) { // 添加一个小延迟,让成功动画有时间显示 const redirectTimer = setTimeout(() => { window.location.href = redirectUrl; }, 1500); return () => clearTimeout(redirectTimer); } }, [redirectUrl]); // 自动检查历史记录并尝试返回 useEffect(() => { // 如果有重定向URL,不执行自动返回 if (redirectUrl) return; // 如果有历史记录,自动尝试返回两级 if (hasHistory && !autoNavigationAttempted) { setAutoNavigationAttempted(true); window.history.go(-2); } }, [hasHistory, redirectUrl, autoNavigationAttempted]); // 页面加载时的动画效果 useEffect(() => { const timer = setTimeout(() => { const successIcon = document.getElementById("success-icon"); if (successIcon) { successIcon.classList.add("scale-100"); successIcon.classList.remove("scale-0"); } }, 100); return () => clearTimeout(timer); }, []); // 处理按钮点击 const handleButtonClick = () => { // 如果有重定向URL,直接跳转 if (redirectUrl) { window.location.href = redirectUrl; return; } // 检查历史记录状态 if (hasHistory) { // 如果有历史记录,往前跳转两级 window.history.go(-2); return; } // 如果没有历史记录,尝试关闭窗口 try { window.close(); // 设置一个短暂的延迟,检查页面是否仍然打开 setTimeout(() => { if (document.visibilityState !== "hidden") { setCloseAttempted(true); toast.error("请手动关闭此页面", { icon: "👋", duration: 5000, style: { background: "#333", color: "#fff", borderRadius: "8px", }, }); } }, 300); } catch (error) { setCloseAttempted(true); toast.error("请手动关闭此页面"); } }; return (

支付成功

{redirectUrl ? "即将返回应用..." : hasHistory ? "即将返回上一页..." : "请点击下方按钮关闭页面"}

{closeAttempted && !redirectUrl && !hasHistory && (

请手动关闭此页面并返回应用

)}
); } // 主页面组件,包含Suspense边界 export default function SuccessPage() { return (
加载中...
} >
); }