diff --git a/.gitignore b/.gitignore index 63e8a04..8a0057f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ 归档/ +xunifriend_RaeeC/runtime/log/202602/01.log diff --git a/lover/routers/__pycache__/chat.cpython-314.pyc b/lover/routers/__pycache__/chat.cpython-314.pyc index 3725a32..ad96641 100644 Binary files a/lover/routers/__pycache__/chat.cpython-314.pyc and b/lover/routers/__pycache__/chat.cpython-314.pyc differ diff --git a/lover/routers/chat.py b/lover/routers/chat.py index 295b5c4..388bcea 100644 --- a/lover/routers/chat.py +++ b/lover/routers/chat.py @@ -4,7 +4,7 @@ import re import random import oss2 -from fastapi import APIRouter, Depends, HTTPException, Query +from fastapi import APIRouter, Depends, HTTPException, Query, Request from pydantic import BaseModel, Field from sqlalchemy import desc from sqlalchemy.orm import Session @@ -474,7 +474,7 @@ def _pick_available_voice(db: Session, lover: Lover, user_row: User) -> VoiceLib return candidate -def _upload_tts_to_oss(file_bytes: bytes, lover_id: int, message_id: int) -> str: +def _upload_tts_to_oss(file_bytes: bytes, lover_id: int, message_id: int, request: Request = None) -> str: """ 上传 TTS 音频文件。 优先使用 OSS,如果未配置则保存到本地。 @@ -520,8 +520,16 @@ def _upload_tts_to_oss(file_bytes: bytes, lover_id: int, message_id: int) -> str with open(file_path, "wb") as f: f.write(file_bytes) - # 返回完整 URL(使用环境变量配置的后端地址) - backend_url = os.getenv("BACKEND_URL", "http://127.0.0.1:8000") + # 自动检测请求来源,生成正确的 URL + if request: + # 从请求头获取 Host + host = request.headers.get("host", "127.0.0.1:8000") + scheme = "https" if request.url.scheme == "https" else "http" + backend_url = f"{scheme}://{host}" + else: + # 降级使用环境变量 + backend_url = os.getenv("BACKEND_URL", "http://127.0.0.1:8000") + return f"{backend_url.rstrip('/')}/tts/{lover_id}/{message_id}.mp3" except Exception as exc: raise HTTPException(status_code=500, detail=f"保存语音文件失败: {exc}") from exc @@ -653,6 +661,7 @@ def list_messages( @router.post("/messages/tts/{message_id}", response_model=ApiResponse[TTSOut]) def generate_message_tts( message_id: int, + request: Request, db: Session = Depends(get_db), user: AuthedUser = Depends(get_current_user), ): @@ -706,7 +715,7 @@ def generate_message_tts( model=model, voice=voice.voice_code, ) - url = _upload_tts_to_oss(audio_bytes, lover.id, msg.id) + url = _upload_tts_to_oss(audio_bytes, lover.id, msg.id, request) except HTTPException as exc: detail_text = str(exc.detail) if hasattr(exc, "detail") else str(exc) fallback_done = False @@ -721,7 +730,7 @@ def generate_message_tts( model=fallback_model, voice=fallback_voice, ) - url = _upload_tts_to_oss(audio_bytes, lover.id, msg.id) + url = _upload_tts_to_oss(audio_bytes, lover.id, msg.id, request) msg.tts_voice_id = None # 兜底音色不绑定库ID msg.tts_model_id = fallback_model fallback_done = True diff --git a/public/tts/47/776.mp3 b/public/tts/47/776.mp3 new file mode 100644 index 0000000..ebe2c07 Binary files /dev/null and b/public/tts/47/776.mp3 differ diff --git a/xuniYou/pages/chat/background.vue b/xuniYou/pages/chat/background.vue index 9eadcd6..44914c5 100644 --- a/xuniYou/pages/chat/background.vue +++ b/xuniYou/pages/chat/background.vue @@ -76,6 +76,8 @@