提交正式官网
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import {
|
||||
createNewsArticle,
|
||||
deleteNewsArticles,
|
||||
getNewsArticleById,
|
||||
listNewsArticles,
|
||||
updateNewsArticle,
|
||||
type MediaType,
|
||||
type NewsArticleInput,
|
||||
} from '~/server/utils/news'
|
||||
import { fail, ok, pageOk } from '~/server/utils/response'
|
||||
|
||||
function authOrFail(event: Parameters<typeof requireAuth>[0]) {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return null
|
||||
}
|
||||
return auth
|
||||
}
|
||||
|
||||
function parseMediaType(value: unknown): MediaType | null {
|
||||
return value === 'video' ? 'video' : value === 'article' ? 'article' : null
|
||||
}
|
||||
|
||||
function parseInput(body: Partial<NewsArticleInput>): NewsArticleInput | null {
|
||||
const locale = body.locale === 'zh' ? 'zh' : body.locale === 'en' ? 'en' : null
|
||||
const mediaType = parseMediaType(body.mediaType)
|
||||
const title = body.title?.trim()
|
||||
const summary = body.summary?.trim()
|
||||
const thumbnail = body.thumbnail?.trim()
|
||||
|
||||
if (!locale || !mediaType || !title || !summary || !thumbnail) return null
|
||||
|
||||
if (mediaType === 'article') {
|
||||
const url = body.url?.trim()
|
||||
if (!url) return null
|
||||
return {
|
||||
locale,
|
||||
mediaType,
|
||||
title,
|
||||
summary,
|
||||
thumbnail,
|
||||
thumbnailAlt: body.thumbnailAlt?.trim(),
|
||||
url,
|
||||
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
|
||||
status: body.status === 'draft' ? 'draft' : 'published',
|
||||
}
|
||||
}
|
||||
|
||||
const youtubeUrl = body.youtubeUrl?.trim()
|
||||
const tiktokUrl = body.tiktokUrl?.trim()
|
||||
if (!youtubeUrl && !tiktokUrl) return null
|
||||
|
||||
return {
|
||||
locale,
|
||||
mediaType,
|
||||
title,
|
||||
summary,
|
||||
thumbnail,
|
||||
thumbnailAlt: body.thumbnailAlt?.trim(),
|
||||
youtubeUrl: youtubeUrl || undefined,
|
||||
tiktokUrl: tiktokUrl || undefined,
|
||||
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
|
||||
status: body.status === 'draft' ? 'draft' : 'published',
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = authOrFail(event)
|
||||
if (!auth) return fail('未授权,请先登录', 401)
|
||||
|
||||
const method = event.method
|
||||
|
||||
if (method === 'GET') {
|
||||
const query = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(query.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(query.pageSize || '10'), 10))
|
||||
const localeRaw = String(query.locale || '')
|
||||
const mediaTypeRaw = String(query.mediaType || query.media_type || '')
|
||||
const keyword = String(query.keyword || '').trim()
|
||||
const statusRaw = String(query.status || '')
|
||||
|
||||
const { list, total } = await listNewsArticles({
|
||||
page,
|
||||
pageSize,
|
||||
locale: localeRaw === 'en' || localeRaw === 'zh' ? localeRaw : undefined,
|
||||
mediaType: parseMediaType(mediaTypeRaw) || undefined,
|
||||
keyword: keyword || undefined,
|
||||
status: statusRaw === 'published' || statusRaw === 'draft' ? statusRaw : undefined,
|
||||
})
|
||||
|
||||
return pageOk(list, total, page, pageSize)
|
||||
}
|
||||
|
||||
if (method === 'POST') {
|
||||
const body = await readBody<Partial<NewsArticleInput>>(event)
|
||||
const input = parseInput(body)
|
||||
if (!input) {
|
||||
return fail(
|
||||
body.mediaType === 'video'
|
||||
? '请填写完整信息(语言、标题、简介、缩略图,以及 YouTube 或 TikTok 链接至少一项)'
|
||||
: '请填写完整信息(语言、标题、简介、缩略图、原文链接)'
|
||||
)
|
||||
}
|
||||
|
||||
const record = await createNewsArticle(input)
|
||||
return ok(record, '创建成功')
|
||||
}
|
||||
|
||||
if (method === 'PUT') {
|
||||
const body = await readBody<Partial<NewsArticleInput> & { id?: string }>(event)
|
||||
if (!body.id) return fail('内容 ID 不能为空')
|
||||
|
||||
const existing = await getNewsArticleById(body.id)
|
||||
if (!existing) return fail('内容不存在')
|
||||
|
||||
const input = parseInput({
|
||||
locale: body.locale ?? existing.locale,
|
||||
mediaType: body.mediaType ?? existing.mediaType,
|
||||
title: body.title ?? existing.title,
|
||||
summary: body.summary ?? existing.summary,
|
||||
thumbnail: body.thumbnail ?? existing.thumbnail,
|
||||
thumbnailAlt: body.thumbnailAlt ?? existing.thumbnailAlt,
|
||||
url: body.url ?? existing.url,
|
||||
youtubeUrl: body.youtubeUrl ?? existing.youtubeUrl,
|
||||
tiktokUrl: body.tiktokUrl ?? existing.tiktokUrl,
|
||||
sortOrder: body.sortOrder ?? existing.sortOrder,
|
||||
status: body.status ?? existing.status,
|
||||
})
|
||||
if (!input) return fail('请填写完整的内容信息')
|
||||
|
||||
const record = await updateNewsArticle(body.id, input)
|
||||
return ok(record, '更新成功')
|
||||
}
|
||||
|
||||
if (method === 'DELETE') {
|
||||
const ids = await readBody<string[]>(event)
|
||||
if (!Array.isArray(ids) || !ids.length) return fail('请选择要删除的内容')
|
||||
|
||||
await deleteNewsArticles(ids)
|
||||
return ok(null, '删除成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
Reference in New Issue
Block a user