提交正式官网
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
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)
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { listPageSeo } 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 queryParams = getQuery(event)
|
||||
const locale = String(queryParams.locale || '')
|
||||
const keyword = String(queryParams.keyword || '')
|
||||
|
||||
const list = await listPageSeo({
|
||||
locale: locale === 'en' || locale === 'zh' ? locale : undefined,
|
||||
keyword: keyword || undefined,
|
||||
})
|
||||
|
||||
return ok(list)
|
||||
})
|
||||
@@ -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