完善支付跳转功能
This commit is contained in:
parent
083c3569e2
commit
e6faca4702
|
@ -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 (
|
||||
<div className="flex flex-col items-center">
|
||||
<div
|
||||
|
@ -139,29 +104,25 @@ function SuccessContent() {
|
|||
</h2>
|
||||
|
||||
<p className="mt-4 text-lg text-center text-gray-300">
|
||||
{redirectUrl ? "即将返回应用..." : "请关闭当前页面返回应用"}
|
||||
{redirectUrl
|
||||
? "即将返回应用..."
|
||||
: hasHistory
|
||||
? "即将返回上一页..."
|
||||
: "请点击下方按钮关闭页面"}
|
||||
</p>
|
||||
|
||||
{closeAttempted && !redirectUrl && (
|
||||
{closeAttempted && !redirectUrl && !hasHistory && (
|
||||
<p className="mt-2 text-sm text-center text-gray-400">
|
||||
{getCloseInstructions()}
|
||||
请手动关闭此页面并返回应用
|
||||
</p>
|
||||
)}
|
||||
|
||||
{process.env.NODE_ENV === "development" && (
|
||||
<p className="mt-2 text-xs text-center text-gray-500">{historyInfo}</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
onClick={handleClose}
|
||||
onClick={handleButtonClick}
|
||||
className="flex items-center justify-center w-full mt-8 px-4 py-3 text-white bg-[#FF669E] rounded-md hover:bg-[#FF4A8E] transition-colors"
|
||||
>
|
||||
<FiArrowLeft className="w-5 h-5 mr-2" />
|
||||
{redirectUrl
|
||||
? "立即返回"
|
||||
: window.history.length > 1
|
||||
? "返回上一页"
|
||||
: "关闭页面"}
|
||||
{redirectUrl ? "立即返回" : hasHistory ? "返回应用" : "关闭页面"}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue