59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { requireAuth } from '~/server/utils/auth'
|
|
import { getPageSeoById, updatePageSeo, type PageSeoUpdateInput } from '~/server/utils/page-seo'
|
|
import { fail, ok } from '~/server/utils/response'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const auth = requireAuth(event)
|
|
if (!auth) {
|
|
setResponseStatus(event, 401)
|
|
return fail('未授权,请先登录', 401)
|
|
}
|
|
|
|
const id = getRouterParam(event, 'id')
|
|
if (!id) {
|
|
setResponseStatus(event, 400)
|
|
return fail('缺少 id 参数', 400)
|
|
}
|
|
|
|
if (event.method === 'GET') {
|
|
const record = await getPageSeoById(id)
|
|
if (!record) {
|
|
setResponseStatus(event, 404)
|
|
return fail('未找到 SEO 配置', 404)
|
|
}
|
|
return ok(record)
|
|
}
|
|
|
|
if (event.method === 'PUT') {
|
|
const body = await readBody<PageSeoUpdateInput>(event)
|
|
if (!body?.title?.trim() || !body?.description?.trim()) {
|
|
return fail('标题和描述不能为空')
|
|
}
|
|
|
|
const updated = await updatePageSeo(
|
|
id,
|
|
{
|
|
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, '保存成功')
|
|
}
|
|
|
|
setResponseStatus(event, 405)
|
|
return fail('Method Not Allowed', 405)
|
|
})
|