feat: 渠道展示、订阅套餐、系统配置全功能
- 新增 Channel / SubscriptionPlan / SystemConfig 三个数据模型 - Order 模型扩展支持订阅订单(order_type, plan_id, subscription_group_id) - Sub2API client 新增分组查询、订阅分配/续期、用户订阅查询 - 订单服务支持订阅履约流程(CAS 锁 + 分组消失安全处理) - 管理后台:渠道管理、订阅套餐管理、系统配置、Sub2API 分组同步 - 用户页面:双 Tab UI(按量付费/包月订阅)、渠道卡片、充值弹窗、订阅确认 - PaymentForm 支持 fixedAmount 固定金额模式 - 订单状态 API 返回 failedReason 用于订阅异常展示 - 数据库迁移脚本
This commit is contained in:
@@ -6,7 +6,7 @@ import { getMethodDailyLimit } from './limits';
|
||||
import { getMethodFeeRate, calculatePayAmount } from './fee';
|
||||
import { initPaymentProviders, paymentRegistry } from '@/lib/payment';
|
||||
import type { PaymentType, PaymentNotification } from '@/lib/payment';
|
||||
import { getUser, createAndRedeem, subtractBalance, addBalance } from '@/lib/sub2api/client';
|
||||
import { getUser, createAndRedeem, subtractBalance, addBalance, getGroup, assignSubscription } from '@/lib/sub2api/client';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { deriveOrderState, isRefundStatus } from './status';
|
||||
import { pickLocaleText, type Locale } from '@/lib/locale';
|
||||
@@ -28,6 +28,9 @@ export interface CreateOrderInput {
|
||||
srcHost?: string;
|
||||
srcUrl?: string;
|
||||
locale?: Locale;
|
||||
// 订阅订单专用
|
||||
orderType?: 'balance' | 'subscription';
|
||||
planId?: string;
|
||||
}
|
||||
|
||||
export interface CreateOrderResult {
|
||||
@@ -50,6 +53,31 @@ export async function createOrder(input: CreateOrderInput): Promise<CreateOrderR
|
||||
const env = getEnv();
|
||||
const locale = input.locale ?? 'zh';
|
||||
const todayStart = getBizDayStartUTC();
|
||||
const orderType = input.orderType ?? 'balance';
|
||||
|
||||
// ── 订阅订单前置校验 ──
|
||||
let subscriptionPlan: { id: string; groupId: number; price: Prisma.Decimal; validityDays: number; name: string } | null = null;
|
||||
if (orderType === 'subscription') {
|
||||
if (!input.planId) {
|
||||
throw new OrderError('INVALID_INPUT', message(locale, '订阅订单必须指定套餐', 'Subscription order requires a plan'), 400);
|
||||
}
|
||||
const plan = await prisma.subscriptionPlan.findUnique({ where: { id: input.planId } });
|
||||
if (!plan || !plan.forSale) {
|
||||
throw new OrderError('PLAN_NOT_AVAILABLE', message(locale, '该套餐不存在或未上架', 'Plan not found or not for sale'), 404);
|
||||
}
|
||||
// 校验 Sub2API 分组仍然存在
|
||||
const group = await getGroup(plan.groupId);
|
||||
if (!group || group.status !== 'active') {
|
||||
throw new OrderError(
|
||||
'GROUP_NOT_FOUND',
|
||||
message(locale, '订阅分组已下架,无法购买', 'Subscription group is no longer available'),
|
||||
410,
|
||||
);
|
||||
}
|
||||
subscriptionPlan = plan;
|
||||
// 订阅订单金额使用服务端套餐价格,不信任客户端
|
||||
input.amount = Number(plan.price);
|
||||
}
|
||||
|
||||
const user = await getUser(input.userId);
|
||||
if (user.status !== 'active') {
|
||||
@@ -149,6 +177,10 @@ export async function createOrder(input: CreateOrderInput): Promise<CreateOrderR
|
||||
clientIp: input.clientIp,
|
||||
srcHost: input.srcHost || null,
|
||||
srcUrl: input.srcUrl || null,
|
||||
orderType,
|
||||
planId: subscriptionPlan?.id ?? null,
|
||||
subscriptionGroupId: subscriptionPlan?.groupId ?? null,
|
||||
subscriptionDays: subscriptionPlan?.validityDays ?? null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -200,7 +232,13 @@ export async function createOrder(input: CreateOrderInput): Promise<CreateOrderR
|
||||
data: {
|
||||
orderId: order.id,
|
||||
action: 'ORDER_CREATED',
|
||||
detail: JSON.stringify({ userId: input.userId, amount: input.amount, paymentType: input.paymentType }),
|
||||
detail: JSON.stringify({
|
||||
userId: input.userId,
|
||||
amount: input.amount,
|
||||
paymentType: input.paymentType,
|
||||
orderType,
|
||||
...(subscriptionPlan && { planId: subscriptionPlan.id, planName: subscriptionPlan.name, groupId: subscriptionPlan.groupId }),
|
||||
}),
|
||||
operator: `user:${input.userId}`,
|
||||
},
|
||||
});
|
||||
@@ -453,10 +491,10 @@ export async function confirmPayment(input: {
|
||||
// FAILED 状态 — 之前充值失败,利用重试通知自动重试充值
|
||||
if (current.status === ORDER_STATUS.FAILED) {
|
||||
try {
|
||||
await executeRecharge(order.id);
|
||||
await executeFulfillment(order.id);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error('Recharge retry failed for order:', order.id, err);
|
||||
console.error('Fulfillment retry failed for order:', order.id, err);
|
||||
return false; // 让支付平台继续重试
|
||||
}
|
||||
}
|
||||
@@ -485,9 +523,9 @@ export async function confirmPayment(input: {
|
||||
});
|
||||
|
||||
try {
|
||||
await executeRecharge(order.id);
|
||||
await executeFulfillment(order.id);
|
||||
} catch (err) {
|
||||
console.error('Recharge failed for order:', order.id, err);
|
||||
console.error('Fulfillment failed for order:', order.id, err);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -512,6 +550,107 @@ export async function handlePaymentNotify(notification: PaymentNotification, pro
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一履约入口 — 根据 orderType 分派到余额充值或订阅分配。
|
||||
*/
|
||||
export async function executeFulfillment(orderId: string): Promise<void> {
|
||||
const order = await prisma.order.findUnique({
|
||||
where: { id: orderId },
|
||||
select: { orderType: true },
|
||||
});
|
||||
if (!order) throw new OrderError('NOT_FOUND', 'Order not found', 404);
|
||||
|
||||
if (order.orderType === 'subscription') {
|
||||
await executeSubscriptionFulfillment(orderId);
|
||||
} else {
|
||||
await executeRecharge(orderId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅履约 — 支付成功后调用 Sub2API 分配订阅。
|
||||
*/
|
||||
export async function executeSubscriptionFulfillment(orderId: string): Promise<void> {
|
||||
const order = await prisma.order.findUnique({ where: { id: orderId } });
|
||||
if (!order) throw new OrderError('NOT_FOUND', 'Order not found', 404);
|
||||
if (order.status === ORDER_STATUS.COMPLETED) return;
|
||||
if (isRefundStatus(order.status)) {
|
||||
throw new OrderError('INVALID_STATUS', 'Refund-related order cannot fulfill', 400);
|
||||
}
|
||||
if (order.status !== ORDER_STATUS.PAID && order.status !== ORDER_STATUS.FAILED) {
|
||||
throw new OrderError('INVALID_STATUS', `Order cannot fulfill in status ${order.status}`, 400);
|
||||
}
|
||||
if (!order.subscriptionGroupId || !order.subscriptionDays) {
|
||||
throw new OrderError('INVALID_STATUS', 'Missing subscription info on order', 400);
|
||||
}
|
||||
|
||||
// CAS 锁
|
||||
const lockResult = await prisma.order.updateMany({
|
||||
where: { id: orderId, status: { in: [ORDER_STATUS.PAID, ORDER_STATUS.FAILED] } },
|
||||
data: { status: ORDER_STATUS.RECHARGING },
|
||||
});
|
||||
if (lockResult.count === 0) return;
|
||||
|
||||
try {
|
||||
// 校验分组是否仍然存在
|
||||
const group = await getGroup(order.subscriptionGroupId);
|
||||
if (!group || group.status !== 'active') {
|
||||
throw new Error(`Subscription group ${order.subscriptionGroupId} no longer exists or inactive`);
|
||||
}
|
||||
|
||||
await assignSubscription(
|
||||
order.userId,
|
||||
order.subscriptionGroupId,
|
||||
order.subscriptionDays,
|
||||
`sub2apipay subscription order:${orderId}`,
|
||||
`sub2apipay:subscription:${order.rechargeCode}`,
|
||||
);
|
||||
|
||||
await prisma.order.updateMany({
|
||||
where: { id: orderId, status: ORDER_STATUS.RECHARGING },
|
||||
data: { status: ORDER_STATUS.COMPLETED, completedAt: new Date() },
|
||||
});
|
||||
|
||||
await prisma.auditLog.create({
|
||||
data: {
|
||||
orderId,
|
||||
action: 'SUBSCRIPTION_SUCCESS',
|
||||
detail: JSON.stringify({
|
||||
groupId: order.subscriptionGroupId,
|
||||
days: order.subscriptionDays,
|
||||
amount: Number(order.amount),
|
||||
}),
|
||||
operator: 'system',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error);
|
||||
const isGroupGone = reason.includes('no longer exists');
|
||||
|
||||
await prisma.order.update({
|
||||
where: { id: orderId },
|
||||
data: {
|
||||
status: ORDER_STATUS.FAILED,
|
||||
failedAt: new Date(),
|
||||
failedReason: isGroupGone
|
||||
? `SUBSCRIPTION_GROUP_GONE: ${reason}`
|
||||
: reason,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.auditLog.create({
|
||||
data: {
|
||||
orderId,
|
||||
action: 'SUBSCRIPTION_FAILED',
|
||||
detail: reason,
|
||||
operator: 'system',
|
||||
},
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeRecharge(orderId: string): Promise<void> {
|
||||
const order = await prisma.order.findUnique({ where: { id: orderId } });
|
||||
if (!order) {
|
||||
@@ -698,7 +837,7 @@ export async function retryRecharge(orderId: string, locale: Locale = 'zh'): Pro
|
||||
},
|
||||
});
|
||||
|
||||
await executeRecharge(orderId);
|
||||
await executeFulfillment(orderId);
|
||||
}
|
||||
|
||||
export interface RefundInput {
|
||||
|
||||
@@ -18,6 +18,7 @@ export interface PublicOrderStatusSnapshot extends DerivedOrderState {
|
||||
id: string;
|
||||
status: string;
|
||||
expiresAt: Date | string;
|
||||
failedReason?: string | null;
|
||||
}
|
||||
|
||||
export interface OrderDisplayState {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { getEnv } from '@/lib/config';
|
||||
import type { Sub2ApiUser, Sub2ApiRedeemCode } from './types';
|
||||
import type { Sub2ApiUser, Sub2ApiRedeemCode, Sub2ApiGroup, Sub2ApiSubscription } from './types';
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 10_000;
|
||||
const RECHARGE_TIMEOUT_MS = 30_000;
|
||||
@@ -101,6 +101,103 @@ export async function createAndRedeem(
|
||||
throw lastError instanceof Error ? lastError : new Error('Recharge failed');
|
||||
}
|
||||
|
||||
// ── 分组 API ──
|
||||
|
||||
export async function getAllGroups(): Promise<Sub2ApiGroup[]> {
|
||||
const env = getEnv();
|
||||
const response = await fetch(`${env.SUB2API_BASE_URL}/api/v1/admin/groups/all`, {
|
||||
headers: getHeaders(),
|
||||
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get groups: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return (data.data ?? []) as Sub2ApiGroup[];
|
||||
}
|
||||
|
||||
export async function getGroup(groupId: number): Promise<Sub2ApiGroup | null> {
|
||||
const env = getEnv();
|
||||
const response = await fetch(`${env.SUB2API_BASE_URL}/api/v1/admin/groups/${groupId}`, {
|
||||
headers: getHeaders(),
|
||||
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) return null;
|
||||
throw new Error(`Failed to get group ${groupId}: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.data as Sub2ApiGroup;
|
||||
}
|
||||
|
||||
// ── 订阅 API ──
|
||||
|
||||
export async function assignSubscription(
|
||||
userId: number,
|
||||
groupId: number,
|
||||
validityDays: number,
|
||||
notes?: string,
|
||||
idempotencyKey?: string,
|
||||
): Promise<Sub2ApiSubscription> {
|
||||
const env = getEnv();
|
||||
const response = await fetch(`${env.SUB2API_BASE_URL}/api/v1/admin/subscriptions/assign`, {
|
||||
method: 'POST',
|
||||
headers: getHeaders(idempotencyKey),
|
||||
body: JSON.stringify({
|
||||
user_id: userId,
|
||||
group_id: groupId,
|
||||
validity_days: validityDays,
|
||||
notes: notes || `Sub2ApiPay subscription order`,
|
||||
}),
|
||||
signal: AbortSignal.timeout(RECHARGE_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(`Assign subscription failed (${response.status}): ${JSON.stringify(errorData)}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.data as Sub2ApiSubscription;
|
||||
}
|
||||
|
||||
export async function getUserSubscriptions(userId: number): Promise<Sub2ApiSubscription[]> {
|
||||
const env = getEnv();
|
||||
const response = await fetch(`${env.SUB2API_BASE_URL}/api/v1/admin/users/${userId}/subscriptions`, {
|
||||
headers: getHeaders(),
|
||||
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) return [];
|
||||
throw new Error(`Failed to get user subscriptions: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return (data.data ?? []) as Sub2ApiSubscription[];
|
||||
}
|
||||
|
||||
export async function extendSubscription(subscriptionId: number, days: number): Promise<void> {
|
||||
const env = getEnv();
|
||||
const response = await fetch(`${env.SUB2API_BASE_URL}/api/v1/admin/subscriptions/${subscriptionId}/extend`, {
|
||||
method: 'POST',
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify({ days }),
|
||||
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(`Extend subscription failed (${response.status}): ${JSON.stringify(errorData)}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ── 余额 API ──
|
||||
|
||||
export async function subtractBalance(
|
||||
userId: number,
|
||||
amount: number,
|
||||
|
||||
@@ -22,3 +22,43 @@ export interface Sub2ApiResponse<T> {
|
||||
data?: T;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
// ── 分组 ──
|
||||
|
||||
export interface Sub2ApiGroup {
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
platform: string;
|
||||
status: string;
|
||||
rate_multiplier: number;
|
||||
subscription_type: string; // "standard" | "subscription"
|
||||
daily_limit_usd: number | null;
|
||||
weekly_limit_usd: number | null;
|
||||
monthly_limit_usd: number | null;
|
||||
default_validity_days: number;
|
||||
sort_order: number;
|
||||
supported_model_scopes: string[] | null;
|
||||
}
|
||||
|
||||
// ── 订阅 ──
|
||||
|
||||
export interface Sub2ApiSubscription {
|
||||
id: number;
|
||||
user_id: number;
|
||||
group_id: number;
|
||||
starts_at: string;
|
||||
expires_at: string;
|
||||
status: string; // "active" | "expired" | "suspended"
|
||||
daily_usage_usd: number;
|
||||
weekly_usage_usd: number;
|
||||
monthly_usage_usd: number;
|
||||
daily_window_start: string | null;
|
||||
weekly_window_start: string | null;
|
||||
monthly_window_start: string | null;
|
||||
assigned_by: number;
|
||||
assigned_at: string;
|
||||
notes: string | null;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
119
src/lib/system-config.ts
Normal file
119
src/lib/system-config.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { prisma } from '@/lib/db';
|
||||
|
||||
// 内存缓存:key → { value, expiresAt }
|
||||
const cache = new Map<string, { value: string; expiresAt: number }>();
|
||||
const CACHE_TTL_MS = 30_000; // 30 秒
|
||||
|
||||
function getCached(key: string): string | undefined {
|
||||
const entry = cache.get(key);
|
||||
if (!entry) return undefined;
|
||||
if (Date.now() > entry.expiresAt) {
|
||||
cache.delete(key);
|
||||
return undefined;
|
||||
}
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
function setCache(key: string, value: string): void {
|
||||
cache.set(key, { value, expiresAt: Date.now() + CACHE_TTL_MS });
|
||||
}
|
||||
|
||||
export function invalidateConfigCache(key?: string): void {
|
||||
if (key) {
|
||||
cache.delete(key);
|
||||
} else {
|
||||
cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSystemConfig(key: string): Promise<string | undefined> {
|
||||
const cached = getCached(key);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
const row = await prisma.systemConfig.findUnique({ where: { key } });
|
||||
if (row) {
|
||||
setCache(key, row.value);
|
||||
return row.value;
|
||||
}
|
||||
|
||||
// 回退到环境变量
|
||||
const envVal = process.env[key];
|
||||
if (envVal !== undefined) {
|
||||
setCache(key, envVal);
|
||||
}
|
||||
return envVal;
|
||||
}
|
||||
|
||||
export async function getSystemConfigs(keys: string[]): Promise<Record<string, string>> {
|
||||
const result: Record<string, string> = {};
|
||||
const missing: string[] = [];
|
||||
|
||||
for (const key of keys) {
|
||||
const cached = getCached(key);
|
||||
if (cached !== undefined) {
|
||||
result[key] = cached;
|
||||
} else {
|
||||
missing.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
if (missing.length > 0) {
|
||||
const rows = await prisma.systemConfig.findMany({
|
||||
where: { key: { in: missing } },
|
||||
});
|
||||
|
||||
const dbMap = new Map(rows.map((r) => [r.key, r.value]));
|
||||
|
||||
for (const key of missing) {
|
||||
const val = dbMap.get(key) ?? process.env[key];
|
||||
if (val !== undefined) {
|
||||
result[key] = val;
|
||||
setCache(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function setSystemConfig(key: string, value: string, group?: string, label?: string): Promise<void> {
|
||||
await prisma.systemConfig.upsert({
|
||||
where: { key },
|
||||
update: { value, ...(group !== undefined && { group }), ...(label !== undefined && { label }) },
|
||||
create: { key, value, group: group ?? 'general', label },
|
||||
});
|
||||
invalidateConfigCache(key);
|
||||
}
|
||||
|
||||
export async function setSystemConfigs(configs: { key: string; value: string; group?: string; label?: string }[]): Promise<void> {
|
||||
await prisma.$transaction(
|
||||
configs.map((c) =>
|
||||
prisma.systemConfig.upsert({
|
||||
where: { key: c.key },
|
||||
update: { value: c.value, ...(c.group !== undefined && { group: c.group }), ...(c.label !== undefined && { label: c.label }) },
|
||||
create: { key: c.key, value: c.value, group: c.group ?? 'general', label: c.label },
|
||||
}),
|
||||
),
|
||||
);
|
||||
invalidateConfigCache();
|
||||
}
|
||||
|
||||
export async function getSystemConfigsByGroup(group: string): Promise<{ key: string; value: string; label: string | null }[]> {
|
||||
return prisma.systemConfig.findMany({
|
||||
where: { group },
|
||||
select: { key: true, value: true, label: true },
|
||||
orderBy: { key: 'asc' },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllSystemConfigs(): Promise<{ key: string; value: string; group: string; label: string | null }[]> {
|
||||
return prisma.systemConfig.findMany({
|
||||
select: { key: true, value: true, group: true, label: true },
|
||||
orderBy: [{ group: 'asc' }, { key: 'asc' }],
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSystemConfig(key: string): Promise<void> {
|
||||
await prisma.systemConfig.delete({ where: { key } }).catch(() => {});
|
||||
invalidateConfigCache(key);
|
||||
}
|
||||
Reference in New Issue
Block a user