d67849a311
- 内容迁入数据库: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 中间件 + 客户端会话恢复插件
167 lines
5.6 KiB
TypeScript
167 lines
5.6 KiB
TypeScript
import { getDb, type ArticleBlock } from './db'
|
|
|
|
/**
|
|
* 内容读取层(动态/作品)。
|
|
* 统一负责 DB 行 -> 业务对象(解析 content/tags/gallery 的 JSON),
|
|
* 供前台内容 API、后台管理 API、sitemap 共用。
|
|
*/
|
|
|
|
export interface Article {
|
|
id: number
|
|
slug: string
|
|
category: 'insights' | 'projects' | 'announcements'
|
|
titleZh: string
|
|
titleEn: string
|
|
excerptZh: string
|
|
excerptEn: string
|
|
contentZh: ArticleBlock[]
|
|
contentEn: ArticleBlock[]
|
|
image: string | null
|
|
date: string | null
|
|
readTime: number
|
|
author: string
|
|
published: number
|
|
createdAt: string
|
|
updatedAt: string
|
|
}
|
|
|
|
export interface Work {
|
|
id: number
|
|
slug: string
|
|
category: 'webDesign' | 'branding' | 'development'
|
|
titleZh: string
|
|
titleEn: string
|
|
descriptionZh: string
|
|
descriptionEn: string
|
|
overviewZh: string
|
|
overviewEn: string
|
|
challengeZh: string
|
|
challengeEn: string
|
|
solutionZh: string
|
|
solutionEn: string
|
|
clientZh: string
|
|
clientEn: string
|
|
durationZh: string
|
|
durationEn: string
|
|
year: string
|
|
tags: string[]
|
|
image: string | null
|
|
gallery: string[]
|
|
url: string | null
|
|
published: number
|
|
createdAt: string
|
|
updatedAt: string
|
|
}
|
|
|
|
const parseJson = <T>(value: string | null, fallback: T): T => {
|
|
if (!value) return fallback
|
|
try {
|
|
return JSON.parse(value) as T
|
|
} catch {
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
function rowToArticle(row: Record<string, unknown>): Article {
|
|
return {
|
|
id: Number(row.id),
|
|
slug: String(row.slug),
|
|
category: row.category as Article['category'],
|
|
titleZh: String(row.title_zh),
|
|
titleEn: String(row.title_en),
|
|
excerptZh: String(row.excerpt_zh),
|
|
excerptEn: String(row.excerpt_en),
|
|
contentZh: parseJson<ArticleBlock[]>(String(row.content_zh ?? ''), []),
|
|
contentEn: parseJson<ArticleBlock[]>(String(row.content_en ?? ''), []),
|
|
image: row.image ? String(row.image) : null,
|
|
date: row.date ? String(row.date) : null,
|
|
readTime: Number(row.read_time) || 5,
|
|
author: String(row.author),
|
|
published: Number(row.published) || 0,
|
|
createdAt: String(row.created_at),
|
|
updatedAt: String(row.updated_at),
|
|
}
|
|
}
|
|
|
|
function rowToWork(row: Record<string, unknown>): Work {
|
|
return {
|
|
id: Number(row.id),
|
|
slug: String(row.slug),
|
|
category: row.category as Work['category'],
|
|
titleZh: String(row.title_zh),
|
|
titleEn: String(row.title_en),
|
|
descriptionZh: String(row.description_zh),
|
|
descriptionEn: String(row.description_en),
|
|
overviewZh: String(row.overview_zh),
|
|
overviewEn: String(row.overview_en),
|
|
challengeZh: String(row.challenge_zh),
|
|
challengeEn: String(row.challenge_en),
|
|
solutionZh: String(row.solution_zh),
|
|
solutionEn: String(row.solution_en),
|
|
clientZh: String(row.client_zh),
|
|
clientEn: String(row.client_en),
|
|
durationZh: String(row.duration_zh),
|
|
durationEn: String(row.duration_en),
|
|
year: String(row.year),
|
|
tags: parseJson<string[]>(String(row.tags ?? ''), []),
|
|
image: row.image ? String(row.image) : null,
|
|
gallery: parseJson<string[]>(String(row.gallery ?? ''), []),
|
|
url: row.url ? String(row.url) : null,
|
|
published: Number(row.published) || 0,
|
|
createdAt: String(row.created_at),
|
|
updatedAt: String(row.updated_at),
|
|
}
|
|
}
|
|
|
|
const ARTICLE_COLS = `id, slug, category, title_zh, title_en, excerpt_zh, excerpt_en, content_zh, content_en, image, date, read_time, author, published, created_at, updated_at`
|
|
const WORK_COLS = `id, slug, category, title_zh, title_en, description_zh, description_en, overview_zh, overview_en, challenge_zh, challenge_en, solution_zh, solution_en, client_zh, client_en, duration_zh, duration_en, year, tags, image, gallery, url, published, created_at, updated_at`
|
|
|
|
export function listArticles(options: { publishedOnly?: boolean } = {}): Article[] {
|
|
const { publishedOnly = true } = options
|
|
const sql = `SELECT ${ARTICLE_COLS} FROM articles
|
|
${publishedOnly ? 'WHERE published = 1' : ''}
|
|
ORDER BY date DESC, id DESC`
|
|
const rows = getDb().prepare(sql).all() as Array<Record<string, unknown>>
|
|
return rows.map(rowToArticle)
|
|
}
|
|
|
|
export function getArticleBySlug(slug: string, options: { publishedOnly?: boolean } = {}): Article | null {
|
|
const { publishedOnly = true } = options
|
|
const sql = `SELECT ${ARTICLE_COLS} FROM articles WHERE slug = ? ${publishedOnly ? 'AND published = 1' : ''}`
|
|
const row = getDb().prepare(sql).get(slug) as Record<string, unknown> | undefined
|
|
return row ? rowToArticle(row) : null
|
|
}
|
|
|
|
export function listWorks(options: { publishedOnly?: boolean } = {}): Work[] {
|
|
const { publishedOnly = true } = options
|
|
const sql = `SELECT ${WORK_COLS} FROM works
|
|
${publishedOnly ? 'WHERE published = 1' : ''}
|
|
ORDER BY id ASC`
|
|
const rows = getDb().prepare(sql).all() as Array<Record<string, unknown>>
|
|
return rows.map(rowToWork)
|
|
}
|
|
|
|
export function getWorkBySlug(slug: string, options: { publishedOnly?: boolean } = {}): Work | null {
|
|
const { publishedOnly = true } = options
|
|
const sql = `SELECT ${WORK_COLS} FROM works WHERE slug = ? ${publishedOnly ? 'AND published = 1' : ''}`
|
|
const row = getDb().prepare(sql).get(slug) as Record<string, unknown> | undefined
|
|
return row ? rowToWork(row) : null
|
|
}
|
|
|
|
/** 后台统计 */
|
|
export function getContentStats(): {
|
|
articles: number
|
|
works: number
|
|
publishedArticles: number
|
|
publishedWorks: number
|
|
} {
|
|
const db = getDb()
|
|
const one = (sql: string) => (db.prepare(sql).get() as { c: number }).c
|
|
return {
|
|
articles: one('SELECT COUNT(*) AS c FROM articles'),
|
|
works: one('SELECT COUNT(*) AS c FROM works'),
|
|
publishedArticles: one('SELECT COUNT(*) AS c FROM articles WHERE published = 1'),
|
|
publishedWorks: one('SELECT COUNT(*) AS c FROM works WHERE published = 1'),
|
|
}
|
|
}
|