feat: 支付渠道每日限额(渠道维度全平台统计)
- config.ts:新增 MAX_DAILY_AMOUNT_ALIPAY/WXPAY/STRIPE(默认 alipay/wxpay 各 1w,stripe 不限) - lib/order/limits.ts:getMethodDailyLimit + queryMethodLimits 共用工具,支持动态渠道兜底 - order/service.ts:createOrder 校验渠道限额,超限抛 METHOD_DAILY_LIMIT_EXCEEDED - api/limits/route.ts:公开接口 GET /api/limits,返回各渠道今日用量/剩余/是否可用 - api/user/route.ts:config 响应中加入 methodLimits,前端一次请求即可获取限额状态 - PaymentForm.tsx:额度已满的渠道置灰 + 标注「今日额度已满」,无法选择 - pay/page.tsx:AppConfig 加 methodLimits,传给 PaymentForm,新增错误码映射
This commit is contained in:
31
src/app/api/limits/route.ts
Normal file
31
src/app/api/limits/route.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getEnv } from '@/lib/config';
|
||||
import { queryMethodLimits } from '@/lib/order/limits';
|
||||
|
||||
/**
|
||||
* GET /api/limits
|
||||
* 返回各支付渠道今日限额使用情况,公开接口(无需鉴权)。
|
||||
*
|
||||
* Response:
|
||||
* {
|
||||
* methods: {
|
||||
* alipay: { dailyLimit: 10000, used: 3500, remaining: 6500, available: true },
|
||||
* wxpay: { dailyLimit: 10000, used: 10000, remaining: 0, available: false },
|
||||
* stripe: { dailyLimit: 0, used: 500, remaining: null, available: true }
|
||||
* },
|
||||
* resetAt: "2026-03-02T00:00:00.000Z" // UTC 次日零点(限额重置时间)
|
||||
* }
|
||||
*/
|
||||
export async function GET() {
|
||||
const env = getEnv();
|
||||
const types = env.ENABLED_PAYMENT_TYPES;
|
||||
|
||||
const todayStart = new Date();
|
||||
todayStart.setUTCHours(0, 0, 0, 0);
|
||||
const resetAt = new Date(todayStart);
|
||||
resetAt.setUTCDate(resetAt.getUTCDate() + 1);
|
||||
|
||||
const methods = await queryMethodLimits(types);
|
||||
|
||||
return NextResponse.json({ methods, resetAt });
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getUser } from '@/lib/sub2api/client';
|
||||
import { getEnv } from '@/lib/config';
|
||||
import { queryMethodLimits } from '@/lib/order/limits';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const userId = Number(request.nextUrl.searchParams.get('user_id'));
|
||||
@@ -10,7 +11,10 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
try {
|
||||
const env = getEnv();
|
||||
const user = await getUser(userId);
|
||||
const [user, methodLimits] = await Promise.all([
|
||||
getUser(userId),
|
||||
queryMethodLimits(env.ENABLED_PAYMENT_TYPES),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
user: {
|
||||
@@ -22,6 +26,7 @@ export async function GET(request: NextRequest) {
|
||||
minAmount: env.MIN_RECHARGE_AMOUNT,
|
||||
maxAmount: env.MAX_RECHARGE_AMOUNT,
|
||||
maxDailyAmount: env.MAX_DAILY_RECHARGE_AMOUNT,
|
||||
methodLimits,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user