685c7b51d1
- 账号体系:修改密码(/account + PATCH /api/auth/password)、自助删除账号 (DELETE /api/auth/me)、订阅退订(token + /unsubscribe 页) - 前台/SEO:RSS 订阅 /feed.xml、作品详情分享、canonical URL、 文章 Article JSON-LD、品牌化错误页(i18n) - 后台增强:图片管理 /admin/uploads、操作审计日志 /admin/audit(含清空)、 联系消息已读标记、评论列表分页 - 工程:vitest 测试(密码哈希/内容校验器/种子完整性,12 例)+ pnpm test - DB 迁移:contacts.read、subscribers.token、audit_logs(老库 ALTER 兼容) - 密码逻辑抽为 server/utils/password.ts 供测试
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { readBody, createError } from 'h3'
|
|
import { randomBytes } from 'node:crypto'
|
|
import { getDb } from '../utils/db'
|
|
import { getConfig } from '../utils/config'
|
|
import { sendMail } from '../utils/email'
|
|
import { isValidEmail } from '../utils/validation'
|
|
|
|
/**
|
|
* 邮箱订阅:去重落库(生成退订 token)+ 尽力发送欢迎邮件
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody(event)
|
|
const email = String(body?.email ?? '').trim().toLowerCase()
|
|
|
|
if (!isValidEmail(email)) {
|
|
throw createError({ statusCode: 400, message: '邮箱格式不正确', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
|
|
const db = getDb()
|
|
const existing = db.prepare('SELECT id FROM subscribers WHERE email = ?').get(email)
|
|
if (existing) {
|
|
throw createError({ statusCode: 400, message: '您已在订阅列表中', data: { code: 'ALREADY_SUBSCRIBED' } })
|
|
}
|
|
|
|
const token = randomBytes(24).toString('hex')
|
|
db.prepare('INSERT INTO subscribers (email, token) VALUES (?, ?)').run(email, token)
|
|
|
|
const { siteUrl } = getConfig()
|
|
const unsubscribeUrl = siteUrl ? `${siteUrl}/unsubscribe?token=${token}` : ''
|
|
await sendMail(
|
|
email,
|
|
'欢迎订阅 ArcticNewOne 资讯',
|
|
`感谢订阅 ArcticNewOne!我们会不定期发送精选更新,助您把握设计、开发与行业动态。\n\n如需退订:${unsubscribeUrl || '请联系管理员'}`,
|
|
)
|
|
|
|
return { ok: true, token }
|
|
})
|