fix: TTS/润色/标题/克隆 API 前置积分检查 + 402 异常透传
- voice.py: synthesize/synthesize-batch/synthesize-file/clone/submit 添加前置积分检查 - script.py: polish/generate_title 添加前置积分检查 - 修复 HTTPException(402) 被外层 except Exception 吞掉变成 500 的问题 - 所有端点在执行业务前先检查余额,不足直接返回 402
This commit is contained in:
@@ -67,6 +67,15 @@ async def polish_content(
|
||||
service = get_script_service()
|
||||
type_name = "画面" if request.polish_type == "scene" else "文案"
|
||||
|
||||
# 前置积分检查
|
||||
required_points = ps._calculate_cost("polish")
|
||||
check = await ps.check_balance(db, current_user.id, required_points)
|
||||
if not check["sufficient"]:
|
||||
raise HTTPException(
|
||||
status_code=402,
|
||||
detail=f"积分不足,需要 {required_points} 积分,当前余额 {check['balance']}",
|
||||
)
|
||||
|
||||
try:
|
||||
polished = await service.polish_content(
|
||||
content=request.content,
|
||||
@@ -90,6 +99,8 @@ async def polish_content(
|
||||
data=polished,
|
||||
message=f"{type_name}润色完成",
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except ValueError as e:
|
||||
if "积分不足" in str(e):
|
||||
raise HTTPException(status_code=402, detail=str(e))
|
||||
@@ -177,6 +188,15 @@ async def generate_title(
|
||||
usage_note=usage_note,
|
||||
)
|
||||
|
||||
# 前置积分检查
|
||||
required_points = ps._calculate_cost("title")
|
||||
check = await ps.check_balance(db, current_user.id, required_points)
|
||||
if not check["sufficient"]:
|
||||
raise HTTPException(
|
||||
status_code=402,
|
||||
detail=f"积分不足,需要 {required_points} 积分,当前余额 {check['balance']}",
|
||||
)
|
||||
|
||||
try:
|
||||
async with asyncio.timeout(15):
|
||||
result = await model_router.generate(
|
||||
@@ -209,6 +229,8 @@ async def generate_title(
|
||||
data=GenerateTitleResponse(title=title),
|
||||
message="标题生成成功",
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except TimeoutError:
|
||||
logger.warning("[generate_title] 标题生成超时")
|
||||
raise HTTPException(status_code=500, detail="标题生成超时,请重试")
|
||||
|
||||
Reference in New Issue
Block a user