完善支付跳转功能

This commit is contained in:
yezian 2025-03-17 19:37:50 +08:00
parent 083c3569e2
commit e6faca4702
1 changed files with 36 additions and 75 deletions

View File

@ -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>
);