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:
2026-08-06 16:15:14 +08:00
parent 5c3f3bca63
commit d67849a311
67 changed files with 4909 additions and 754 deletions
+120
View File
@@ -1,7 +1,15 @@
import Database from 'better-sqlite3'
import { mkdirSync } from 'node:fs'
import { dirname } from 'node:path'
import { scryptSync, randomBytes } from 'node:crypto'
import { getConfig } from './config'
import { articlesSeed, worksSeed } from './seed'
/** 文章正文内容块 */
export interface ArticleBlock {
type: 'paragraph' | 'heading' | 'quote' | 'image'
text: string
}
/**
* SQLite 数据层单例。
@@ -30,6 +38,7 @@ export function getDb(): Database.Database {
_db.pragma('journal_mode = WAL')
_db.pragma('foreign_keys = ON')
migrate(_db)
seedIfEmpty(_db)
return _db
}
@@ -89,9 +98,120 @@ function migrate(db: Database.Database): void {
);
CREATE INDEX IF NOT EXISTS idx_comments_news ON comments(news_slug);
-- 内容管理系统:动态(articles)与作品(works)
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT NOT NULL UNIQUE,
category TEXT NOT NULL,
title_zh TEXT NOT NULL,
title_en TEXT NOT NULL,
excerpt_zh TEXT NOT NULL,
excerpt_en TEXT NOT NULL,
content_zh TEXT NOT NULL DEFAULT '[]',
content_en TEXT NOT NULL DEFAULT '[]',
image TEXT,
date TEXT,
read_time INTEGER NOT NULL DEFAULT 5,
author TEXT NOT NULL DEFAULT 'ArcticNewOne',
published INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_articles_category ON articles(category);
CREATE INDEX IF NOT EXISTS idx_articles_published ON articles(published);
CREATE TABLE IF NOT EXISTS works (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT NOT NULL UNIQUE,
category TEXT NOT NULL,
title_zh TEXT NOT NULL,
title_en TEXT NOT NULL,
description_zh TEXT NOT NULL,
description_en TEXT NOT NULL,
overview_zh TEXT NOT NULL DEFAULT '',
overview_en TEXT NOT NULL DEFAULT '',
challenge_zh TEXT NOT NULL DEFAULT '',
challenge_en TEXT NOT NULL DEFAULT '',
solution_zh TEXT NOT NULL DEFAULT '',
solution_en TEXT NOT NULL DEFAULT '',
client_zh TEXT NOT NULL DEFAULT '',
client_en TEXT NOT NULL DEFAULT '',
duration_zh TEXT NOT NULL DEFAULT '',
duration_en TEXT NOT NULL DEFAULT '',
year TEXT NOT NULL DEFAULT '',
tags TEXT NOT NULL DEFAULT '[]',
image TEXT,
gallery TEXT NOT NULL DEFAULT '[]',
url TEXT,
published INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_works_category ON works(category);
CREATE INDEX IF NOT EXISTS idx_works_published ON works(published);
`)
}
/**
* 初始化种子数据:内容表为空时灌入 articles/works 种子;
* users 为空且配置了 adminInitialPassword 时创建预置管理员。
*/
function seedIfEmpty(db: Database.Database): void {
const { adminInitialPassword, adminEmails } = getConfig()
const articleCount = (db.prepare('SELECT COUNT(*) AS c FROM articles').get() as { c: number }).c
if (articleCount === 0) {
const insert = db.prepare(
`INSERT INTO articles
(slug, category, title_zh, title_en, excerpt_zh, excerpt_en, content_zh, content_en, image, date, read_time, author, published)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
)
const tx = db.transaction(() => {
for (const a of articlesSeed) {
insert.run(a.slug, a.category, a.titleZh, a.titleEn, a.excerptZh, a.excerptEn,
JSON.stringify(a.contentZh), JSON.stringify(a.contentEn), a.image, a.date, a.readTime, a.author, a.published)
}
})
tx()
}
const workCount = (db.prepare('SELECT COUNT(*) AS c FROM works').get() as { c: number }).c
if (workCount === 0) {
const insert = db.prepare(
`INSERT INTO works
(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)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
)
const tx = db.transaction(() => {
for (const w of worksSeed) {
insert.run(w.slug, w.category, w.titleZh, w.titleEn, w.descriptionZh, w.descriptionEn,
w.overviewZh, w.overviewEn, w.challengeZh, w.challengeEn, w.solutionZh, w.solutionEn,
w.clientZh, w.clientEn, w.durationZh, w.durationEn, w.year,
JSON.stringify(w.tags), w.image, JSON.stringify(w.gallery), w.url, w.published)
}
})
tx()
}
// 预置管理员(仅当首次初始化且配置了初始密码)
const userCount = (db.prepare('SELECT COUNT(*) AS c FROM users').get() as { c: number }).c
if (userCount === 0 && adminInitialPassword && adminEmails.length > 0) {
const email = adminEmails[0]
const username = 'admin'
// 与 auth.ts 相同的 salt:hash 存储格式(scrypt)
const salt = randomBytes(16).toString('hex')
const hash = scryptSync(adminInitialPassword, salt, 64).toString('hex')
db.prepare('INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, ?)')
.run(username, email, `${salt}:${hash}`, 'admin')
console.log('[seed] 已创建预置管理员账号: admin(邮箱 ' + email + ')')
}
}
/** 供服务端任意位置安全关闭数据库(测试/热更新用) */
export function closeDb(): void {
if (_db) {