wx 9a0afb7cce feat(blog): tags 空时从 SEO 关键词自动 fallback 前 3 个
admin 编辑页暂无 tags 输入框,保存的文章 tags 为空,前端卡片无小标签显示
(对比老文章都有"草原穿搭/旅拍穿搭"等 tag)。

在 server/utils/article-helpers.js 加 resolveTagsWithFallback:
- 前端传 tags 数组(非空)→ 按前端
- 否则从 seoKeywords 用 [,,、] 拆分取前 3 个
- 都没有 → []

POST/PUT 均接入。PUT 时用户改 seoKeywords 也会同步刷新 tags。
2026-04-20 13:54:33 +08:00

41 行
1.7 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

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 fallbackadmin 编辑页暂无 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 }
})