From e6faca47027649b2b43aebeb8f0230f057ce4161 Mon Sep 17 00:00:00 2001 From: yezian Date: Mon, 17 Mar 2025 19:37:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=94=AF=E4=BB=98=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/success/page.jsx | 111 ++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 75 deletions(-) diff --git a/app/success/page.jsx b/app/success/page.jsx index eaf653f..9f36a88 100644 --- a/app/success/page.jsx +++ b/app/success/page.jsx @@ -8,8 +8,10 @@ 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(() => { @@ -23,6 +25,18 @@ function SuccessContent() { } }, [redirectUrl]); + // 自动检查历史记录并尝试返回 + useEffect(() => { + // 如果有重定向URL,不执行自动返回 + if (redirectUrl) return; + + // 如果有历史记录,自动尝试返回两级 + if (hasHistory && !autoNavigationAttempted) { + setAutoNavigationAttempted(true); + window.history.go(-2); + } + }, [hasHistory, redirectUrl, autoNavigationAttempted]); + // 页面加载时的动画效果 useEffect(() => { const timer = setTimeout(() => { @@ -36,64 +50,30 @@ function SuccessContent() { return () => clearTimeout(timer); }, []); - // 尝试关闭页面 - const handleClose = () => { + // 处理按钮点击 + const handleButtonClick = () => { // 如果有重定向URL,直接跳转 if (redirectUrl) { window.location.href = redirectUrl; return; } + // 检查历史记录状态 + if (hasHistory) { + // 如果有历史记录,往前跳转两级 + window.history.go(-2); + return; + } + + // 如果没有历史记录,尝试关闭窗口 try { - // 检查历史记录状态 - const historyLength = window.history.length; - console.log(`历史记录长度: ${historyLength}`); - - if (historyLength > 1) { - console.log("检测到历史记录,尝试返回上一页"); - // 如果有历史记录,往前跳转两级 - window.history.go(-2); - - // 设置一个短暂的延迟,检查是否成功跳转 - setTimeout(() => { - // 如果页面仍然存在,可能跳转失败,尝试其他方法 - if (document.visibilityState !== "hidden") { - console.log("返回上一页可能失败,尝试返回一级"); - window.history.back(); - } - }, 200); - - return; - } - - console.log("无历史记录,尝试关闭窗口"); - // 如果没有历史记录,尝试关闭窗口 - // 尝试多种方法关闭窗口 - const isOpener = window.opener && !window.opener.closed; - - if (isOpener) { - // 如果是从另一个窗口打开的,尝试通知父窗口 - try { - // 只有在同源的情况下才能访问 opener - window.opener.focus(); - console.log("尝试聚焦父窗口"); - } catch (e) { - console.log("无法访问opener,可能是跨域限制", e); - } - } - - // 尝试关闭窗口 - console.log("尝试关闭窗口"); - const closingAttempt = window.close(); - console.log("关闭窗口结果:", closingAttempt); + window.close(); // 设置一个短暂的延迟,检查页面是否仍然打开 setTimeout(() => { - // 如果页面仍然打开,说明关闭失败 if (document.visibilityState !== "hidden") { setCloseAttempted(true); - // 显示toast提示 - toast.error("请手动关闭此页面并返回应用", { + toast.error("请手动关闭此页面", { icon: "👋", duration: 5000, style: { @@ -105,26 +85,11 @@ function SuccessContent() { } }, 300); } catch (error) { - console.error("关闭页面失败:", error); setCloseAttempted(true); - toast.error("请手动关闭此页面返回应用"); + toast.error("请手动关闭此页面"); } }; - // 获取关闭页面的提示文本 - const getCloseInstructions = () => { - const hasHistory = window.history.length > 1; - - if (hasHistory) { - return "将返回之前的页面"; - } else { - return "请点击浏览器右上角的 × 关闭此页面"; - } - }; - - // 用于调试的历史记录信息 - const historyInfo = `历史记录长度: ${window.history.length}`; - return (

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

- {closeAttempted && !redirectUrl && ( + {closeAttempted && !redirectUrl && !hasHistory && (

- {getCloseInstructions()} + 请手动关闭此页面并返回应用

)} - {process.env.NODE_ENV === "development" && ( -

{historyInfo}

- )} -
);