feat: Next.js 后端 + SQLite + 暗门后台 /virtheart + 服务咨询页
后端(node:sqlite 零原生依赖,data/site.db,WAL + busy_timeout 抗并发): - works / service_items / site_config / inquiries / admin_users / admin_sessions / captcha_challenges - API:admin 登录登出/密码、作品与服务 CRUD、图片上传、留言箱、 验证码 challenge/verify、咨询提交(一次性滑块令牌防重放) 后台 /virtheart(仅手动访问,未登录一律 307 → /virtheart/login): - 服务端布局鉴权 + DB 会话(httpOnly cookie 7 天)+ CSRF 头校验 - 概览 / 作品(上传、网格预设、发布开关)/ 服务 / 留言箱 / 配置(全站文案) - 设计与主站同语言:暗底点阵、mono、lime 选中,非传统后台模板 公开站: - 路由组 (site) 隔离站点壳层;首页/服务页文案全部 DB 驱动即时生效 - /services 服务菜单 + 留言表单 + 全屏居中滑块拼图验证码(对齐缺口) - 提交入 inquiries 并 SMTP(飞书 notify@)邮件通知 chenchun@virtheart.com - Header 新增「服务」菜单;旧作品清理,空状态自动接管 - 根布局仅承载字体/全局样式,后台无站点壳层 验证:登录/CSRF/发布隔离/滑块容错/令牌单次消费/SMTP 真发全部通过; lint 与基线一致(119 errors 均为存量)。
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* SMTP 邮件 — 飞书邮箱(notify@virtheart.com)。
|
||||
* 凭据全部来自环境变量(.env.local,不入库不入 git)。
|
||||
*/
|
||||
import nodemailer from "nodemailer";
|
||||
|
||||
const SMTP_HOST = process.env.SMTP_HOST || "smtp.feishu.cn";
|
||||
const SMTP_PORT = Number(process.env.SMTP_PORT || 465);
|
||||
const SMTP_USER = process.env.SMTP_USER || "";
|
||||
const SMTP_PASS = process.env.SMTP_PASS || "";
|
||||
const NOTIFY_TO = process.env.NOTIFY_TO || "chenchun@virtheart.com";
|
||||
|
||||
let transporter: nodemailer.Transporter | null = null;
|
||||
|
||||
function getTransporter(): nodemailer.Transporter {
|
||||
if (!transporter) {
|
||||
transporter = nodemailer.createTransport({
|
||||
host: SMTP_HOST,
|
||||
port: SMTP_PORT,
|
||||
secure: SMTP_PORT === 465,
|
||||
auth: SMTP_USER ? { user: SMTP_USER, pass: SMTP_PASS } : undefined,
|
||||
});
|
||||
}
|
||||
return transporter;
|
||||
}
|
||||
|
||||
export type InquiryEmailData = {
|
||||
id: number;
|
||||
name: string;
|
||||
contact: string;
|
||||
service_title: string;
|
||||
message: string;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export async function sendInquiryNotification(
|
||||
data: InquiryEmailData
|
||||
): Promise<void> {
|
||||
if (!SMTP_USER) {
|
||||
throw new Error("SMTP_USER 未配置(.env.local)");
|
||||
}
|
||||
const rows: [string, string][] = [
|
||||
["留言编号", `#${data.id}`],
|
||||
["姓名", data.name],
|
||||
["联系方式", data.contact],
|
||||
["感兴趣的服务", data.service_title || "(未选择)"],
|
||||
["提交时间", data.created_at],
|
||||
];
|
||||
const html = `
|
||||
<div style="font-family:-apple-system,'PingFang SC','Microsoft YaHei',sans-serif;max-width:560px">
|
||||
<h2 style="font-size:16px">新服务咨询 · 北极新一</h2>
|
||||
<table style="border-collapse:collapse;width:100%;font-size:14px">
|
||||
${rows
|
||||
.map(
|
||||
([k, v]) =>
|
||||
`<tr><td style="padding:6px 12px 6px 0;color:#666;white-space:nowrap;vertical-align:top">${k}</td><td style="padding:6px 0"><strong>${escapeHtml(
|
||||
v
|
||||
)}</strong></td></tr>`
|
||||
)
|
||||
.join("")}
|
||||
</table>
|
||||
<div style="margin-top:16px;padding:12px;background:#f6f6f2;border-radius:8px;font-size:14px;white-space:pre-wrap">${escapeHtml(
|
||||
data.message
|
||||
)}</div>
|
||||
<p style="color:#999;font-size:12px;margin-top:16px">可登录 /virtheart 后台留言箱查看与更新状态。</p>
|
||||
</div>`;
|
||||
|
||||
await getTransporter().sendMail({
|
||||
from: `"通知邮箱" <${SMTP_USER}>`,
|
||||
to: NOTIFY_TO,
|
||||
subject: `【服务咨询 #${data.id}】${data.name} - ${
|
||||
data.service_title || "未选择服务"
|
||||
}`,
|
||||
text: rows.map(([k, v]) => `${k}:${v}`).join("\n") + `\n\n留言:\n${data.message}`,
|
||||
html,
|
||||
});
|
||||
}
|
||||
|
||||
function escapeHtml(s: string) {
|
||||
return s
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
Reference in New Issue
Block a user