diff --git a/src/app/pay/result/page.tsx b/src/app/pay/result/page.tsx
index daf0c96..a7c2d14 100644
--- a/src/app/pay/result/page.tsx
+++ b/src/app/pay/result/page.tsx
@@ -15,6 +15,7 @@ function ResultContent() {
const [status, setStatus] = useState(null);
const [loading, setLoading] = useState(true);
const [isInPopup, setIsInPopup] = useState(false);
+ const [countdown, setCountdown] = useState(5);
// Detect if opened as a popup window (from stripe-popup or via popup=1 param)
useEffect(() => {
@@ -53,16 +54,35 @@ function ResultContent() {
};
}, [outTradeNo]);
- // Auto-close popup window on success
const isSuccess = status === 'COMPLETED' || status === 'PAID' || status === 'RECHARGING';
- useEffect(() => {
- if (!isInPopup || !isSuccess) return;
- const timer = setTimeout(() => {
+ const goBack = () => {
+ if (isInPopup) {
window.close();
- }, 3000);
- return () => clearTimeout(timer);
- }, [isInPopup, isSuccess]);
+ } else if (window.history.length > 1) {
+ window.history.back();
+ } else {
+ window.close();
+ }
+ };
+
+ // Countdown auto-return on success
+ useEffect(() => {
+ if (!isSuccess) return;
+ setCountdown(5);
+ const timer = setInterval(() => {
+ setCountdown((prev) => {
+ if (prev <= 1) {
+ clearInterval(timer);
+ goBack();
+ return 0;
+ }
+ return prev - 1;
+ });
+ }, 1000);
+ return () => clearInterval(timer);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [isSuccess, isInPopup]);
if (loading) {
return (
@@ -91,35 +111,31 @@ function ResultContent() {
{status === 'COMPLETED' ? '余额已成功到账!' : '支付成功,余额正在充值中...'}
- {isInPopup && (
-
-
- 此窗口将在 3 秒后自动关闭
-
-
-
- )}
+
+
+ {countdown > 0 ? `${countdown} 秒后自动返回` : '正在返回...'}
+
+
+
>
) : isPending ? (
<>
⏳
等待支付
订单尚未完成支付
- {isInPopup && (
-
- )}
+
>
) : (
<>
@@ -134,15 +150,13 @@ function ResultContent() {
? '订单已被取消'
: '请联系管理员处理'}
- {isInPopup && (
-
- )}
+
>
)}