/* 系统设置页:邮件通知 + 大模型接口(舆情/跟踪已移至 /automation) */ let curSettings = null; async function loadSettings() { try { curSettings = await api('/api/settings'); const mail = curSettings.mail, llm = curSettings.llm; $('#emailEnabled').checked = !!mail.email_enabled; $('#smtpHost').value = mail.smtp_host; $('#smtpPort').value = mail.smtp_port; $('#smtpMode').value = mail.smtp_mode || 'plain'; $('#smtpUser').value = mail.smtp_user; $('#smtpPass').value = mail.smtp_pass; $('#senderName').value = mail.sender_name; $('#emailTo').value = mail.email_to; $('#llmBaseUrl').value = llm.base_url; $('#llmApiKey').value = llm.api_key; $('#llmModel').value = llm.model; } catch (e) { toast('设置加载失败'); } } function collectMail() { return { smtp_host: $('#smtpHost').value.trim(), smtp_port: $('#smtpPort').value, smtp_mode: $('#smtpMode').value, smtp_user: $('#smtpUser').value.trim(), smtp_pass: $('#smtpPass').value, sender_name: $('#senderName').value.trim(), email_to: $('#emailTo').value.trim(), email_enabled: $('#emailEnabled').checked }; } async function save(which) { try { const body = {}; if (which === 'mail') body.mail = collectMail(); if (which === 'llm') body.llm = { llm_base_url: $('#llmBaseUrl').value.trim(), llm_api_key: $('#llmApiKey').value.trim(), llm_model: $('#llmModel').value.trim() }; const r = await api('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); toast(r.msg || '已保存'); } catch (e) { toast('保存失败:' + e.message); } } async function testEmail() { const btn = event.target; btn.disabled = true; btn.textContent = '发送中…'; try { await api('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ mail: collectMail() }) }); const r = await api('/api/settings/test-email', { method: 'POST' }); toast(r.msg || '发送成功'); } catch (e) { toast('发送失败:' + e.message); } btn.disabled = false; btn.textContent = '📨 发送测试邮件'; } async function testLlm() { const btn = event.target; btn.disabled = true; btn.textContent = '测试中…'; try { await api('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ llm: { llm_base_url: $('#llmBaseUrl').value.trim(), llm_api_key: $('#llmApiKey').value.trim(), llm_model: $('#llmModel').value.trim() } }) }); const r = await api('/api/settings/test-llm', { method: 'POST' }); toast(r.msg || '连接正常'); } catch (e) { toast('连接失败:' + e.message); } btn.disabled = false; btn.textContent = '🔌 测试连接'; } loadSettings();