From 2faebef7f2d41b444d7fc8b3fb50f94037a93a9a Mon Sep 17 00:00:00 2001 From: Miyamizu-MitsuhaSang <2510681107@qq.com> Date: Mon, 15 Sep 2025 17:39:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=BF=BB=E8=AF=91=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BC=A0=E5=85=A5=E5=8F=82=E6=95=B0=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=9APOST+JSON=E8=AF=B7=E6=B1=82=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/translator.py | 36 +++++++++++++++++------------------- app/schemas/trans_schemas.py | 3 ++- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/app/api/translator.py b/app/api/translator.py index d8a24e0..118422e 100644 --- a/app/api/translator.py +++ b/app/api/translator.py @@ -10,7 +10,7 @@ import requests from fastapi import APIRouter, Depends, HTTPException from app.models import User -from app.schemas.trans_schemas import TransResponse +from app.schemas.trans_schemas import TransResponse, TransRequest from app.utils.security import is_admin_user, get_current_user from scripts.md5 import make_md5 from settings import settings @@ -60,18 +60,18 @@ async def baidu_translation(query: str, from_lang: str, to_lang: str): "sign": sign, } - print(payload) - - request = httpx.Request( - "POST", - url, - data=payload, - headers={"Content-Type": "application/x-www-form-urlencoded"} - ) - print("完整请求内容:") - print("URL:", request.url) - print("Headers:", request.headers) - print("Body:", request.content.decode("utf-8")) + # print(payload) + # + # request = httpx.Request( + # "POST", + # url, + # data=payload, + # headers={"Content-Type": "application/x-www-form-urlencoded"} + # ) + # print("完整请求内容:") + # print("URL:", request.url) + # print("Headers:", request.headers) + # print("Body:", request.content.decode("utf-8")) async with httpx.AsyncClient(timeout=10) as client: response = await client.post( @@ -94,15 +94,13 @@ async def baidu_translation(query: str, from_lang: str, to_lang: str): @translator_router.post('/translate', response_model=TransResponse) async def translate( - query: str, - from_lang: str = 'auto', - to_lang: str = 'zh', + translate_request: TransRequest, user=Depends(get_current_user) ): text = await baidu_translation( - query=query, - from_lang=from_lang, - to_lang=to_lang + query=translate_request.query, + from_lang=translate_request.from_lang, + to_lang=translate_request.to_lang, ) return TransResponse(translated_text=text) diff --git a/app/schemas/trans_schemas.py b/app/schemas/trans_schemas.py index 87aaa31..479e668 100644 --- a/app/schemas/trans_schemas.py +++ b/app/schemas/trans_schemas.py @@ -4,7 +4,8 @@ from pydantic import BaseModel, field_validator, model_validator class TransRequest(BaseModel): - from_lang: Literal['fr', 'jp', 'zh'] = 'fr' + query: str + from_lang: Literal['auto', 'fr', 'jp', 'zh'] = 'auto' to_lang: Literal['fr', 'jp', 'zh'] = 'zh' @field_validator('from_lang', 'to_lang')