hulai-website/server/api/admin/articles.post.js
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

36 行
1.1 KiB
JavaScript

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 body = await readBody(event)
if (!body?.title || !body?.slug) {
throw createError({ statusCode: 422, message: '标题和 slug 不能为空' })
}
const db = useDB()
const now = new Date().toISOString()
const result = db.insert(schema.articles).values({
slug: body.slug,
title: body.title,
summary: body.summary || '',
content: body.content || '',
category: body.category || '',
type: body.type || 'news',
coverImage: body.coverImage || null,
author: body.author || '呼籁旅行',
tags: JSON.stringify(resolveTagsWithFallback(body)),
seoTitle: body.seoTitle || null,
seoDesc: body.seoDesc || null,
seoKeywords: body.seoKeywords || null,
published: body.published !== false ? 1 : 0,
createdAt: now,
updatedAt: now,
}).run()
return { success: true, id: result.lastInsertRowid }
})