|
|
@@ -1384,9 +1384,43 @@ export async function YJBDC_Articles_Admin(ctx) {
|
|
1384
|
1384
|
};
|
|
1385
|
1385
|
|
|
1386
|
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
|
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
|
};
|