完善支付跳转功能

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() { function SuccessContent() {
const [closeAttempted, setCloseAttempted] = useState(false); const [closeAttempted, setCloseAttempted] = useState(false);
const [autoNavigationAttempted, setAutoNavigationAttempted] = useState(false);
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const redirectUrl = searchParams.get("redirect_url"); const redirectUrl = searchParams.get("redirect_url");
const hasHistory = typeof window !== "undefined" && window.history.length > 1;
// //
useEffect(() => { useEffect(() => {
@ -23,6 +25,18 @@ function SuccessContent() {
} }
}, [redirectUrl]); }, [redirectUrl]);
//
useEffect(() => {
// URL
if (redirectUrl) return;
//
if (hasHistory && !autoNavigationAttempted) {
setAutoNavigationAttempted(true);
window.history.go(-2);
}
}, [hasHistory, redirectUrl, autoNavigationAttempted]);
// //
useEffect(() => { useEffect(() => {
const timer = setTimeout(() => { const timer = setTimeout(() => {
@ -36,64 +50,30 @@ function SuccessContent() {
return () => clearTimeout(timer); return () => clearTimeout(timer);
}, []); }, []);
// //
const handleClose = () => { const handleButtonClick = () => {
// URL // URL
if (redirectUrl) { if (redirectUrl) {
window.location.href = redirectUrl; window.location.href = redirectUrl;
return; return;
} }
//
if (hasHistory) {
//
window.history.go(-2);
return;
}
//
try { try {
// window.close();
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);
// //
setTimeout(() => { setTimeout(() => {
//
if (document.visibilityState !== "hidden") { if (document.visibilityState !== "hidden") {
setCloseAttempted(true); setCloseAttempted(true);
// toast toast.error("请手动关闭此页面", {
toast.error("请手动关闭此页面并返回应用", {
icon: "👋", icon: "👋",
duration: 5000, duration: 5000,
style: { style: {
@ -105,26 +85,11 @@ function SuccessContent() {
} }
}, 300); }, 300);
} catch (error) { } catch (error) {
console.error("关闭页面失败:", error);
setCloseAttempted(true); 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 ( return (
<div className="flex flex-col items-center"> <div className="flex flex-col items-center">
<div <div
@ -139,29 +104,25 @@ function SuccessContent() {
</h2> </h2>
<p className="mt-4 text-lg text-center text-gray-300"> <p className="mt-4 text-lg text-center text-gray-300">
{redirectUrl ? "即将返回应用..." : "请关闭当前页面返回应用"} {redirectUrl
? "即将返回应用..."
: hasHistory
? "即将返回上一页..."
: "请点击下方按钮关闭页面"}
</p> </p>
{closeAttempted && !redirectUrl && ( {closeAttempted && !redirectUrl && !hasHistory && (
<p className="mt-2 text-sm text-center text-gray-400"> <p className="mt-2 text-sm text-center text-gray-400">
{getCloseInstructions()} 请手动关闭此页面并返回应用
</p> </p>
)} )}
{process.env.NODE_ENV === "development" && (
<p className="mt-2 text-xs text-center text-gray-500">{historyInfo}</p>
)}
<button <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" 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" /> <FiArrowLeft className="w-5 h-5 mr-2" />
{redirectUrl {redirectUrl ? "立即返回" : hasHistory ? "返回应用" : "关闭页面"}
? "立即返回"
: window.history.length > 1
? "返回上一页"
: "关闭页面"}
</button> </button>
</div> </div>
); );