feat: credit-based billing system
- New DB models: credit_packages, subscription_plans, user_credits, credit_consumptions, credit_purchases - CreditService: balance, deduct, add_credits, grant_free_trial, history - User API: /api/v1/credits/* (balance/history/packages/purchase/subscribe) - Admin API: /api/v1/admin/credit-* (CRUD packages/plans, user credits, consumptions) - PaymentService.create_credit_order + handle_callback for credit purchases - Credit deduction on: discovery, translate, marketing, ai_chat, followup - Free trial 30 credits on registration - Documentation: docs/CREDIT_SYSTEM.md
This commit is contained in:
@@ -9,6 +9,7 @@ from app.ai.local_faq import match_faq
|
||||
from app.api.v1.deps import get_current_user_id
|
||||
from app.models.system_config import SystemConfig
|
||||
from app.services.admin import AdminService
|
||||
from app.services.credit import CreditService
|
||||
import logging
|
||||
import time
|
||||
import re
|
||||
@@ -108,6 +109,14 @@ async def chat(
|
||||
f"db={t1-t0:.2f}s orm={t2-t1:.2f}s total={t4-t_start:.2f}s"
|
||||
)
|
||||
else:
|
||||
credit_svc = CreditService(db)
|
||||
ok, balance = await credit_svc.deduct(user_id, "ai_chat")
|
||||
if not ok:
|
||||
raise HTTPException(
|
||||
status_code=402,
|
||||
detail=f"次数不足 (剩余 {balance:.1f})"
|
||||
)
|
||||
|
||||
t3 = time.time()
|
||||
ai = get_ai_router()
|
||||
result = await ai.chat(data.message, data.history or [], system_prompt)
|
||||
|
||||
Reference in New Issue
Block a user