feat: optimize verification code recognition strategy

This commit is contained in:
eoao
2026-05-10 00:55:27 +08:00
parent 7423a25fd6
commit ead8f9e2b8
2 changed files with 30 additions and 18 deletions

View File

@@ -92,7 +92,7 @@ export async function email(message, env, ctx) {
}
const toName = email.to.find(item => item.address === message.to)?.name || '';
const code = aiCode === settingConst.aiCode.OPEN && checkAiCodeFilter(aiCodeFilter, email) ? await aiService.extractCode({ env }, email) : '';
const code = await aiService.extractCode({ env }, email, { aiCode, aiCodeFilter });
const params = {
toEmail: message.to,
@@ -185,19 +185,6 @@ export async function email(message, env, ctx) {
}
}
function checkAiCodeFilter(aiCodeFilterStr, email) {
const filterList = aiCodeFilterStr ? aiCodeFilterStr.split(',').map(item => item.trim().toLowerCase()).filter(Boolean) : [];
if (filterList.length === 0) {
return true;
}
const fromEmail = (email.from?.address || '').trim().toLowerCase();
const fromDomain = emailUtils.getDomain(fromEmail).toLowerCase();
return filterList.some(item => item === fromEmail || item === fromDomain);
}
function checkBlock(blackSubjectStr, blackContentStr, blackFromStr, email) {
const blackFromList = blackFromStr ? blackFromStr.split(',') : []

View File

@@ -1,13 +1,16 @@
import emailUtils from '../utils/email-utils';
import { settingConst } from '../const/entity-const';
const codeKeywords = ['码', '令牌', 'code', 'token'];
const aiService = {
async extractCode(c, email) {
const ai = c.env.AI || c.env.ai;
if (!ai) {
async extractCode(c, email, options = {}) {
if (!this.shouldExtractCode(options.aiCode, options.aiCodeFilter, email)) {
return '';
}
const ai = c.env.AI;
try {
const subject = email.subject || '';
const text = emailUtils.formatText(email.text || '');
@@ -48,6 +51,28 @@ const aiService = {
console.error('验证码提取失败: ', e);
return '';
}
},
shouldExtractCode(aiCode, aiCodeFilterStr, email) {
if (aiCode !== settingConst.aiCode.OPEN) {
return false;
}
const filterList = aiCodeFilterStr ? aiCodeFilterStr.split(',').map(item => item.trim().toLowerCase()).filter(Boolean) : [];
if (filterList.length > 0) {
const fromEmail = (email.from?.address || '').trim().toLowerCase();
const fromDomain = emailUtils.getDomain(fromEmail).toLowerCase();
return filterList.some(item => item === fromEmail || item === fromDomain);
}
const subject = email.subject || '';
const text = emailUtils.formatText(email.text || '');
const htmlText = emailUtils.htmlToText(email.html || '');
const content = `${subject}\n${htmlText || text}`;
const lowerContent = content.toLowerCase();
return codeKeywords.some(keyword => lowerContent.includes(keyword));
}
};