feat(wallet): 钱包系统替代配额系统,新增支付与退款机制

- 新增钱包余额管理、充值、扣费、退款完整流程
- 新增支付网关抽象层(支持手动/支付宝/微信)
- 用量计费从配额系统迁移到钱包余额扣费
- 新增管理员钱包管理与支付订单管理页面
- 新增用户钱包中心页面
- 移除独立 Key 锁定机制,统一由钱包余额控制
- 新增相关 API 路由、序列化器与数据库迁移
- 新增钱包、支付、退款相关测试
This commit is contained in:
LewisPen
2026-03-08 00:05:48 +08:00
committed by fawney19
parent 9cdcce1b5f
commit 783f654953
108 changed files with 13152 additions and 3372 deletions

View File

@@ -11,6 +11,7 @@ from sqlalchemy.orm import Session
from src.core.logger import logger
from src.services.auth.service import AuthService
from src.services.usage.service import UsageService
from src.services.wallet import WalletService
from .base import AuthContext, AuthPlugin
@@ -62,8 +63,13 @@ class ApiKeyAuthPlugin(AuthPlugin):
user, api_key_obj = auth_result
# 检查用户配额或独立Key余额
quota_ok, message = UsageService.check_user_quota(db, user, api_key=api_key_obj)
# 检查用户或独立 Key 的钱包余额可用性
access_ok, message = UsageService.check_request_balance(db, user, api_key=api_key_obj)
billing_wallet = (
WalletService.get_wallet(db, api_key_id=api_key_obj.id)
if api_key_obj.is_standalone
else WalletService.get_wallet(db, user_id=user.id)
)
# 创建认证上下文
auth_context = AuthContext(
@@ -72,15 +78,13 @@ class ApiKeyAuthPlugin(AuthPlugin):
api_key_id=api_key_obj.id,
api_key_name=api_key_obj.name if hasattr(api_key_obj, "name") else None,
permissions={
"can_use_api": quota_ok,
"can_use_api": access_ok,
"is_admin": user.is_admin if hasattr(user, "is_admin") else False,
"is_standalone_key": api_key_obj.is_standalone, # 标记是否为独立余额Key
},
quota_info={
"quota_usd": user.quota_usd,
"used_usd": user.used_usd,
"remaining_usd": None if user.quota_usd is None else user.quota_usd - user.used_usd,
"quota_ok": quota_ok,
billing_info={
"billing": WalletService.serialize_wallet_summary(billing_wallet),
"balance_ok": access_ok,
"message": message,
},
metadata={

View File

@@ -27,7 +27,7 @@ class AuthContext:
api_key_id: int | None = None
api_key_name: str | None = None
permissions: dict[str, bool] = None
quota_info: dict[str, Any] = None
billing_info: dict[str, Any] = None
metadata: dict[str, Any] = None
def __post_init__(self) -> None:

View File

@@ -13,6 +13,7 @@ from sqlalchemy.orm import Session
from src.core.logger import logger
from src.models.database import User
from src.services.auth.service import AuthService
from src.services.wallet import WalletService
from .base import AuthContext, AuthPlugin
@@ -82,18 +83,22 @@ class JwtAuthPlugin(AuthPlugin):
logger.warning("JWT认证失败 - Token身份校验失败")
return None
wallet_access = WalletService.check_request_allowed(db, user=user, api_key=None)
# 创建认证上下文
auth_context = AuthContext(
user_id=user.id,
user_name=user.username,
permissions={"can_use_api": True, "is_admin": user.role.value == "admin"},
quota_info={
"quota_usd": user.quota_usd,
"used_usd": user.used_usd,
"remaining_usd": (
None if user.quota_usd is None else user.quota_usd - user.used_usd
permissions={
"can_use_api": wallet_access.allowed,
"is_admin": user.role.value == "admin",
},
billing_info={
"billing": WalletService.serialize_wallet_summary(
WalletService.get_wallet(db, user_id=user.id)
),
"quota_ok": True, # JWT用户通常已经通过前端验证
"balance_ok": wallet_access.allowed,
"message": wallet_access.message,
},
metadata={
"auth_method": "jwt",