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 供测试
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import { readBody, createError } from 'h3'
|
|
import { requireAdmin } from '../../utils/auth'
|
|
import { getDb } from '../../utils/db'
|
|
import { getWorkBySlug } from '../../utils/content'
|
|
import { writeAudit } from '../../utils/audit'
|
|
import {
|
|
normalizeSlug,
|
|
isValidSlug,
|
|
isValidWorkCategory,
|
|
normalizeTags,
|
|
normalizeGallery,
|
|
} from '../../utils/validateContent'
|
|
|
|
/**
|
|
* 后台新增作品
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
requireAdmin(event)
|
|
const body = await readBody(event)
|
|
const db = getDb()
|
|
|
|
const slug = normalizeSlug(body?.slug)
|
|
if (!isValidSlug(slug)) {
|
|
throw createError({ statusCode: 400, message: 'slug 需为 2-64 位小写字母/数字/中划线', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
if (!isValidWorkCategory(body?.category)) {
|
|
throw createError({ statusCode: 400, message: '分类不合法', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
const titleZh = String(body?.titleZh ?? '').trim()
|
|
const titleEn = String(body?.titleEn ?? '').trim()
|
|
if (!titleZh || !titleEn) {
|
|
throw createError({ statusCode: 400, message: '中英文标题不能为空', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
if (getWorkBySlug(slug, { publishedOnly: false })) {
|
|
throw createError({ statusCode: 400, message: 'slug 已存在', data: { code: 'SLUG_TAKEN' } })
|
|
}
|
|
|
|
db.prepare(
|
|
`INSERT INTO works
|
|
(slug, category, title_zh, title_en, description_zh, description_en,
|
|
overview_zh, overview_en, challenge_zh, challenge_en, solution_zh, solution_en,
|
|
client_zh, client_en, duration_zh, duration_en, year, tags, image, gallery, url, published, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))`,
|
|
).run(
|
|
slug,
|
|
body.category,
|
|
titleZh,
|
|
titleEn,
|
|
String(body?.descriptionZh ?? ''),
|
|
String(body?.descriptionEn ?? ''),
|
|
String(body?.overviewZh ?? ''),
|
|
String(body?.overviewEn ?? ''),
|
|
String(body?.challengeZh ?? ''),
|
|
String(body?.challengeEn ?? ''),
|
|
String(body?.solutionZh ?? ''),
|
|
String(body?.solutionEn ?? ''),
|
|
String(body?.clientZh ?? ''),
|
|
String(body?.clientEn ?? ''),
|
|
String(body?.durationZh ?? ''),
|
|
String(body?.durationEn ?? ''),
|
|
String(body?.year ?? ''),
|
|
JSON.stringify(normalizeTags(body?.tags)),
|
|
body?.image ? String(body.image).trim() : null,
|
|
JSON.stringify(normalizeGallery(body?.gallery)),
|
|
body?.url ? String(body.url).trim() : null,
|
|
body?.published ? 1 : 0,
|
|
)
|
|
|
|
writeAudit(event, 'create', `work:${slug}`)
|
|
return { ok: true, work: getWorkBySlug(slug, { publishedOnly: false }) }
|
|
})
|