- 新增 Channel / SubscriptionPlan / SystemConfig 三个数据模型 - Order 模型扩展支持订阅订单(order_type, plan_id, subscription_group_id) - Sub2API client 新增分组查询、订阅分配/续期、用户订阅查询 - 订单服务支持订阅履约流程(CAS 锁 + 分组消失安全处理) - 管理后台:渠道管理、订阅套餐管理、系统配置、Sub2API 分组同步 - 用户页面:双 Tab UI(按量付费/包月订阅)、渠道卡片、充值弹窗、订阅确认 - PaymentForm 支持 fixedAmount 固定金额模式 - 订单状态 API 返回 failedReason 用于订阅异常展示 - 数据库迁移脚本
154 lines
4.2 KiB
TypeScript
154 lines
4.2 KiB
TypeScript
import { ORDER_STATUS, REFUND_STATUSES } from '@/lib/constants';
|
|
|
|
export type RechargeStatus = 'not_paid' | 'paid_pending' | 'recharging' | 'success' | 'failed' | 'closed';
|
|
|
|
export interface OrderStatusLike {
|
|
status: string;
|
|
paidAt?: Date | string | null;
|
|
completedAt?: Date | string | null;
|
|
}
|
|
|
|
export interface DerivedOrderState {
|
|
paymentSuccess: boolean;
|
|
rechargeSuccess: boolean;
|
|
rechargeStatus: RechargeStatus;
|
|
}
|
|
|
|
export interface PublicOrderStatusSnapshot extends DerivedOrderState {
|
|
id: string;
|
|
status: string;
|
|
expiresAt: Date | string;
|
|
failedReason?: string | null;
|
|
}
|
|
|
|
export interface OrderDisplayState {
|
|
label: string;
|
|
color: string;
|
|
icon: string;
|
|
message: string;
|
|
}
|
|
|
|
const CLOSED_STATUSES = new Set<string>([
|
|
ORDER_STATUS.EXPIRED,
|
|
ORDER_STATUS.CANCELLED,
|
|
ORDER_STATUS.REFUNDING,
|
|
ORDER_STATUS.REFUNDED,
|
|
ORDER_STATUS.REFUND_FAILED,
|
|
]);
|
|
|
|
function hasDate(value: Date | string | null | undefined): boolean {
|
|
return Boolean(value);
|
|
}
|
|
|
|
export function isRefundStatus(status: string): boolean {
|
|
return REFUND_STATUSES.has(status);
|
|
}
|
|
|
|
export function isRechargeRetryable(order: OrderStatusLike): boolean {
|
|
return hasDate(order.paidAt) && order.status === ORDER_STATUS.FAILED && !isRefundStatus(order.status);
|
|
}
|
|
|
|
export function deriveOrderState(order: OrderStatusLike): DerivedOrderState {
|
|
const paymentSuccess = hasDate(order.paidAt);
|
|
const rechargeSuccess = hasDate(order.completedAt) || order.status === ORDER_STATUS.COMPLETED;
|
|
|
|
if (rechargeSuccess) {
|
|
return { paymentSuccess, rechargeSuccess: true, rechargeStatus: 'success' };
|
|
}
|
|
|
|
if (order.status === ORDER_STATUS.RECHARGING) {
|
|
return { paymentSuccess, rechargeSuccess: false, rechargeStatus: 'recharging' };
|
|
}
|
|
|
|
if (order.status === ORDER_STATUS.FAILED) {
|
|
return { paymentSuccess, rechargeSuccess: false, rechargeStatus: 'failed' };
|
|
}
|
|
|
|
if (CLOSED_STATUSES.has(order.status)) {
|
|
return { paymentSuccess, rechargeSuccess: false, rechargeStatus: 'closed' };
|
|
}
|
|
|
|
if (paymentSuccess) {
|
|
return { paymentSuccess, rechargeSuccess: false, rechargeStatus: 'paid_pending' };
|
|
}
|
|
|
|
return { paymentSuccess: false, rechargeSuccess: false, rechargeStatus: 'not_paid' };
|
|
}
|
|
|
|
export function getOrderDisplayState(
|
|
order: Pick<PublicOrderStatusSnapshot, 'status' | 'paymentSuccess' | 'rechargeSuccess' | 'rechargeStatus'>,
|
|
): OrderDisplayState {
|
|
if (order.rechargeSuccess || order.rechargeStatus === 'success') {
|
|
return {
|
|
label: '充值成功',
|
|
color: 'text-green-600',
|
|
icon: '✓',
|
|
message: '余额已到账,感谢您的充值!',
|
|
};
|
|
}
|
|
|
|
if (order.paymentSuccess) {
|
|
if (order.rechargeStatus === 'paid_pending' || order.rechargeStatus === 'recharging') {
|
|
return {
|
|
label: '充值中',
|
|
color: 'text-blue-600',
|
|
icon: '⟳',
|
|
message: '支付成功,正在充值余额中,请稍候...',
|
|
};
|
|
}
|
|
|
|
if (order.rechargeStatus === 'failed') {
|
|
return {
|
|
label: '支付成功',
|
|
color: 'text-amber-600',
|
|
icon: '!',
|
|
message:
|
|
'支付已完成,但余额充值暂未完成。系统可能会自动重试,请稍后在订单列表查看;如长时间未到账请联系管理员。',
|
|
};
|
|
}
|
|
}
|
|
|
|
if (order.status === ORDER_STATUS.FAILED) {
|
|
return {
|
|
label: '支付失败',
|
|
color: 'text-red-600',
|
|
icon: '✗',
|
|
message: '支付未完成,请重新发起支付;如已扣款未到账,请联系管理员处理。',
|
|
};
|
|
}
|
|
|
|
if (order.status === ORDER_STATUS.PENDING) {
|
|
return {
|
|
label: '等待支付',
|
|
color: 'text-yellow-600',
|
|
icon: '⏳',
|
|
message: '订单尚未完成支付。',
|
|
};
|
|
}
|
|
|
|
if (order.status === ORDER_STATUS.EXPIRED) {
|
|
return {
|
|
label: '订单超时',
|
|
color: 'text-gray-500',
|
|
icon: '⏰',
|
|
message: '订单已超时,请重新创建订单。',
|
|
};
|
|
}
|
|
|
|
if (order.status === ORDER_STATUS.CANCELLED) {
|
|
return {
|
|
label: '已取消',
|
|
color: 'text-gray-500',
|
|
icon: '✗',
|
|
message: '订单已取消。',
|
|
};
|
|
}
|
|
|
|
return {
|
|
label: '支付异常',
|
|
color: 'text-red-600',
|
|
icon: '✗',
|
|
message: '支付状态异常,请联系管理员处理。',
|
|
};
|
|
}
|