提交正式官网
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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, '保存成功')
|
||||
})
|
||||
Reference in New Issue
Block a user