]> acesimba.cloud Git - words.git/commitdiff
fix: TTS 支持含斜杠短语 + 提交学习进度 + 修正 gitignore
authorroot <root@localhost.localdomain>
Sat, 15 Aug 2026 10:17:28 +0000 (18:17 +0800)
committerroot <root@localhost.localdomain>
Sat, 15 Aug 2026 10:17:28 +0000 (18:17 +0800)
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 音频。

backend/db/bcd.db
backend/main.py
frontend/.gitignore
frontend/public/sw.js
frontend/src/api.js

index fb865f0a7cfdccd74152fa291f359330b0131a53..ca3a2be4b0d82bf8187196f8c8869c7ee3ebc78f 100644 (file)
Binary files a/backend/db/bcd.db and b/backend/db/bcd.db differ
index 49f93c59d9aefeba298428db990649f2b1a7a8cf..3c2c7c0b67a5272aba978b893845dd4f18e9d6d3 100644 (file)
@@ -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:
index bc2c3eba60ee566aa65fb50265b3f604d1840e86..ad914166a035b602d778cc0414525dd7654d3bea 100644 (file)
@@ -23,4 +23,4 @@ yarn-debug.log*
 yarn-error.log*
 
 # HTTPS 同源构建产物(由 REACT_APP_API_BASE="" BUILD_PATH=build-https 生成,不入库)
-frontend/build-https/
+build-https/
index 4888e89d9714dbe8b12b49beb9414f473cdbfa2e..beef00e2edc166ac81c6d462ba3c6b1ad282f1ab 100644 (file)
@@ -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;
     }
index 0982fde74e3b5f2a11fbae7fdc31d70d1f5331a2..6e735fd9d2d1e342551a9b05ab749aa7aebf960e 100644 (file)
@@ -27,5 +27,6 @@ export const api = {
     }),
     // 图片与 TTS 走 <img src>/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)}`,
 };