chengjie 2 月之前
父节点
当前提交
cdb79cb269
共有 2 个文件被更改,包括 45 次插入10 次删除
  1. 7 6
      src/api/yjbdc/readerController.js
  2. 38 4
      src/api/yjbdc/yjbdcController.js

+ 7 - 6
src/api/yjbdc/readerController.js

@@ -47,15 +47,12 @@ export async function GetReaderBooksChapter(ctx) {
47
     let content = await fs.promises.readFile(filePath, 'utf-8');
47
     let content = await fs.promises.readFile(filePath, 'utf-8');
48
     content=JSON.parse(content);
48
     content=JSON.parse(content);
49
     for(const item of content) {
49
     for(const item of content) {
50
+        if (param.Title === "Strange-Life-of-a-Cat") {
51
+            item.title ="Chapter " + (Number(item.title.substring(8))-5); 
52
+        }
50
         result.push(item.title);
53
         result.push(item.title);
51
     }
54
     }
52
 
55
 
53
-    // 处理Strange-Life-of-a-Cat的章节,去掉前5个章节
54
-    // if (param.Title === "Strange-Life-of-a-Cat") {
55
-    //     for(let i=0;i<5;i++) 
56
-    //         result = result.slice(1); // 去掉第一个章节
57
-    // }
58
-
59
     ctx.body = { "errcode": 10000, result: result };
56
     ctx.body = { "errcode": 10000, result: result };
60
 }
57
 }
61
 
58
 
@@ -65,6 +62,10 @@ export async function GetReaderBooksChapterContent(ctx) {
65
         Chapter: ctx.query.Chapter || "0",
62
         Chapter: ctx.query.Chapter || "0",
66
     };
63
     };
67
 
64
 
65
+    if (param.Title === "Strange-Life-of-a-Cat") {
66
+        param.Chapter=param.Chapter+5; 
67
+    }
68
+
68
     let result = [];
69
     let result = [];
69
 
70
 
70
     const {readFile} = fs.promises;
71
     const {readFile} = fs.promises;

+ 38 - 4
src/api/yjbdc/yjbdcController.js

@@ -1384,9 +1384,43 @@ export async function YJBDC_Articles_Admin(ctx) {
1384
 };
1384
 };
1385
 
1385
 
1386
 export async function GetSentenceTranslate(ctx) {
1386
 export async function GetSentenceTranslate(ctx) {
1387
+    const url = 'GetSentenceTranslate?Sentence=' + encodeURIComponent(ctx.request.body.Sentence);
1388
+    let result = globalCache.get(url);
1389
+
1390
+    // 检查是否是队列状态而不是正在处理状态
1391
+    if (result && result.isQueued) {
1392
+        // 如果是队列状态,继续处理
1393
+        result = 0;
1394
+    }
1395
+
1396
+    if (result) {
1397
+        ctx.body = result;
1398
+        return;
1399
+    }
1400
+
1387
     const param = ctx.request.body;
1401
     const param = ctx.request.body;
1388
-    // 调用翻译API
1389
-    const result = await machineTranslationAPI.translateText(param.Sentence);
1390
-    //console.log(result);
1391
-    ctx.body = { "errcode": 10000, result: result.data };
1402
+    
1403
+    // 设置缓存,表示正在处理
1404
+    result = { errcode: 10000, result: "-2" };
1405
+    globalCache.set(url, result, config.BufferMemoryTime);
1406
+    console.log("翻译中,暂存缓存");
1407
+    
1408
+    try {
1409
+        // 调用翻译API
1410
+        const translationResult = await machineTranslationAPI.translateText(param.Sentence);
1411
+        //console.log(translationResult);
1412
+        
1413
+        result = { "errcode": 10000, result: translationResult.data };
1414
+        
1415
+        // 更新缓存
1416
+        globalCache.set(url, result, config.BufferMemoryTimeHigh);
1417
+        console.log("翻译完成,更新缓存");
1418
+    } catch (error) {
1419
+        console.error("翻译出错:", error);
1420
+        result = { "errcode": 10001, result: "翻译服务出错" };
1421
+        // 删除缓存,允许重试
1422
+        globalCache.delete(url);
1423
+    }
1424
+    
1425
+    ctx.body = result;
1392
 };
1426
 };