import { eq } from 'drizzle-orm' import { useDB, schema } from '../../../utils/db.js' import { requireAdmin } from '../../../utils/auth.js' import { resolveTagsWithFallback } from '../../../utils/article-helpers.js' export default defineEventHandler(async (event) => { requireAdmin(event) const id = Number(getRouterParam(event, 'id')) const body = await readBody(event) const db = useDB() const updateData = { updatedAt: new Date().toISOString() } if (body.title !== undefined) updateData.title = body.title if (body.slug !== undefined) updateData.slug = body.slug if (body.summary !== undefined) updateData.summary = body.summary if (body.content !== undefined) updateData.content = body.content if (body.category !== undefined) updateData.category = body.category if (body.coverImage !== undefined) updateData.coverImage = body.coverImage if (body.author !== undefined) updateData.author = body.author if (body.seoTitle !== undefined) updateData.seoTitle = body.seoTitle if (body.seoDesc !== undefined) updateData.seoDesc = body.seoDesc if (body.seoKeywords !== undefined) updateData.seoKeywords = body.seoKeywords // tags:前端传了用前端的;没传/空数组时从 seoKeywords fallback(admin 编辑页暂无 tags 输入) if (body.tags !== undefined) { updateData.tags = JSON.stringify(resolveTagsWithFallback(body)) } else if (body.seoKeywords !== undefined) { // 用户改了 seoKeywords 但没传 tags,同步更新 tags updateData.tags = JSON.stringify(resolveTagsWithFallback(body)) } if (body.published !== undefined) updateData.published = body.published ? 1 : 0 db.update(schema.articles) .set(updateData) .where(eq(schema.articles.id, id)) .run() return { success: true } })