Files
18xchat/assets/Scripts/chat18x/payment/PaymentUtil.ts
T
2025-08-27 14:30:30 +08:00

38 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 支付/订单相关工具
*/
import type { OrderStatus } from 'db://assets/Scripts/chat18x/payment/types';
/**
* 将数值状态映射为前端字符串状态
* @param status CSQueryOrderRes.status-1=失败,0=成功,1=处理中,其它=未知
* @param options 可选标记(若你的业务在别处判定了取消/过期)
* @returns OrderStatus
*/
export function mapQueryOrderStatus(
status: number | null | undefined,
options: { canceled?: boolean; expired?: boolean } = {}
): OrderStatus {
const { canceled = false, expired = false } = options;
if (canceled) return "CANCELED";
if (expired) return "EXPIRED";
switch (status) {
case 1: return "PENDING"; // 处理中
case 0: return "SUCCESS"; // 成功
case -1: return "FAILED"; // 失败
default: return "CREATED"; // 未知/初始,按“已创建”兜底
}
}
/** 是否为终态(无需再轮询) */
export function isTerminalStatus(s: OrderStatus): boolean {
return s === "SUCCESS" || s === "FAILED" || s === "CANCELED" || s === "EXPIRED";
}
/** 是否应继续轮询查询订单 */
export function shouldPoll(s: OrderStatus): boolean {
return s === "CREATED" || s === "PENDING";
}