;'s'
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
/**
|
||||
* 支付状态轮询:点击支付后轮询,支付成功刷新,超时重置
|
||||
* 优先使用 sessionStorage(pay_pending_order),其次 cookie
|
||||
* 支付状态轮询
|
||||
* - 手机 H5:传入 orderId(点击支付后由 state 传入),直接轮询,不依赖 sessionStorage/cookie
|
||||
* - PC/微信:不传 orderId,使用 cookie 轮询(兼容旧逻辑)
|
||||
*/
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
const COOKIE_NAME = "nomadvip_pay_order";
|
||||
const SESSION_KEY_ORDER = "pay_pending_order";
|
||||
const SESSION_KEY_TS = "pay_pending_ts";
|
||||
const POLL_INTERVAL = 2000;
|
||||
const MAX_POLLS = 160; // ~5 分钟
|
||||
const MAX_AGE_MS = 15 * 60 * 1000;
|
||||
@@ -22,29 +21,6 @@ function clearCookie(name: string): void {
|
||||
document.cookie = `${name}=; path=/; max-age=0`;
|
||||
}
|
||||
|
||||
function getSessionOrder(): [string | null, boolean] {
|
||||
if (typeof window === "undefined") return [null, false];
|
||||
const orderId = sessionStorage.getItem(SESSION_KEY_ORDER)?.trim() || null;
|
||||
const ts = sessionStorage.getItem(SESSION_KEY_TS);
|
||||
const timestamp = ts ? parseInt(ts, 10) : NaN;
|
||||
if (!orderId) return [null, false];
|
||||
if (Number.isNaN(timestamp)) return [orderId, true];
|
||||
if (Date.now() - timestamp > MAX_AGE_MS) return [orderId, false];
|
||||
return [orderId, true];
|
||||
}
|
||||
|
||||
function setSessionOrder(orderId: string): void {
|
||||
if (typeof window === "undefined") return;
|
||||
sessionStorage.setItem(SESSION_KEY_ORDER, orderId);
|
||||
sessionStorage.setItem(SESSION_KEY_TS, String(Date.now()));
|
||||
}
|
||||
|
||||
function clearSessionOrder(): void {
|
||||
if (typeof window === "undefined") return;
|
||||
sessionStorage.removeItem(SESSION_KEY_ORDER);
|
||||
sessionStorage.removeItem(SESSION_KEY_TS);
|
||||
}
|
||||
|
||||
function parsePayOrderCookie(raw: string | null): [string | null, boolean] {
|
||||
if (!raw?.trim()) return [null, false];
|
||||
const parts = raw.split("|");
|
||||
@@ -56,20 +32,12 @@ function parsePayOrderCookie(raw: string | null): [string | null, boolean] {
|
||||
return [orderId, true];
|
||||
}
|
||||
|
||||
function getOrderIdToPoll(): [string | null, "session" | "cookie" | null] {
|
||||
const [sessionOrder, sessionValid] = getSessionOrder();
|
||||
if (sessionOrder && sessionValid) return [sessionOrder, "session"];
|
||||
const raw = getCookie(COOKIE_NAME);
|
||||
const [cookieOrder, cookieValid] = parsePayOrderCookie(raw);
|
||||
if (cookieOrder && cookieValid) return [cookieOrder, "cookie"];
|
||||
return [null, null];
|
||||
}
|
||||
|
||||
export function setPayPendingOrder(orderId: string): void {
|
||||
setSessionOrder(orderId);
|
||||
}
|
||||
|
||||
export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?: () => void): boolean {
|
||||
export function usePayStatusPoll(
|
||||
onPaid: (orderId?: string) => void,
|
||||
onTimeout?: () => void,
|
||||
/** 手机 H5:点击支付后传入的 orderId,直接轮询,不依赖 sessionStorage/cookie */
|
||||
orderIdFromState?: string | null
|
||||
): boolean {
|
||||
const [polling, setPolling] = useState(false);
|
||||
const onPaidRef = useRef(onPaid);
|
||||
const onTimeoutRef = useRef(onTimeout);
|
||||
@@ -77,10 +45,16 @@ export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?:
|
||||
onTimeoutRef.current = onTimeout;
|
||||
|
||||
useEffect(() => {
|
||||
const [orderId] = getOrderIdToPoll();
|
||||
const orderId =
|
||||
orderIdFromState !== undefined
|
||||
? (orderIdFromState?.trim() || null)
|
||||
: (() => {
|
||||
const raw = getCookie(COOKIE_NAME);
|
||||
const [oid, valid] = parsePayOrderCookie(raw);
|
||||
return valid ? oid : null;
|
||||
})();
|
||||
if (!orderId) {
|
||||
const raw = getCookie(COOKIE_NAME);
|
||||
if (raw) clearCookie(COOKIE_NAME);
|
||||
if (orderIdFromState === undefined && getCookie(COOKIE_NAME)) clearCookie(COOKIE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -90,7 +64,6 @@ export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?:
|
||||
count++;
|
||||
if (count > MAX_POLLS) {
|
||||
clearInterval(timer);
|
||||
clearSessionOrder();
|
||||
clearCookie(COOKIE_NAME);
|
||||
setPolling(false);
|
||||
onTimeoutRef.current?.();
|
||||
@@ -101,7 +74,6 @@ export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?:
|
||||
const data = await res.json();
|
||||
if (data?.ok && data?.paid) {
|
||||
clearInterval(timer);
|
||||
clearSessionOrder();
|
||||
clearCookie(COOKIE_NAME);
|
||||
setPolling(false);
|
||||
onPaidRef.current(orderId);
|
||||
@@ -112,7 +84,7 @@ export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?:
|
||||
}, POLL_INTERVAL);
|
||||
|
||||
return () => clearInterval(timer);
|
||||
}, []);
|
||||
}, [orderIdFromState]);
|
||||
|
||||
return polling;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user