From: root Date: Sat, 15 Aug 2026 10:17:28 +0000 (+0800) Subject: fix: TTS 支持含斜杠短语 + 提交学习进度 + 修正 gitignore X-Git-Url: http://acesimba.cloud/gitweb/?a=commitdiff_plain;h=3f0ec3bbf262e46a6875c83747bf409d9e340f6f;p=words.git fix: TTS 支持含斜杠短语 + 提交学习进度 + 修正 gitignore 1. 后端 /api/tts 新增 ?w= 查询参数路由:词内字面 "/"(如 "in the/a form of")不再与 URL 路径分隔符冲突导致 404; 原 /api/tts/{word} 路径路由保留向后兼容。 2. 前端 ttsUrl 改为 /api/tts?w= 形式(api.js 一处改动覆盖 StudyMode/TestMode/离线预取所有调用点)。 3. SW 缓存规则改为匹配 /api/tts(无尾斜杠),保证新形式 音频仍走 cache-first,离线可用。 4. frontend/.gitignore 修正为 build-https/(原 frontend/build-https/ 无效),构建产物不再误入库。 5. 提交今日 iPad 离线学习进度(db/bcd.db)。 已验证端到端:含斜杠短语经 :8443 → :1060 均返回 200 音频。 --- diff --git a/backend/db/bcd.db b/backend/db/bcd.db index fb865f0..ca3a2be 100644 Binary files a/backend/db/bcd.db and b/backend/db/bcd.db differ diff --git a/backend/main.py b/backend/main.py index 49f93c5..3c2c7c0 100644 --- a/backend/main.py +++ b/backend/main.py @@ -252,8 +252,7 @@ async def get_picture(pic_id: int): TTS_CACHE_DIR = os.path.join(BASE_DIR, "db", "tts_cache") os.makedirs(TTS_CACHE_DIR, exist_ok=True) -@app.get("/api/tts/{word}") -async def get_tts(word: str): +def _build_tts_response(word: str): word = word.strip() if not word: raise HTTPException(status_code=400, detail="empty word") @@ -304,6 +303,17 @@ async def get_tts(word: str): return Response(content=data, media_type="audio/mpeg") + +@app.get("/api/tts/{word}") +async def get_tts(word: str): + return _build_tts_response(word) + + +@app.get("/api/tts") +async def get_tts_query(w: str = ""): + # 含 / 的短语(如 "in the/a form of")走查询参数,避免与 URL 路径分隔符冲突 + return _build_tts_response(w) + @app.post("/api/progress/update") async def update_progress(data: ProgressUpdate): try: diff --git a/frontend/.gitignore b/frontend/.gitignore index bc2c3eb..ad91416 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -23,4 +23,4 @@ yarn-debug.log* yarn-error.log* # HTTPS 同源构建产物(由 REACT_APP_API_BASE="" BUILD_PATH=build-https 生成,不入库) -frontend/build-https/ +build-https/ diff --git a/frontend/public/sw.js b/frontend/public/sw.js index 4888e89..beef00e 100644 --- a/frontend/public/sw.js +++ b/frontend/public/sw.js @@ -31,7 +31,8 @@ self.addEventListener('fetch', (event) => { const path = url.pathname; // 图片与 TTS 音频: cache-first (含跨域, 后端已开 CORS) - if (path.startsWith('/api/picture/') || path.startsWith('/api/tts/')) { + // 注意 TTS 现用查询参数形式 /api/tts?w=,pathname 为 /api/tts(无尾斜杠),故不加尾斜杠匹配 + if (path.startsWith('/api/picture') || path.startsWith('/api/tts')) { event.respondWith(cacheFirst(req)); return; } diff --git a/frontend/src/api.js b/frontend/src/api.js index 0982fde..6e735fd 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -27,5 +27,6 @@ export const api = { }), // 图片与 TTS èµ° /new Audio(),不走 axios;SW 仍能拦截缓存 pictureUrl: (picId) => `${API_BASE}/api/picture/${picId}`, - ttsUrl: (word) => `${API_BASE}/api/tts/${encodeURIComponent(word)}`, + // 走查询参数 w=,避免词内字面 "/"(如 "in the/a form of")与 URL 路径分隔符冲突导致 404 + ttsUrl: (word) => `${API_BASE}/api/tts?w=${encodeURIComponent(word)}`, };