const PROVIDERS = { SMS: { SPUG: { label: "Spug短信", configFields: [ { key: "baseUrl", label: "基础地址", placeholder: "https://push.spug.cc", required: true } ], secretFields: [ { key: "apiKey", label: "API Key", required: true } ] }, MOCK_SMS: { label: "开发Mock短信", configFields: [], secretFields: [] } }, EMAIL: { GMAIL: { label: "Gmail SMTP", configFields: [ { key: "host", label: "SMTP主机", placeholder: "smtp.gmail.com", required: true, defaultValue: "smtp.gmail.com" }, { key: "port", label: "SMTP端口", placeholder: "587", required: true, defaultValue: "587" }, { key: "username", label: "SMTP用户名", placeholder: "your-account@gmail.com", required: true }, { key: "fromAddress", label: "发件人地址", placeholder: "your-account@gmail.com", required: true }, { key: "auth", label: "启用认证", type: "checkbox", defaultValue: true }, { key: "starttls", label: "启用 STARTTLS", type: "checkbox", defaultValue: true }, { key: "ssl", label: "启用 SSL", type: "checkbox", defaultValue: false } ], secretFields: [ { key: "password", label: "SMTP密码 / 授权码", required: true } ] }, QQMAIL: { label: "QQ邮箱 SMTP", configFields: [ { key: "host", label: "SMTP主机", placeholder: "smtp.qq.com", required: true, defaultValue: "smtp.qq.com" }, { key: "port", label: "SMTP端口", placeholder: "587", required: true, defaultValue: "587" }, { key: "username", label: "SMTP用户名", placeholder: "your-account@qq.com", required: true }, { key: "fromAddress", label: "发件人地址", placeholder: "your-account@qq.com", required: true }, { key: "auth", label: "启用认证", type: "checkbox", defaultValue: true }, { key: "starttls", label: "启用 STARTTLS", type: "checkbox", defaultValue: true }, { key: "ssl", label: "启用 SSL", type: "checkbox", defaultValue: false } ], secretFields: [ { key: "password", label: "SMTP密码 / 授权码", required: true } ] }, MOCK_EMAIL: { label: "开发Mock邮件", configFields: [ { key: "fromAddress", label: "模拟发件人", placeholder: "mock@messagehub.local", defaultValue: "mock@messagehub.local" } ], secretFields: [] } } }; const state = { channels: [], editingChannel: null }; const elements = { channelTableBody: document.getElementById("channelTableBody"), channelEmptyState: document.getElementById("channelEmptyState"), refreshButton: document.getElementById("refreshButton"), createButton: document.getElementById("createButton"), resetButton: document.getElementById("resetButton"), channelForm: document.getElementById("channelForm"), channelId: document.getElementById("channelId"), channelType: document.getElementById("channelType"), providerCode: document.getElementById("providerCode"), channelName: document.getElementById("channelName"), remark: document.getElementById("remark"), enabled: document.getElementById("enabled"), isDefault: document.getElementById("isDefault"), dynamicFields: document.getElementById("dynamicFields"), formTitle: document.getElementById("formTitle"), formSubtitle: document.getElementById("formSubtitle"), testForm: document.getElementById("testForm"), testChannelId: document.getElementById("testChannelId"), testReceiver: document.getElementById("testReceiver"), testSubject: document.getElementById("testSubject"), testTemplateCode: document.getElementById("testTemplateCode"), testContent: document.getElementById("testContent"), testTemplateParams: document.getElementById("testTemplateParams"), toast: document.getElementById("toast") }; function showToast(message, isError = false) { elements.toast.hidden = false; elements.toast.textContent = message; elements.toast.style.background = isError ? "rgba(146, 34, 34, 0.92)" : "rgba(44, 35, 24, 0.92)"; clearTimeout(showToast.timer); showToast.timer = setTimeout(() => { elements.toast.hidden = true; }, 2800); } async function request(url, options = {}) { const response = await fetch(url, { headers: { "Content-Type": "application/json", ...(options.headers || {}) }, ...options }); const payload = await response.json().catch(() => ({ code: 1, message: "服务响应解析失败" })); if (!response.ok || payload.code !== 0) { throw new Error(payload.message || "请求失败"); } return payload.data; } function providerSchema(channelType, providerCode) { return PROVIDERS[channelType][providerCode]; } function fillProviderOptions() { const channelType = elements.channelType.value; const options = Object.entries(PROVIDERS[channelType]).map(([code, schema]) => { const selected = code === elements.providerCode.value ? "selected" : ""; return ``; }); elements.providerCode.innerHTML = options.join(""); if (!PROVIDERS[channelType][elements.providerCode.value]) { elements.providerCode.value = Object.keys(PROVIDERS[channelType])[0]; } renderDynamicFields(); } function renderDynamicFields() { const schema = providerSchema(elements.channelType.value, elements.providerCode.value); const config = state.editingChannel?.config || {}; const secretMask = state.editingChannel?.secretConfigMask || {}; const configFields = schema.configFields.map((field) => { const value = config[field.key] ?? field.defaultValue ?? ""; if (field.type === "checkbox") { return ` `; } return ` `; }); const secretFields = schema.secretFields.map((field) => { const placeholder = secretMask[field.key] ? `当前值:${secretMask[field.key]};留空则保持不变` : ""; return ` `; }); elements.dynamicFields.innerHTML = [...configFields, ...secretFields].join(""); } function resetForm(channel = null) { state.editingChannel = channel; elements.channelId.value = channel?.id || ""; elements.channelType.value = channel?.channelType || "SMS"; fillProviderOptions(); elements.providerCode.value = channel?.providerCode || elements.providerCode.value; elements.channelName.value = channel?.channelName || ""; elements.remark.value = channel?.remark || ""; elements.enabled.checked = channel?.enabled ?? true; elements.isDefault.checked = channel?.isDefault ?? false; elements.formTitle.textContent = channel ? "编辑通道" : "新增通道"; elements.formSubtitle.textContent = channel ? "留空的敏感字段会保留原值。" : "保存后即可加入发送主链路。"; renderDynamicFields(); } function statusBadge(channel) { if (!channel.enabled) { return `已停用`; } return channel.isDefault ? `已启用 / 默认` : `已启用`; } function healthBadge(channel) { if (channel.healthStatus === "HEALTHY") { return `正常`; } if (channel.healthStatus === "UNHEALTHY") { return `异常`; } return `待检测`; } function renderChannelTable() { elements.channelTableBody.innerHTML = ""; elements.channelEmptyState.hidden = state.channels.length > 0; state.channels.forEach((channel) => { const row = document.createElement("tr"); row.innerHTML = `