v2.1.2: auto调用全部失败时邮件通知 + 网页配置邮箱

- 新增邮件通知: auto 配置被调用后所有提供商都失败(含无可用提供商)时自动发邮件
- 通知路径: chat_completions 400/503 + 通用能力端点(生图/语音/视频) + ASR端点
- 防轰炸: cooldown_seconds(默认300s)最小间隔, 连续失败只发一封(实测3次失败1封)
- 网页配置: 系统配置页新增「邮件通知」卡片, 支持 SMTP服务器/端口/加密(plain/starttls/ssl)/账号密码/发件人/收件人/间隔 + 发送测试邮件按钮
- API: GET/PUT /api/admin/email(密码掩码), POST /api/admin/email/test
- 默认SMTP: mail.tphai.com:587 plain, 收件人 wlq@tphai.com
- 修复: config页loadEmailConfig竞态(卡片未渲染就填充); 邮件失败不影响API响应
This commit is contained in:
2026-08-28 13:17:31 +08:00
parent d387d5ba09
commit 904a4d0d6c
4 changed files with 378 additions and 2 deletions
+158
View File
@@ -110,6 +110,14 @@
</div>
</div>
<div class="bg-white rounded-xl border border-gray-100 p-6">
<h2 class="text-lg font-semibold text-gray-800 mb-1">邮件通知(auto 调用全部失败时通知)</h2>
<p class="text-sm text-gray-500 mb-4">某个 auto 配置被调用后所有提供商都失败,自动发送邮件到收件人</p>
<div id="emailConfigContent" class="space-y-4">
<p class="text-gray-500 text-sm">加载中...</p>
</div>
</div>
<div class="bg-white rounded-xl border border-gray-100 p-6">
<h2 class="text-lg font-semibold text-gray-800 mb-4">提供商配置</h2>
<div class="space-y-4">
@@ -151,6 +159,7 @@
</p>
</div>
`;
loadEmailConfig();
}
async function toggleCacheModel() {
@@ -192,6 +201,155 @@
}
}
async function loadEmailConfig() {
const res = await fetch('/api/admin/email');
const e = await res.json();
const container = document.getElementById('emailConfigContent');
container.innerHTML = `
<div class="grid grid-cols-2 gap-4">
<div class="flex items-center gap-2 p-3 bg-gray-50 rounded-lg">
<span class="text-sm text-gray-700">启用邮件通知</span>
<button id="emailEnabledToggle" onclick="toggleEmailEnabled()" class="relative w-10 h-5 rounded-full transition ${e.enabled ? 'bg-green-500' : 'bg-gray-300'}">
<span id="emailEnabledKnob" class="absolute top-0.5 w-4 h-4 bg-white rounded-full shadow transition ${e.enabled ? 'left-5' : 'left-0.5'}"></span>
</button>
</div>
<div class="flex items-center gap-2 p-3 bg-gray-50 rounded-lg">
<span class="text-sm text-gray-700">auto 失败时通知</span>
<button id="notifyToggle" onclick="toggleNotify()" class="relative w-10 h-5 rounded-full transition ${e.notify_on_auto_failure ? 'bg-green-500' : 'bg-gray-300'}">
<span id="notifyKnob" class="absolute top-0.5 w-4 h-4 bg-white rounded-full shadow transition ${e.notify_on_auto_failure ? 'left-5' : 'left-0.5'}"></span>
</button>
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">SMTP 服务器</label>
<input type="text" id="emailHost" value="${e.smtp_host}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">端口</label>
<input type="number" id="emailPort" value="${e.smtp_port}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">加密方式</label>
<select id="emailMode" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
<option value="plain" ${e.smtp_mode==='plain'?'selected':''}>plain(无加密)</option>
<option value="starttls" ${e.smtp_mode==='starttls'?'selected':''}>STARTTLS</option>
<option value="ssl" ${e.smtp_mode==='ssl'?'selected':''}>SSL</option>
</select>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">账号</label>
<input type="text" id="emailUser" value="${e.smtp_user}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">密码(留空不改)</label>
<input type="password" id="emailPass" value="${e.smtp_password}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">发件人名称</label>
<input type="text" id="emailFromName" value="${e.from_name}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">发件地址</label>
<input type="text" id="emailFromAddr" value="${e.from_addr}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">收件人(逗号分隔)</label>
<input type="text" id="emailTo" value="${(e.to_addrs||[]).join(', ')}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
<div class="flex items-end gap-3">
<div class="flex-1">
<label class="block text-sm font-medium text-gray-700 mb-1">失败通知最小间隔(秒)</label>
<input type="number" id="emailCooldown" value="${e.cooldown_seconds}" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
</div>
<button onclick="saveEmail()" class="px-4 py-2 gradient-bg text-white rounded-lg hover:opacity-90">
<i class="ri-save-line mr-1"></i> 保存
</button>
<button onclick="testEmail(this)" class="px-4 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600">
<i class="ri-mail-send-line mr-1"></i> 发送测试邮件
</button>
</div>
`;
}
async function toggleEmailEnabled() {
const res = await fetch('/api/admin/email');
const e = await res.json();
const newVal = !e.enabled;
const r = await fetch('/api/admin/email', {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled: newVal })
});
const d = await r.json();
if (d.success) {
const btn = document.getElementById('emailEnabledToggle');
const knob = document.getElementById('emailEnabledKnob');
btn.classList.toggle('bg-green-500', newVal); btn.classList.toggle('bg-gray-300', !newVal);
knob.classList.toggle('left-5', newVal); knob.classList.toggle('left-0.5', !newVal);
} else { alert('保存失败: ' + (d.error || '')); }
}
async function toggleNotify() {
const res = await fetch('/api/admin/email');
const e = await res.json();
const newVal = !e.notify_on_auto_failure;
const r = await fetch('/api/admin/email', {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ notify_on_auto_failure: newVal })
});
const d = await r.json();
if (d.success) {
const btn = document.getElementById('notifyToggle');
const knob = document.getElementById('notifyKnob');
btn.classList.toggle('bg-green-500', newVal); btn.classList.toggle('bg-gray-300', !newVal);
knob.classList.toggle('left-5', newVal); knob.classList.toggle('left-0.5', !newVal);
} else { alert('保存失败: ' + (d.error || '')); }
}
async function saveEmail() {
const data = {
smtp_host: document.getElementById('emailHost').value.trim(),
smtp_port: parseInt(document.getElementById('emailPort').value) || 25,
smtp_mode: document.getElementById('emailMode').value,
smtp_user: document.getElementById('emailUser').value.trim(),
from_name: document.getElementById('emailFromName').value.trim(),
from_addr: document.getElementById('emailFromAddr').value.trim(),
to_addrs: document.getElementById('emailTo').value,
cooldown_seconds: parseInt(document.getElementById('emailCooldown').value) || 300
};
const pass = document.getElementById('emailPass').value;
if (pass && pass !== '********') data.smtp_password = pass;
const res = await fetch('/api/admin/email', {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const d = await res.json();
if (d.success) {
alert('✅ 邮件配置已保存');
loadEmailConfig();
} else { alert('保存失败: ' + (d.error || '')); }
}
async function testEmail(btn) {
if (!btn) btn = event.target.closest('button');
btn.disabled = true; btn.innerHTML = '<i class="ri-loader-4-line animate-spin mr-1"></i> 发送中...';
try {
const res = await fetch('/api/admin/email/test', { method: 'POST' });
const d = await res.json();
if (d.success) { alert('✅ ' + d.message); }
else { alert('❌ ' + (d.error || '发送失败')); }
} catch (e) {
alert('❌ ' + e.message);
}
btn.disabled = false; btn.innerHTML = '<i class="ri-mail-send-line mr-1"></i> 发送测试邮件';
}
loadConfig();
</script>
</body>