45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { requireAuth } from '~/server/utils/auth'
|
|
import { updatePageSeo, type PageSeoUpdateInput } from '~/server/utils/page-seo'
|
|
import { fail, ok } from '~/server/utils/response'
|
|
|
|
type SeoUpdateBody = PageSeoUpdateInput & { id: string }
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const auth = requireAuth(event)
|
|
if (!auth) {
|
|
setResponseStatus(event, 401)
|
|
return fail('未授权,请先登录', 401)
|
|
}
|
|
|
|
const body = await readBody<SeoUpdateBody>(event)
|
|
if (!body?.id?.trim()) {
|
|
setResponseStatus(event, 400)
|
|
return fail('缺少 id 参数', 400)
|
|
}
|
|
if (!body.title?.trim() || !body.description?.trim()) {
|
|
return fail('标题和描述不能为空')
|
|
}
|
|
|
|
const updated = await updatePageSeo(
|
|
body.id.trim(),
|
|
{
|
|
title: body.title.trim(),
|
|
description: body.description.trim(),
|
|
keywords: body.keywords?.trim() || undefined,
|
|
ogTitle: body.ogTitle?.trim() || undefined,
|
|
ogDescription: body.ogDescription?.trim() || undefined,
|
|
ogImage: body.ogImage?.trim() || undefined,
|
|
robots: body.robots?.trim() || 'index,follow',
|
|
ogType: body.ogType === 'article' ? 'article' : 'website',
|
|
},
|
|
auth.userId
|
|
)
|
|
|
|
if (!updated) {
|
|
setResponseStatus(event, 404)
|
|
return fail('未找到 SEO 配置', 404)
|
|
}
|
|
|
|
return ok(updated, '保存成功')
|
|
})
|