refactor: 清理未使用IPC命令、修正point_service注释与扣费逻辑、修复camelToSnake正则、优化vidu import
- 删除8个未使用IPC命令,保留validate_media_path - file.rs返回类型优化为ApiResponse<()> - point_service.consume()注释与签名一致 - VideoGeneration改为拼接成功后扣费 - 添加漏扣费风险注释 - 删除过时测试文件 - 修复camelToSnake连续大写字母问题 - vidu.py import移至模块顶层 Refs: P1-1~P1-6 技术债务清理
This commit is contained in:
@@ -23,8 +23,6 @@ from app.schemas.point import (
|
||||
PointBalanceResponse,
|
||||
PointTransactionItem,
|
||||
PointTransactionListResponse,
|
||||
RechargeOrderItem,
|
||||
RechargeOrderListResponse,
|
||||
RechargeRequest,
|
||||
RechargeResponse,
|
||||
)
|
||||
@@ -134,7 +132,7 @@ async def create_recharge_order(
|
||||
request: RechargeRequest,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
|
||||
http_request: Request = None,
|
||||
):
|
||||
"""
|
||||
创建积分充值订单(微信支付 Native 扫码)
|
||||
@@ -175,6 +173,16 @@ async def create_recharge_order(
|
||||
beijing_tz = timezone(timedelta(hours=8))
|
||||
time_expire = expire_at.astimezone(beijing_tz).strftime("%Y%m%d%H%M%S")
|
||||
|
||||
# 获取客户端真实 IP(优先从 Nginx 转发头读取)
|
||||
client_ip = None
|
||||
if http_request:
|
||||
xff = http_request.headers.get("x-forwarded-for")
|
||||
if xff:
|
||||
client_ip = xff.split(",")[0].strip()
|
||||
else:
|
||||
xri = http_request.headers.get("x-real-ip")
|
||||
client_ip = xri or (http_request.client.host if http_request.client else None)
|
||||
|
||||
# 调用微信支付统一下单
|
||||
try:
|
||||
from app.services.wxpay_service import WechatPayError, get_wxpay_service
|
||||
@@ -191,6 +199,7 @@ async def create_recharge_order(
|
||||
amount=request.amount_rmb,
|
||||
attach=str(current_user.id),
|
||||
time_expire=time_expire,
|
||||
client_ip=client_ip,
|
||||
)
|
||||
|
||||
# 记录微信返回
|
||||
@@ -486,55 +495,6 @@ async def get_points_rules(
|
||||
|
||||
# ── 积分预估查询 ──────────────────────────────────────
|
||||
|
||||
@router.get("/cost")
|
||||
async def get_cost(
|
||||
source_type: str,
|
||||
seconds: int = 0,
|
||||
char_count: int = 0,
|
||||
total_seconds: int = 0,
|
||||
input_seconds: int = 0,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
查询某操作所需积分(预估上限和实际计费规则),并附带余额检查。
|
||||
|
||||
用于前端在执行业务前预估所需积分,做余额检查。
|
||||
"""
|
||||
try:
|
||||
# 构建预估参数
|
||||
estimate_param = {}
|
||||
if source_type == "tts":
|
||||
estimate_param["char_count"] = char_count
|
||||
elif source_type in ("video", "caption"):
|
||||
estimate_param["input_seconds"] = input_seconds
|
||||
elif source_type == "compose":
|
||||
estimate_param["total_seconds"] = total_seconds
|
||||
|
||||
estimated = point_service._estimate_max_cost(source_type, estimate_param)
|
||||
|
||||
# 实际计费(按传入秒数计算)
|
||||
actual_param = {"seconds": seconds}
|
||||
actual = point_service._calculate_cost(source_type, actual_param)
|
||||
|
||||
# 余额检查
|
||||
balance_info = await point_service.check_balance(
|
||||
db, current_user.id, required_points=estimated
|
||||
)
|
||||
|
||||
return success_response(
|
||||
data={
|
||||
"source_type": source_type,
|
||||
"estimated_points": estimated,
|
||||
"actual_points": actual,
|
||||
"sufficient": balance_info["sufficient"],
|
||||
"balance": balance_info["balance"],
|
||||
"required": balance_info["required"],
|
||||
},
|
||||
message="积分预估查询成功",
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
# ── 直接消费扣费(前端/Rust 层调用)───────────────────
|
||||
@@ -563,7 +523,7 @@ async def consume_points(
|
||||
source_type=request.source_type,
|
||||
source_id=request.source_id,
|
||||
description=f"【{request.description or request.source_type}】",
|
||||
allow_negative=False,
|
||||
allow_negative=request.allow_negative,
|
||||
)
|
||||
except ValueError as e:
|
||||
# 余额不足(在同一事务内判断,避免竞态)
|
||||
@@ -583,31 +543,3 @@ async def consume_points(
|
||||
)
|
||||
|
||||
|
||||
# ── 充值订单查询 ──────────────────────────────────────
|
||||
|
||||
@router.get("/orders", response_model=ApiResponse[RechargeOrderListResponse])
|
||||
async def list_recharge_orders(
|
||||
pagination: PaginationParams = Depends(),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""获取当前用户充值订单列表"""
|
||||
items = await point_recharge_order.get_by_user_id(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
skip=pagination.offset,
|
||||
limit=pagination.page_size,
|
||||
)
|
||||
|
||||
total = len(items)
|
||||
|
||||
return success_response(
|
||||
data=RechargeOrderListResponse(
|
||||
items=[RechargeOrderItem.model_validate(o) for o in items],
|
||||
total=total,
|
||||
skip=pagination.offset,
|
||||
limit=pagination.page_size,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user