File size: 20,272 Bytes
7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 98bb76b 7ee9dd5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | const { Resend } = require('resend');
const { decrypt } = require('./crypto');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
// 调试日志函数
function debugLog(message, data = null) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] EMAIL DEBUG: ${message}`);
if (data) {
console.log(JSON.stringify(data, null, 2));
}
}
async function sendModelChangeNotification(siteName, diff, fastify) {
debugLog(`=== 开始邮件通知流程 ===`);
debugLog(`站点名称: ${siteName}`);
debugLog('变化数据:', diff);
try {
// 获取邮件配置
debugLog('正在获取邮件配置...');
const config = await prisma.emailConfig.findFirst();
debugLog('邮件配置查询结果:', {
exists: !!config,
enabled: config?.enabled,
hasApiKey: !!config?.resendApiKeyEnc,
notifyEmailsRaw: config?.notifyEmails,
emailCount: config?.notifyEmails ? (() => {
try {
return JSON.parse(config.notifyEmails || '[]').length;
} catch {
return 0;
}
})() : 0
});
if (!config || !config.enabled) {
debugLog('❌ 邮件配置未启用或不存在,跳过发送');
return;
}
// 解析邮箱列表(支持两种格式:JSON数组或逗号分隔字符串)
let emails = [];
try {
const emailStr = (config.notifyEmails || '').trim();
// 检测是否为 JSON 数组格式
if (emailStr.startsWith('[')) {
emails = JSON.parse(emailStr);
} else {
// 作为逗号或分号分隔的字符串处理
emails = emailStr.split(/[,;]/).map(e => e.trim()).filter(e => e.length > 0);
}
debugLog(`✅ 解析邮箱列表成功: [${emails.join(', ')}]`);
} catch (error) {
debugLog('❌ 解析邮箱列表失败:', error.message);
return;
}
if (!emails.length) {
debugLog('❌ 没有配置收件邮箱,跳过发送');
return;
}
// 解密 API Key
let apiKey;
try {
apiKey = decrypt(config.resendApiKeyEnc);
debugLog('✅ API Key 解密成功');
} catch (error) {
debugLog('❌ API Key 解密失败:', error.message);
return;
}
debugLog('正在初始化 Resend...');
const resend = new Resend(apiKey);
debugLog('✅ Resend 初始化完成');
// 检查是否有实际变化(只检查新增和删除)
const hasChanges = (diff.added && diff.added.length > 0) ||
(diff.removed && diff.removed.length > 0);
debugLog('📊 变化检查:', {
hasChanges,
added: diff.added?.length || 0,
removed: diff.removed?.length || 0
});
if (!hasChanges) {
debugLog('❌ 没有模型变化,跳过发送');
return;
}
// 构建邮件内容
debugLog('🔧 开始构建邮件内容...');
let htmlContent = `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">🔔 AI 模型变化通知</h1>
<p><strong>站点:</strong>${siteName}</p>
<p><strong>检测时间:</strong>${new Date().toLocaleString('zh-CN')}</p>
`;
// 如果有签到信息,添加签到结果
if (diff.checkInResult) {
const { checkInSuccess, checkInMessage, checkInQuota } = diff.checkInResult;
const statusIcon = checkInSuccess ? '✅' : '❌';
const statusColor = checkInSuccess ? '#52c41a' : '#ff4d4f';
htmlContent += `
<div style="background: ${checkInSuccess ? '#f6ffed' : '#fff2f0'}; border: 1px solid ${checkInSuccess ? '#b7eb8f' : '#ffccc7'}; padding: 12px; border-radius: 4px; margin: 10px 0;">
<h3 style="color: ${statusColor}; margin: 0 0 8px 0;">${statusIcon} 签到结果</h3>
<p style="margin: 0;"><strong>状态:</strong>${checkInMessage || (checkInSuccess ? '签到成功' : '签到失败')}</p>
${checkInQuota ? `<p style="margin: 4px 0 0 0;"><strong>获得额度:</strong>${checkInQuota}</p>` : ''}
</div>
`;
}
if (diff.added && diff.added.length > 0) {
htmlContent += `<h2 style="color: #52c41a;">✅ 新增模型 (${diff.added.length})</h2><ul>`;
diff.added.forEach(model => {
htmlContent += `<li>${model.id || 'Unknown'}</li>`;
});
htmlContent += '</ul>';
}
if (diff.removed && diff.removed.length > 0) {
htmlContent += `<h2 style="color: #ff4d4f;">❌ 移除模型 (${diff.removed.length})</h2><ul>`;
diff.removed.forEach(model => {
htmlContent += `<li>${model.id || 'Unknown'}</li>`;
});
htmlContent += '</ul>';
}
htmlContent += `<p style="color: #666; font-size: 12px; margin-top: 30px;">本邮件由 AI 模型监测系统自动发送</p></div>`;
debugLog('✅ 邮件内容构建完成');
// 准备邮件参数(完全按照官方示例)
const emailParams = {
from: 'AI模型监测 <onboarding@resend.dev>',
to: emails,
subject: `[AI模型监测] ${siteName} - 模型发生变化`,
html: htmlContent
};
debugLog('📧 准备发送邮件:', {
from: emailParams.from,
to: emailParams.to,
subject: emailParams.subject,
htmlLength: htmlContent.length
});
// 发送邮件(使用官方示例格式)
debugLog('🚀 正在调用 Resend API...');
const result = await resend.emails.send(emailParams);
debugLog('🎉 邮件发送成功!结果:', result);
fastify?.log?.info(`邮件发送成功: ${siteName} -> ${emails.join(', ')}`);
return result;
} catch (error) {
debugLog('💥 邮件发送过程出错:', {
errorName: error.name,
errorMessage: error.message,
errorStack: error.stack?.split('\n').slice(0, 3),
siteName
});
fastify?.log?.error(`邮件通知失败: ${siteName}`, error);
// 不抛出错误,避免影响主流程
console.error(`[EMAIL] 最终错误: ${siteName}`, error.message);
} finally {
debugLog(`=== 邮件通知流程结束 ===`);
}
}
// 聚合发送多个站点的变更通知
async function sendAggregatedNotification(siteChanges, fastify, failedSites = []) {
debugLog(`=== 开始聚合邮件通知流程 ===`);
debugLog(`站点变更数量: ${siteChanges.length}`);
debugLog(`失败站点数量: ${failedSites.length}`);
try {
// 获取邮件配置
debugLog('正在获取邮件配置...');
const config = await prisma.emailConfig.findFirst();
debugLog('邮件配置查询结果:', {
exists: !!config,
enabled: config?.enabled,
hasApiKey: !!config?.resendApiKeyEnc
});
if (!config || !config.enabled) {
debugLog('❌ 邮件配置未启用或不存在,跳过发送');
return;
}
// 解析邮箱列表
let emails = [];
try {
const emailStr = (config.notifyEmails || '').trim();
if (emailStr.startsWith('[')) {
emails = JSON.parse(emailStr);
} else {
emails = emailStr.split(/[,;]/).map(e => e.trim()).filter(e => e.length > 0);
}
debugLog(`✅ 解析邮箱列表成功: [${emails.join(', ')}]`);
} catch (error) {
debugLog('❌ 解析邮箱列表失败:', error.message);
return;
}
if (!emails.length) {
debugLog('❌ 没有配置收件邮箱,跳过发送');
return;
}
// 解密 API Key
let apiKey;
try {
apiKey = decrypt(config.resendApiKeyEnc);
debugLog('✅ API Key 解密成功');
} catch (error) {
debugLog('❌ API Key 解密失败:', error.message);
return;
}
debugLog('正在初始化 Resend...');
const resend = new Resend(apiKey);
debugLog('✅ Resend 初始化完成');
// 统计总变更数(只统计新增和删除)
let totalAdded = 0;
let totalRemoved = 0;
siteChanges.forEach(sc => {
totalAdded += sc.diff?.added?.length || 0;
totalRemoved += sc.diff?.removed?.length || 0;
});
debugLog('📊 聚合统计:', {
sitesWithChanges: siteChanges.length,
totalAdded,
totalRemoved
});
// 构建聚合邮件内容
debugLog('🔧 开始构建聚合邮件内容...');
let htmlContent = `
<div style="font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; background: #f5f5f5; padding: 20px;">
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 30px; border-radius: 12px 12px 0 0; text-align: center;">
<h1 style="color: white; margin: 0; font-size: 28px;">🔔 站点检测通知</h1>
<p style="color: rgba(255,255,255,0.9); margin: 10px 0 0 0; font-size: 16px;">模型监测 & 签到报告</p>
</div>
<div style="background: white; padding: 30px; border-radius: 0 0 12px 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
<div style="background: #e6f7ff; border-left: 4px solid #1890ff; padding: 15px; margin-bottom: 25px; border-radius: 4px;">
<p style="margin: 0; color: #0050b3; font-size: 15px;">
<strong>📅 检测时间:</strong>${new Date().toLocaleString('zh-CN')}
</p>
<p style="margin: 8px 0 0 0; color: #0050b3; font-size: 15px;">
<strong>🎯 检测站点:</strong>${siteChanges.length} 个
</p>
${failedSites.length > 0 ? `
<p style="margin: 8px 0 0 0; color: #cf1322; font-size: 15px;">
<strong>⚠️ 失败站点:</strong>${failedSites.length} 个
</p>
` : ''}
</div>
<div style="display: flex; gap: 15px; margin-bottom: 30px; flex-wrap: wrap;">
<div style="flex: 1; min-width: 200px; background: #f6ffed; border: 1px solid #b7eb8f; padding: 20px; border-radius: 8px; text-align: center;">
<div style="font-size: 36px; font-weight: bold; color: #52c41a;">${totalAdded}</div>
<div style="color: #389e0d; margin-top: 8px; font-size: 15px; font-weight: 600;">➕ 新增模型</div>
</div>
<div style="flex: 1; min-width: 200px; background: #fff2f0; border: 1px solid #ffccc7; padding: 20px; border-radius: 8px; text-align: center;">
<div style="font-size: 36px; font-weight: bold; color: #ff4d4f;">${totalRemoved}</div>
<div style="color: #cf1322; margin-top: 8px; font-size: 15px; font-weight: 600;">➖ 移除模型</div>
</div>
</div>
`;
// 为每个站点添加变更详情
siteChanges.forEach((siteChange, index) => {
const { siteName, diff, checkInResult } = siteChange;
// 统计变更项
const modelChanges = (diff?.added?.length || 0) + (diff?.removed?.length || 0);
const hasCheckIn = !!checkInResult;
const hasModelChange = modelChanges > 0;
// 生成标签文本
let labelText = '';
if (hasCheckIn && hasModelChange) {
labelText = '签到 + 模型变更';
} else if (hasCheckIn) {
labelText = `签到${checkInResult.checkInSuccess ? '成功' : '失败'}`;
} else if (hasModelChange) {
labelText = modelChanges + ' 项模型变更';
}
htmlContent += `
<details style="margin-bottom: 20px; border: 1px solid #e8e8e8; border-radius: 8px; overflow: hidden;" ${index === 0 ? 'open' : ''}>
<summary style="background: linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%); padding: 15px 20px; cursor: pointer; font-size: 16px; font-weight: 600; color: #333; user-select: none;">
🎯 ${siteName}
<span style="float: right; background: #1890ff; color: white; padding: 2px 12px; border-radius: 12px; font-size: 13px; font-weight: normal;">
${labelText}
</span>
</summary>
<div style="padding: 20px; background: #fafafa;">
`;
// 添加签到结果(如果有)- 放在最前面
if (checkInResult) {
const { checkInSuccess, checkInMessage, checkInQuota, checkInError } = checkInResult;
const statusIcon = checkInSuccess ? '✅' : '❌';
const statusColor = checkInSuccess ? '#52c41a' : '#ff4d4f';
const statusBg = checkInSuccess ? '#f6ffed' : '#fff2f0';
const statusBorder = checkInSuccess ? '#b7eb8f' : '#ffccc7';
htmlContent += `
<div style="background: ${statusBg}; border-left: 4px solid ${statusColor}; padding: 12px 15px; border-radius: 4px; margin-bottom: ${modelChanges > 0 ? '20px' : '0'};">
<div style="display: flex; align-items: flex-start; gap: 10px;">
<span style="font-size: 20px; flex-shrink: 0;">${statusIcon}</span>
<div style="flex: 1;">
<div style="font-weight: 600; color: ${statusColor}; font-size: 15px; margin-bottom: 4px;">
签到${checkInSuccess ? '成功' : '失败'}
</div>
<div style="color: #666; font-size: 13px; margin-bottom: 4px;">
${checkInMessage || (checkInSuccess ? '签到成功' : '签到失败')}
</div>
${checkInQuota ? `
<div style="color: #52c41a; font-size: 13px; font-weight: 600;">
💰 获得额度: ${checkInQuota}
</div>
` : ''}
${checkInError ? `
<div style="color: #8c8c8c; font-size: 12px; font-family: monospace; background: white; padding: 6px 8px; border-radius: 4px; margin-top: 6px; word-break: break-all;">
${checkInError}
</div>
` : ''}
</div>
</div>
</div>
`;
}
// 新增模型(只在有变更时显示)
if (diff && diff.added && diff.added.length > 0) {
htmlContent += `
<div style="margin-bottom: 20px;">
<h3 style="color: #52c41a; margin: 0 0 10px 0; font-size: 15px; display: flex; align-items: center; gap: 8px;">
<span style="background: #52c41a; color: white; width: 24px; height: 24px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 14px;">➕</span>
新增模型 (${diff.added.length})
</h3>
<div style="display: flex; flex-wrap: wrap; gap: 8px;">
`;
diff.added.slice(0, 20).forEach(model => {
htmlContent += `<span style="background: #f6ffed; border: 1px solid #b7eb8f; color: #389e0d; padding: 4px 12px; border-radius: 16px; font-size: 13px; font-family: monospace;">${model.id}</span>`;
});
if (diff.added.length > 20) {
htmlContent += `<span style="color: #666; font-size: 13px; padding: 4px 12px;">... 还有 ${diff.added.length - 20} 个</span>`;
}
htmlContent += `</div></div>`;
}
// 移除模型(只在有变更时显示)
if (diff && diff.removed && diff.removed.length > 0) {
htmlContent += `
<div style="margin-bottom: 20px;">
<h3 style="color: #ff4d4f; margin: 0 0 10px 0; font-size: 15px; display: flex; align-items: center; gap: 8px;">
<span style="background: #ff4d4f; color: white; width: 24px; height: 24px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 14px;">➖</span>
移除模型 (${diff.removed.length})
</h3>
<div style="display: flex; flex-wrap: wrap; gap: 8px;">
`;
diff.removed.slice(0, 20).forEach(model => {
htmlContent += `<span style="background: #fff2f0; border: 1px solid #ffccc7; color: #cf1322; padding: 4px 12px; border-radius: 16px; font-size: 13px; font-family: monospace;">${model.id}</span>`;
});
if (diff.removed.length > 20) {
htmlContent += `<span style="color: #666; font-size: 13px; padding: 4px 12px;">... 还有 ${diff.removed.length - 20} 个</span>`;
}
htmlContent += `</div></div>`;
}
htmlContent += `</div></details>`;
});
// 添加失败站点部分
if (failedSites.length > 0) {
htmlContent += `
<div style="margin-top: 30px; padding: 20px; background: #fff2f0; border: 2px solid #ffccc7; border-radius: 8px;">
<h2 style="color: #cf1322; margin: 0 0 15px 0; font-size: 18px; display: flex; align-items: center; gap: 10px;">
<span style="font-size: 24px;">⚠️</span>
检测失败的站点 (${failedSites.length})
</h2>
<p style="margin: 0 0 15px 0; color: #8c8c8c; font-size: 14px;">
以下站点在本次检测中出现错误,请检查站点配置或网络连接。
</p>
`;
failedSites.forEach((failedSite, index) => {
htmlContent += `
<div style="background: white; border-left: 4px solid #ff4d4f; padding: 12px 15px; margin-bottom: ${index < failedSites.length - 1 ? '12px' : '0'}; border-radius: 4px;">
<div style="display: flex; align-items: flex-start; gap: 10px;">
<span style="background: #ff4d4f; color: white; width: 20px; height: 20px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 12px; flex-shrink: 0; margin-top: 2px;">❌</span>
<div style="flex: 1;">
<div style="font-weight: 600; color: #333; font-size: 15px; margin-bottom: 6px;">${failedSite.siteName}</div>
<div style="color: #8c8c8c; font-size: 13px; font-family: monospace; background: #f5f5f5; padding: 8px 10px; border-radius: 4px; word-break: break-all;">
${failedSite.error}
</div>
</div>
</div>
</div>
`;
});
htmlContent += `</div>`;
}
htmlContent += `
<div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #e8e8e8; color: #999; font-size: 12px; text-align: center;">
<p style="margin: 0;">本邮件由 AI 模型监测系统自动发送</p>
<p style="margin: 5px 0 0 0;">🕔 ${new Date().toLocaleString('zh-CN')}</p>
</div>
</div>
</div>
`;
debugLog('✅ 邮件内容构建完成');
// 准备邮件参数
let subject = `[AI模型监测] 定时检测报告`;
if (siteChanges.length > 0 && failedSites.length > 0) {
subject += ` - ${siteChanges.length}个站点发生变化,${failedSites.length}个站点失败`;
} else if (siteChanges.length > 0) {
subject += ` - ${siteChanges.length}个站点发生变化`;
} else if (failedSites.length > 0) {
subject += ` - ${failedSites.length}个站点检测失败`;
}
const emailParams = {
from: 'AI模型监测 <onboarding@resend.dev>',
to: emails,
subject,
html: htmlContent
};
debugLog('📧 准备发送邮件:', {
from: emailParams.from,
to: emailParams.to,
subject: emailParams.subject,
htmlLength: htmlContent.length
});
// 发送邮件
debugLog('🚀 正在调用 Resend API...');
const result = await resend.emails.send(emailParams);
debugLog('🎉 聚合邮件发送成功!结果:', result);
const logMsg = failedSites.length > 0
? `聚合邮件发送成功: ${siteChanges.length} 个变更, ${failedSites.length} 个失败 -> ${emails.join(', ')}`
: `聚合邮件发送成功: ${siteChanges.length} 个站点 -> ${emails.join(', ')}`;
fastify?.log?.info(logMsg);
return result;
} catch (error) {
debugLog('💥 聚合邮件发送过程出错:', {
errorName: error.name,
errorMessage: error.message,
errorStack: error.stack?.split('\n').slice(0, 3)
});
fastify?.log?.error('聚合邮件通知失败', error);
console.error('[EMAIL] 聚合邮件最终错误:', error.message);
} finally {
debugLog(`=== 聚合邮件通知流程结束 ===`);
}
}
module.exports = { sendModelChangeNotification, sendAggregatedNotification };
|