feat: Implement core API endpoints for AI content generation, user management, and Alipay payment processing.
This commit is contained in:
parent
1202291e4b
commit
2453bb05ea
@ -95,7 +95,7 @@ def generate():
|
||||
|
||||
# 5. 启动异步生图任务
|
||||
app = current_app._get_current_object()
|
||||
task_id = start_async_image_task(app, user_id, payload, api_key, target_api, cost, data.get('mode'), model_value)
|
||||
task_id = start_async_image_task(app, user_id, payload, api_key, target_api, cost, data.get('mode'), model_value, use_trial)
|
||||
|
||||
return jsonify({
|
||||
"task_id": task_id,
|
||||
|
||||
@ -4,7 +4,7 @@ from models import Order, User, to_bj_time, get_bj_now
|
||||
from services.alipay_service import AlipayService
|
||||
from services.logger import system_logger
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
payment_bp = Blueprint('payment', __name__, url_prefix='/payment')
|
||||
|
||||
@ -102,8 +102,7 @@ def payment_history():
|
||||
)
|
||||
).order_by(Order.created_at.desc()).all()
|
||||
|
||||
import datetime as dt_module
|
||||
return render_template('recharge_history.html', orders=orders, modules={'datetime': dt_module})
|
||||
return render_template('recharge_history.html', orders=orders, modules={'datetime': datetime})
|
||||
|
||||
@payment_bp.route('/api/history', methods=['GET'])
|
||||
def api_payment_history():
|
||||
|
||||
@ -117,7 +117,7 @@ def handle_chat_generation_sync(user_id, api_key, model_value, prompt, use_trial
|
||||
refund_points(user_id, cost)
|
||||
return {"error": str(e)}, 500
|
||||
|
||||
def start_async_image_task(app, user_id, payload, api_key, target_api, cost, mode, model_value):
|
||||
def start_async_image_task(app, user_id, payload, api_key, target_api, cost, mode, model_value, use_trial=False):
|
||||
"""启动异步生图任务"""
|
||||
task_id = str(uuid.uuid4())
|
||||
|
||||
@ -126,7 +126,7 @@ def start_async_image_task(app, user_id, payload, api_key, target_api, cost, mod
|
||||
|
||||
threading.Thread(
|
||||
target=process_image_generation,
|
||||
args=(app, user_id, task_id, payload, api_key, target_api, cost)
|
||||
args=(app, user_id, task_id, payload, api_key, target_api, cost, use_trial)
|
||||
).start()
|
||||
|
||||
return task_id
|
||||
@ -153,7 +153,7 @@ def start_async_video_task(app, user_id, payload, cost, model_value):
|
||||
|
||||
threading.Thread(
|
||||
target=process_video_generation,
|
||||
args=(app, user_id, task_id, payload, api_key, cost)
|
||||
args=(app, user_id, task_id, payload, api_key, cost, True) # 视频目前默认为积分模式
|
||||
).start()
|
||||
|
||||
return task_id
|
||||
|
||||
@ -90,7 +90,7 @@ def sync_images_background(app, record_id, raw_urls):
|
||||
except Exception as e:
|
||||
print(f"❌ 更新记录失败: {e}")
|
||||
|
||||
def process_image_generation(app, user_id, task_id, payload, api_key, target_api, cost):
|
||||
def process_image_generation(app, user_id, task_id, payload, api_key, target_api, cost, use_trial=False):
|
||||
"""异步执行图片生成并存入 Redis"""
|
||||
with app.app_context():
|
||||
try:
|
||||
@ -99,10 +99,9 @@ def process_image_generation(app, user_id, task_id, payload, api_key, target_api
|
||||
resp = requests.post(target_api, json=payload, headers=headers, timeout=1000)
|
||||
|
||||
if resp.status_code != 200:
|
||||
user = db.session.get(User, user_id)
|
||||
if user and "sk-" in api_key:
|
||||
user.points += cost
|
||||
db.session.commit()
|
||||
if use_trial:
|
||||
from services.generation_service import refund_points
|
||||
refund_points(user_id, cost)
|
||||
|
||||
# 记录详细的失败上下文
|
||||
system_logger.error(f"生图任务失败: {resp.text}", user_id=user_id, task_id=task_id, prompt=payload.get('prompt'), model=payload.get('model'))
|
||||
@ -135,10 +134,9 @@ def process_image_generation(app, user_id, task_id, payload, api_key, target_api
|
||||
|
||||
except Exception as e:
|
||||
# 异常处理:退还积分
|
||||
user = db.session.get(User, user_id)
|
||||
if user and "sk-" in api_key:
|
||||
user.points += cost
|
||||
db.session.commit()
|
||||
if use_trial:
|
||||
from services.generation_service import refund_points
|
||||
refund_points(user_id, cost)
|
||||
|
||||
system_logger.error(f"生图任务异常: {str(e)}", user_id=user_id, task_id=task_id, prompt=payload.get('prompt'), model=payload.get('model'))
|
||||
redis_client.setex(f"task:{task_id}", 3600, json.dumps({"status": "error", "message": str(e)}))
|
||||
@ -203,7 +201,7 @@ def sync_video_background(app, record_id, raw_url, internal_task_id=None):
|
||||
except Exception as dbe:
|
||||
system_logger.error(f"更新视频记录失败: {str(dbe)}")
|
||||
|
||||
def process_video_generation(app, user_id, internal_task_id, payload, api_key, cost):
|
||||
def process_video_generation(app, user_id, internal_task_id, payload, api_key, cost, use_trial=True):
|
||||
"""异步提交并查询视频任务状态"""
|
||||
with app.app_context():
|
||||
try:
|
||||
@ -273,13 +271,12 @@ def process_video_generation(app, user_id, internal_task_id, payload, api_key, c
|
||||
except Exception as e:
|
||||
system_logger.error(f"视频生成执行异常: {str(e)}", user_id=user_id, task_id=internal_task_id, prompt=payload.get('prompt'))
|
||||
# 尝试退费
|
||||
try:
|
||||
user = db.session.get(User, user_id)
|
||||
if user:
|
||||
user.points += cost
|
||||
db.session.commit()
|
||||
except Exception as re:
|
||||
system_logger.error(f"退费失败: {str(re)}")
|
||||
if use_trial:
|
||||
try:
|
||||
from services.generation_service import refund_points
|
||||
refund_points(user_id, cost)
|
||||
except Exception as re:
|
||||
system_logger.error(f"退费失败: {str(re)}")
|
||||
|
||||
# 确保 Redis 状态一定被更新,防止前端死循环
|
||||
redis_client.setex(f"task:{internal_task_id}", 3600, json.dumps({"status": "error", "message": str(e)}))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user