...
This commit is contained in:
@@ -24,16 +24,12 @@ export async function executeDictionaryLookup(
|
||||
// 代码层面验证:输入是否有效
|
||||
if (!analysis.isValid) {
|
||||
console.log("[阶段1] 输入无效:", analysis.reason);
|
||||
return {
|
||||
error: analysis.reason || "无效输入",
|
||||
};
|
||||
throw analysis.reason || "无效输入";
|
||||
}
|
||||
|
||||
if (analysis.isEmpty) {
|
||||
console.log("[阶段1] 输入为空");
|
||||
return {
|
||||
error: "输入为空",
|
||||
};
|
||||
throw "输入为空";
|
||||
}
|
||||
|
||||
console.log("[阶段1] 输入分析完成:", analysis);
|
||||
@@ -65,9 +61,7 @@ export async function executeDictionaryLookup(
|
||||
// 代码层面验证:标准形式不能为空
|
||||
if (!standardFormResult.standardForm) {
|
||||
console.error("[阶段3] 标准形式为空");
|
||||
return {
|
||||
error: "无法生成标准形式",
|
||||
};
|
||||
throw "无法生成标准形式";
|
||||
}
|
||||
|
||||
console.log("[阶段3] 标准形式生成完成:", standardFormResult);
|
||||
@@ -99,8 +93,6 @@ export async function executeDictionaryLookup(
|
||||
|
||||
// 任何阶段失败都返回错误(包含 reason)
|
||||
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||||
return {
|
||||
error: errorMessage,
|
||||
};
|
||||
throw errorMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
|
||||
import { executeDictionaryLookup } from "./dictionary";
|
||||
import { createLookUp, createPhrase, createWord, createPhraseEntry, createWordEntry, selectLastLookUp } from "../services/dictionaryService";
|
||||
import { DictLookUpRequest, DictWordResponse, isDictErrorResponse, isDictPhraseResponse, isDictWordResponse, type DictLookUpResponse } from "@/lib/shared";
|
||||
import { text } from "node:stream/consumers";
|
||||
import { DictLookUpRequest, DictWordResponse, isDictPhraseResponse, isDictWordResponse, type DictLookUpResponse } from "@/lib/shared";
|
||||
import { lookUpValidation } from "@/lib/shared/validations/dictionaryValidations";
|
||||
|
||||
const saveResult = async (req: DictLookUpRequest, res: DictLookUpResponse) => {
|
||||
if (isDictErrorResponse(res)) return;
|
||||
else if (isDictPhraseResponse(res)) {
|
||||
if (isDictPhraseResponse(res)) {
|
||||
// 先创建 Phrase
|
||||
const phrase = await createPhrase({
|
||||
standardForm: res.standardForm,
|
||||
@@ -83,62 +82,59 @@ export const lookUp = async (req: DictLookUpRequest): Promise<DictLookUpResponse
|
||||
userId
|
||||
} = req;
|
||||
|
||||
try {
|
||||
const lastLookUp = await selectLastLookUp({
|
||||
lookUpValidation(req);
|
||||
|
||||
const lastLookUp = await selectLastLookUp({
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang
|
||||
});
|
||||
|
||||
if (forceRelook || !lastLookUp) {
|
||||
// 使用新的模块化查询系统
|
||||
const response = await executeDictionaryLookup(
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang
|
||||
});
|
||||
);
|
||||
|
||||
if (forceRelook || !lastLookUp) {
|
||||
// 使用新的模块化查询系统
|
||||
const response = await executeDictionaryLookup(
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang
|
||||
);
|
||||
saveResult({
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang,
|
||||
userId,
|
||||
forceRelook
|
||||
}, response);
|
||||
|
||||
saveResult({
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang,
|
||||
userId,
|
||||
forceRelook
|
||||
}, response);
|
||||
|
||||
return response;
|
||||
return response;
|
||||
} else {
|
||||
// 从数据库返回缓存的结果
|
||||
if (lastLookUp.dictionaryWordId) {
|
||||
createLookUp({
|
||||
userId: userId,
|
||||
text: text,
|
||||
queryLang: queryLang,
|
||||
definitionLang: definitionLang,
|
||||
dictionaryWordId: lastLookUp.dictionaryWordId,
|
||||
});
|
||||
return {
|
||||
standardForm: lastLookUp.dictionaryWord!.standardForm,
|
||||
entries: lastLookUp.dictionaryWord!.entries
|
||||
};
|
||||
} else if (lastLookUp.dictionaryPhraseId) {
|
||||
createLookUp({
|
||||
userId: userId,
|
||||
text: text,
|
||||
queryLang: queryLang,
|
||||
definitionLang: definitionLang,
|
||||
dictionaryPhraseId: lastLookUp.dictionaryPhraseId
|
||||
});
|
||||
return {
|
||||
standardForm: lastLookUp.dictionaryPhrase!.standardForm,
|
||||
entries: lastLookUp.dictionaryPhrase!.entries
|
||||
};
|
||||
} else {
|
||||
// 从数据库返回缓存的结果
|
||||
if (lastLookUp.dictionaryWordId) {
|
||||
createLookUp({
|
||||
userId: userId,
|
||||
text: text,
|
||||
queryLang: queryLang,
|
||||
definitionLang: definitionLang,
|
||||
dictionaryWordId: lastLookUp.dictionaryWordId,
|
||||
});
|
||||
return {
|
||||
standardForm: lastLookUp.dictionaryWord!.standardForm,
|
||||
entries: lastLookUp.dictionaryWord!.entries
|
||||
};
|
||||
} else if (lastLookUp.dictionaryPhraseId) {
|
||||
createLookUp({
|
||||
userId: userId,
|
||||
text: text,
|
||||
queryLang: queryLang,
|
||||
definitionLang: definitionLang,
|
||||
dictionaryPhraseId: lastLookUp.dictionaryPhraseId
|
||||
});
|
||||
return {
|
||||
standardForm: lastLookUp.dictionaryPhrase!.standardForm,
|
||||
entries: lastLookUp.dictionaryPhrase!.entries
|
||||
};
|
||||
} else {
|
||||
return { error: "Database structure error!" };
|
||||
}
|
||||
throw "错误D101";
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return { error: "look up error" };
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user