admin 编辑页暂无 tags 输入框,保存的文章 tags 为空,前端卡片无小标签显示 (对比老文章都有"草原穿搭/旅拍穿搭"等 tag)。 在 server/utils/article-helpers.js 加 resolveTagsWithFallback: - 前端传 tags 数组(非空)→ 按前端 - 否则从 seoKeywords 用 [,,、] 拆分取前 3 个 - 都没有 → [] POST/PUT 均接入。PUT 时用户改 seoKeywords 也会同步刷新 tags。
41 行
1.7 KiB
JavaScript
41 行
1.7 KiB
JavaScript
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 }
|
||
})
|