Files
arcticnewone/server/api/admin/articles/[id].patch.ts
T
virtheart 685c7b51d1 feat: 收官补齐全部未实现功能
- 账号体系:修改密码(/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 供测试
2026-08-06 17:26:26 +08:00

85 lines
3.4 KiB
TypeScript

import { readBody, createError, getRouterParam } from 'h3'
import { requireAdmin } from '../../../utils/auth'
import { getDb } from '../../../utils/db'
import { getArticleBySlug } from '../../../utils/content'
import { normalizeSlug, isValidSlug, isValidArticleCategory, normalizeBlocks } from '../../../utils/validateContent'
import { writeAudit } from '../../../utils/audit'
/**
* 后台更新动态(部分字段)
*/
export default defineEventHandler(async (event) => {
requireAdmin(event)
const id = Number(getRouterParam(event, 'id'))
const body = await readBody(event)
const db = getDb()
const existing = db.prepare('SELECT slug FROM articles WHERE id = ?').get(id) as { slug: string } | undefined
if (!existing) {
throw createError({ statusCode: 404, message: '动态不存在', data: { code: 'NOT_FOUND' } })
}
const sets: string[] = []
const params: unknown[] = []
if (body?.slug !== undefined) {
const slug = normalizeSlug(body.slug)
if (!isValidSlug(slug)) {
throw createError({ statusCode: 400, message: 'slug 需为 2-64 位小写字母/数字/中划线', data: { code: 'VALIDATION_ERROR' } })
}
const taken = db.prepare('SELECT id FROM articles WHERE slug = ? AND id != ?').get(slug, id)
if (taken) {
throw createError({ statusCode: 400, message: 'slug 已存在', data: { code: 'SLUG_TAKEN' } })
}
sets.push('slug = ?'); params.push(slug)
}
if (body?.category !== undefined) {
if (!isValidArticleCategory(body.category)) {
throw createError({ statusCode: 400, message: '分类不合法', data: { code: 'VALIDATION_ERROR' } })
}
sets.push('category = ?'); params.push(body.category)
}
for (const field of ['titleZh', 'titleEn', 'excerptZh', 'excerptEn', 'image', 'date', 'author'] as const) {
if (body?.[field] !== undefined) {
if (field === 'titleZh' || field === 'titleEn') {
if (!String(body[field]).trim()) {
throw createError({ statusCode: 400, message: '中英文标题不能为空', data: { code: 'VALIDATION_ERROR' } })
}
}
sets.push(fieldToCol(field) + ' = ?'); params.push(String(body[field]).trim())
}
}
if (body?.readTime !== undefined) {
sets.push('read_time = ?'); params.push(Number(body.readTime) || 5)
}
if (body?.published !== undefined) {
sets.push('published = ?'); params.push(body.published ? 1 : 0)
}
if (body?.contentZh !== undefined) {
sets.push('content_zh = ?'); params.push(JSON.stringify(normalizeBlocks(body.contentZh)))
}
if (body?.contentEn !== undefined) {
sets.push('content_en = ?'); params.push(JSON.stringify(normalizeBlocks(body.contentEn)))
}
if (sets.length === 0) {
throw createError({ statusCode: 400, message: '没有可更新的字段', data: { code: 'VALIDATION_ERROR' } })
}
sets.push("updated_at = datetime('now')")
db.prepare(`UPDATE articles SET ${sets.join(', ')} WHERE id = ?`).run(...params, id)
const row = db.prepare('SELECT slug FROM articles WHERE id = ?').get(id) as { slug: string }
writeAudit(event, 'update', `article:${id}`)
return { ok: true, article: getArticleBySlug(row.slug, { publishedOnly: false }) }
})
function fieldToCol(field: string): string {
const map: Record<string, string> = {
titleZh: 'title_zh', titleEn: 'title_en',
excerptZh: 'excerpt_zh', excerptEn: 'excerpt_en',
image: 'image', date: 'date', author: 'author',
}
return map[field]
}