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