import { requireAdmin } from "#server/utils/admin-guard"; import { setGlobalConfigValue } from "#server/service/config"; import { delCache } from "#server/utils/context"; interface EmailConfigBody { host?: string; port?: number; user?: string; pass?: string; from?: string; secure?: boolean; } export default defineWrappedResponseHandler(async (event) => { await requireAdmin(event); const body = await readBody(event); const updates: Promise[] = []; if (body.host !== undefined) { updates.push(setGlobalConfigValue("smtpHost", body.host)); } if (body.port !== undefined) { updates.push(setGlobalConfigValue("smtpPort", body.port)); } if (body.user !== undefined) { updates.push(setGlobalConfigValue("smtpUser", body.user)); } if (body.pass !== undefined && body.pass !== "") { // Only update password if a new non-empty value is provided updates.push(setGlobalConfigValue("smtpPass", body.pass)); } if (body.from !== undefined) { updates.push(setGlobalConfigValue("smtpFrom", body.from)); } if (body.secure !== undefined) { updates.push(setGlobalConfigValue("smtpSecure", body.secure)); } await Promise.all(updates); // Invalidate config caches await delCache("config:global:public"); await delCache("config:global:admin"); return R.success({ message: "邮件配置已保存" }); });