's'
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
/**
|
||||
* 微信 H5 支付完成后可能不跳转,通过 cookie 轮询检测支付状态
|
||||
* 参考 digital usePayStatusPoll
|
||||
* 支付状态轮询:点击支付后轮询,支付成功刷新,超时重置
|
||||
* 优先使用 sessionStorage(pay_pending_order),其次 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;
|
||||
const MAX_POLLS = 160; // ~5 分钟
|
||||
const MAX_AGE_MS = 15 * 60 * 1000;
|
||||
|
||||
function getCookie(name: string): string | null {
|
||||
@@ -20,6 +22,29 @@ 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("|");
|
||||
@@ -31,6 +56,19 @@ 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 {
|
||||
const [polling, setPolling] = useState(false);
|
||||
const onPaidRef = useRef(onPaid);
|
||||
@@ -39,9 +77,9 @@ export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?:
|
||||
onTimeoutRef.current = onTimeout;
|
||||
|
||||
useEffect(() => {
|
||||
const raw = getCookie(COOKIE_NAME);
|
||||
const [orderId, isValid] = parsePayOrderCookie(raw);
|
||||
if (!orderId || !isValid) {
|
||||
const [orderId] = getOrderIdToPoll();
|
||||
if (!orderId) {
|
||||
const raw = getCookie(COOKIE_NAME);
|
||||
if (raw) clearCookie(COOKIE_NAME);
|
||||
return;
|
||||
}
|
||||
@@ -52,6 +90,7 @@ export function usePayStatusPoll(onPaid: (orderId?: string) => void, onTimeout?:
|
||||
count++;
|
||||
if (count > MAX_POLLS) {
|
||||
clearInterval(timer);
|
||||
clearSessionOrder();
|
||||
clearCookie(COOKIE_NAME);
|
||||
setPolling(false);
|
||||
onTimeoutRef.current?.();
|
||||
@@ -62,6 +101,7 @@ 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);
|
||||
|
||||
Reference in New Issue
Block a user