客户信息删除代码更新
This commit is contained in:
+48
-7
@@ -10,6 +10,7 @@ API 路由定义模块
|
||||
from fastapi import APIRouter, Request, HTTPException, status, Depends
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from pydantic import ValidationError
|
||||
from typing import Dict, Any
|
||||
import json
|
||||
import anyio
|
||||
@@ -37,7 +38,7 @@ async def healthcheck():
|
||||
|
||||
用于检查服务是否正常运行
|
||||
"""
|
||||
return HealthResponse(status="ok", version="1.0.0")
|
||||
return HealthResponse(status="ok", version="2.0.0")
|
||||
|
||||
|
||||
@router.post("/webhook", response_model=WebhookResponse, tags=["业务"])
|
||||
@@ -73,14 +74,14 @@ async def webhook(
|
||||
raw_data = await request.json()
|
||||
# 使用 Pydantic 进行数据验证(允许额外字段)
|
||||
webhook_data = WebhookRequest(**raw_data)
|
||||
data = webhook_data.dict(exclude_none=True)
|
||||
data = webhook_data.model_dump(exclude_none=True)
|
||||
except json.JSONDecodeError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请求体必须是有效的 JSON 格式"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"请求数据验证失败: {str(e)}")
|
||||
logger.warning(f"请求数据验证失败: {str(e)}, 原始数据: {raw_data if 'raw_data' in locals() else 'N/A'}")
|
||||
# 如果验证失败,仍然尝试使用原始数据(向后兼容)
|
||||
data = raw_data if 'raw_data' in locals() else {}
|
||||
|
||||
@@ -88,9 +89,11 @@ async def webhook(
|
||||
header = request.headers
|
||||
decoded_header = app_tools.decode_headers(header)
|
||||
|
||||
# 验证 Action 字段
|
||||
action = decoded_header.get('Action')
|
||||
# 验证 Action 字段(HTTP头在FastAPI中会被转换为小写)
|
||||
# 同时检查 'Action' 和 'action' 以兼容不同情况
|
||||
action = decoded_header.get('Action') or decoded_header.get('action')
|
||||
if not action:
|
||||
logger.warning(f"请求头中缺少 Action 字段,请求头: {decoded_header}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请求头中缺少必需的 Action 字段"
|
||||
@@ -98,7 +101,8 @@ async def webhook(
|
||||
|
||||
# 处理 F6_Plugin 特殊逻辑
|
||||
if action == 'F6_Plugin':
|
||||
check = decoded_header.get('Check')
|
||||
# 同时检查 'Check' 和 'check' 以兼容不同情况
|
||||
check = decoded_header.get('Check') or decoded_header.get('check')
|
||||
if check == '否':
|
||||
handler = f6_plugin_module.check_file
|
||||
elif check == '是':
|
||||
@@ -150,13 +154,50 @@ async def webhook(
|
||||
if not isinstance(result, dict):
|
||||
result = {"msg": str(result)}
|
||||
|
||||
# 处理 msg 字段:如果 msg 是字典,将其内容展开到结果中
|
||||
if "msg" in result and isinstance(result["msg"], dict):
|
||||
msg_dict = result.pop("msg")
|
||||
logger.warning(f"操作 {action} 返回的 msg 字段是字典类型,正在自动转换。原始数据: {json.dumps(msg_dict, ensure_ascii=False)}")
|
||||
# 如果字典中有 msg 字段,使用它;否则使用 JSON 字符串
|
||||
if "msg" in msg_dict:
|
||||
result["msg"] = msg_dict.pop("msg")
|
||||
else:
|
||||
result["msg"] = json.dumps(msg_dict, ensure_ascii=False)
|
||||
# 将字典中的其他字段合并到结果中
|
||||
result.update(msg_dict)
|
||||
|
||||
if "msg" not in result:
|
||||
result["msg"] = "操作完成"
|
||||
|
||||
# 确保 msg 是字符串类型
|
||||
if not isinstance(result.get("msg"), str):
|
||||
logger.warning(f"操作 {action} 返回的 msg 字段类型为 {type(result.get('msg'))},正在转换为字符串")
|
||||
result["msg"] = str(result.get("msg", "操作完成"))
|
||||
|
||||
logger.info(f"操作完成: {action}, 结果: {json.dumps(result, ensure_ascii=False)}")
|
||||
|
||||
# 返回响应(使用 Pydantic 模型验证)
|
||||
return WebhookResponse(**result)
|
||||
try:
|
||||
return WebhookResponse(**result)
|
||||
except ValidationError as validation_error:
|
||||
# 捕获 Pydantic 验证错误,提供更清晰的错误信息
|
||||
error_messages = []
|
||||
for error in validation_error.errors():
|
||||
field = " -> ".join(str(loc) for loc in error.get("loc", []))
|
||||
error_type = error.get("type", "unknown")
|
||||
error_msg = error.get("msg", "验证失败")
|
||||
error_messages.append(f"字段 '{field}': {error_msg} (类型: {error_type})")
|
||||
|
||||
error_detail = "; ".join(error_messages)
|
||||
logger.error(
|
||||
f"响应数据验证失败 - 操作: {action}, "
|
||||
f"错误详情: {error_detail}, "
|
||||
f"原始数据: {json.dumps(result, ensure_ascii=False, default=str)}"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"响应数据格式错误: {error_detail}。请检查操作 '{action}' 的返回格式是否符合 API 规范(msg 字段必须是字符串类型)。"
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
# 重新抛出 HTTP 异常
|
||||
|
||||
Reference in New Issue
Block a user