feat: 后台管理系统(Admin Backend + 全量 CMS)
- 内容迁入数据库:articles/works 表(中英双语 + 内容块), server/utils/seed.ts 灌入现有 6 动态 + 6 作品,站点初始内容不变 - 前台内容 API:/api/news、/api/work(列表/详情,仅已发布), 页面/首页组件改为 useFetch 拉取并按 locale 选 zh/en 字段 - 后台管理 /admin(admin 中间件守卫): - Dashboard 统计、动态/作品增删改(块编辑器、图片上传 /api/admin/upload) - 用户管理(角色切换/删除)、评论审核、联系/订阅管理(导出 CSV) - 预置管理员:NUXT_ADMIN_INITIAL_PASSWORD 首次初始化创建 admin 账号 - 图片自托管:上传存 PVC /data/uploads,/uploads/[...] 路由服务 - sitemap 改查 DB;i18n 移除内容键(305 键对齐);删除 app/data 冗余模块 - 角色以数据库为准(后台可切换);admin 中间件 + 客户端会话恢复插件
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import { readBody, createError, getRouterParam } from 'h3'
|
||||
import { requireAdmin } from '../../../utils/auth'
|
||||
import { getDb } from '../../../utils/db'
|
||||
import { getWorkBySlug } from '../../../utils/content'
|
||||
import {
|
||||
normalizeSlug,
|
||||
isValidSlug,
|
||||
isValidWorkCategory,
|
||||
normalizeTags,
|
||||
normalizeGallery,
|
||||
} from '../../../utils/validateContent'
|
||||
|
||||
/**
|
||||
* 后台更新作品(部分字段)
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
requireAdmin(event)
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
const body = await readBody(event)
|
||||
const db = getDb()
|
||||
|
||||
const existing = db.prepare('SELECT slug FROM works WHERE id = ?').get(id) as { slug: string } | undefined
|
||||
if (!existing) {
|
||||
throw createError({ statusCode: 404, message: '作品不存在', data: { code: 'NOT_FOUND' } })
|
||||
}
|
||||
|
||||
const sets: string[] = []
|
||||
const params: unknown[] = []
|
||||
|
||||
if (body?.slug !== undefined) {
|
||||
const slug = normalizeSlug(body.slug)
|
||||
if (!isValidSlug(slug)) {
|
||||
throw createError({ statusCode: 400, message: 'slug 需为 2-64 位小写字母/数字/中划线', data: { code: 'VALIDATION_ERROR' } })
|
||||
}
|
||||
const taken = db.prepare('SELECT id FROM works WHERE slug = ? AND id != ?').get(slug, id)
|
||||
if (taken) {
|
||||
throw createError({ statusCode: 400, message: 'slug 已存在', data: { code: 'SLUG_TAKEN' } })
|
||||
}
|
||||
sets.push('slug = ?'); params.push(slug)
|
||||
}
|
||||
if (body?.category !== undefined) {
|
||||
if (!isValidWorkCategory(body.category)) {
|
||||
throw createError({ statusCode: 400, message: '分类不合法', data: { code: 'VALIDATION_ERROR' } })
|
||||
}
|
||||
sets.push('category = ?'); params.push(body.category)
|
||||
}
|
||||
for (const field of [
|
||||
'titleZh', 'titleEn', 'descriptionZh', 'descriptionEn',
|
||||
'overviewZh', 'overviewEn', 'challengeZh', 'challengeEn',
|
||||
'solutionZh', 'solutionEn', 'clientZh', 'clientEn',
|
||||
'durationZh', 'durationEn', 'year', 'image', 'url',
|
||||
] as const) {
|
||||
if (body?.[field] !== undefined) {
|
||||
if (field === 'titleZh' || field === 'titleEn') {
|
||||
if (!String(body[field]).trim()) {
|
||||
throw createError({ statusCode: 400, message: '中英文标题不能为空', data: { code: 'VALIDATION_ERROR' } })
|
||||
}
|
||||
}
|
||||
sets.push(fieldToCol(field) + ' = ?'); params.push(String(body[field]).trim())
|
||||
}
|
||||
}
|
||||
if (body?.tags !== undefined) {
|
||||
sets.push('tags = ?'); params.push(JSON.stringify(normalizeTags(body.tags)))
|
||||
}
|
||||
if (body?.gallery !== undefined) {
|
||||
sets.push('gallery = ?'); params.push(JSON.stringify(normalizeGallery(body.gallery)))
|
||||
}
|
||||
if (body?.published !== undefined) {
|
||||
sets.push('published = ?'); params.push(body.published ? 1 : 0)
|
||||
}
|
||||
|
||||
if (sets.length === 0) {
|
||||
throw createError({ statusCode: 400, message: '没有可更新的字段', data: { code: 'VALIDATION_ERROR' } })
|
||||
}
|
||||
|
||||
sets.push("updated_at = datetime('now')")
|
||||
db.prepare(`UPDATE works SET ${sets.join(', ')} WHERE id = ?`).run(...params, id)
|
||||
|
||||
const row = db.prepare('SELECT slug FROM works WHERE id = ?').get(id) as { slug: string }
|
||||
return { ok: true, work: getWorkBySlug(row.slug, { publishedOnly: false }) }
|
||||
})
|
||||
|
||||
function fieldToCol(field: string): string {
|
||||
const map: Record<string, string> = {
|
||||
titleZh: 'title_zh', titleEn: 'title_en',
|
||||
descriptionZh: 'description_zh', descriptionEn: 'description_en',
|
||||
overviewZh: 'overview_zh', overviewEn: 'overview_en',
|
||||
challengeZh: 'challenge_zh', challengeEn: 'challenge_en',
|
||||
solutionZh: 'solution_zh', solutionEn: 'solution_en',
|
||||
clientZh: 'client_zh', clientEn: 'client_en',
|
||||
durationZh: 'duration_zh', durationEn: 'duration_en',
|
||||
year: 'year', image: 'image', url: 'url',
|
||||
}
|
||||
return map[field]
|
||||
}
|
||||
Reference in New Issue
Block a user