4bef241544
- server/api: 注册/登录/登出/会话恢复(JWT + httpOnly Cookie, bcrypt 密码哈希) - server/api: 联系表单与资讯订阅入库,支持 SMTP 邮件通知(可选) - 应用启动自动建表(幂等),docker-compose 新增 PostgreSQL 16 服务 - 前端移除全部 mock:user store、登录注册弹窗、联系表单、页脚订阅接入真实接口 - i18n 补充服务端错误码中英文案,移除 demoHint - deploy: Deployment 环境变量(Secret 引用)、可选集群内 PostgreSQL(postgres.yaml) - 新增 .env.example 与 README/deploy 文档更新
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { useDb } from '../db'
|
|
import { apiError, isValidEmail } from '../utils/api'
|
|
import { sendMail } from '../utils/mail'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody(event)
|
|
const name = String(body?.name || '').trim()
|
|
const email = String(body?.email || '').trim().toLowerCase()
|
|
const subject = String(body?.subject || '').trim()
|
|
const message = String(body?.message || '').trim()
|
|
|
|
// 参数校验
|
|
if (!name || name.length > 100) throw apiError(event, 'contact.invalidName')
|
|
if (!isValidEmail(email)) throw apiError(event, 'contact.invalidEmail')
|
|
if (subject.length > 255) throw apiError(event, 'contact.invalidSubject')
|
|
if (message.length < 10 || message.length > 5000) throw apiError(event, 'contact.invalidMessage')
|
|
|
|
// 入库(无论邮件是否配置成功,留言都不丢失)
|
|
const ip = getRequestIP(event, { xForwardedFor: true }) || null
|
|
await useDb().query(
|
|
`INSERT INTO contact_messages (name, email, subject, message, ip)
|
|
VALUES ($1, $2, $3, $4, $5)`,
|
|
[name, email, subject, message, ip]
|
|
)
|
|
|
|
// 邮件通知(SMTP 未配置时静默跳过,见 sendMail)
|
|
const mailText = [
|
|
`姓名: ${name}`,
|
|
`邮箱: ${email}`,
|
|
`主题: ${subject || '(无主题)'}`,
|
|
``,
|
|
`留言内容:`,
|
|
message,
|
|
``,
|
|
`IP: ${ip || '未知'}`,
|
|
`时间: ${new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })}`,
|
|
].join('\n')
|
|
await sendMail({
|
|
subject: `[网站联系] ${subject || '来自网站的留言'}`,
|
|
text: mailText,
|
|
})
|
|
|
|
return { success: true }
|
|
})
|