d666cedfc6
后端(node:sqlite 零原生依赖,data/site.db,WAL + busy_timeout 抗并发): - works / service_items / site_config / inquiries / admin_users / admin_sessions / captcha_challenges - API:admin 登录登出/密码、作品与服务 CRUD、图片上传、留言箱、 验证码 challenge/verify、咨询提交(一次性滑块令牌防重放) 后台 /virtheart(仅手动访问,未登录一律 307 → /virtheart/login): - 服务端布局鉴权 + DB 会话(httpOnly cookie 7 天)+ CSRF 头校验 - 概览 / 作品(上传、网格预设、发布开关)/ 服务 / 留言箱 / 配置(全站文案) - 设计与主站同语言:暗底点阵、mono、lime 选中,非传统后台模板 公开站: - 路由组 (site) 隔离站点壳层;首页/服务页文案全部 DB 驱动即时生效 - /services 服务菜单 + 留言表单 + 全屏居中滑块拼图验证码(对齐缺口) - 提交入 inquiries 并 SMTP(飞书 notify@)邮件通知 chenchun@virtheart.com - Header 新增「服务」菜单;旧作品清理,空状态自动接管 - 根布局仅承载字体/全局样式,后台无站点壳层 验证:登录/CSRF/发布隔离/滑块容错/令牌单次消费/SMTP 真发全部通过; lint 与基线一致(119 errors 均为存量)。
371 lines
10 KiB
TypeScript
371 lines
10 KiB
TypeScript
/**
|
|
* 站点配置访问层 — site_config(key→JSON value) 的类型化读写。
|
|
* getSiteConfig() 每次直接读库(SQLite 同步读取开销可忽略),后台改完
|
|
* 立即生效,无需缓存失效逻辑。
|
|
*/
|
|
import { db } from "@/lib/db";
|
|
|
|
export type SiteConfig = {
|
|
site_title: string;
|
|
site_description: string;
|
|
brand_name: string;
|
|
hero_line1: string;
|
|
hero_line2: string;
|
|
hero_tagline: string;
|
|
hero_big_1: string;
|
|
hero_big_2: string;
|
|
hero_big_3: string;
|
|
about_p1: string;
|
|
about_p2: string;
|
|
about_p2_link_label: string;
|
|
about_p2_link_href: string;
|
|
intro_prefix: string;
|
|
intro_suffix: string;
|
|
intro_brand: string;
|
|
footer_headline: string;
|
|
contact_email: string;
|
|
work_empty_headline: string;
|
|
work_empty_note: string;
|
|
work_empty_cta_prefix: string;
|
|
work_empty_cta_suffix: string;
|
|
work_empty_cta_action: string;
|
|
copyright_name: string;
|
|
services_hero: string;
|
|
services_submit_note: string;
|
|
};
|
|
|
|
export const CONFIG_KEYS = [
|
|
"site_title",
|
|
"site_description",
|
|
"brand_name",
|
|
"hero_line1",
|
|
"hero_line2",
|
|
"hero_tagline",
|
|
"hero_big_1",
|
|
"hero_big_2",
|
|
"hero_big_3",
|
|
"about_p1",
|
|
"about_p2",
|
|
"about_p2_link_label",
|
|
"about_p2_link_href",
|
|
"intro_prefix",
|
|
"intro_suffix",
|
|
"intro_brand",
|
|
"footer_headline",
|
|
"contact_email",
|
|
"work_empty_headline",
|
|
"work_empty_note",
|
|
"work_empty_cta_prefix",
|
|
"work_empty_cta_suffix",
|
|
"work_empty_cta_action",
|
|
"copyright_name",
|
|
"services_hero",
|
|
"services_submit_note",
|
|
] as const satisfies readonly (keyof SiteConfig)[];
|
|
|
|
/** 配置分组(后台表单渲染顺序)。 */
|
|
export const CONFIG_GROUPS: { title: string; keys: (keyof SiteConfig)[] }[] = [
|
|
{
|
|
title: "基础",
|
|
keys: ["site_title", "site_description", "brand_name", "copyright_name"],
|
|
},
|
|
{
|
|
title: "首屏",
|
|
keys: [
|
|
"hero_line1",
|
|
"hero_line2",
|
|
"hero_tagline",
|
|
"hero_big_1",
|
|
"hero_big_2",
|
|
"hero_big_3",
|
|
"intro_prefix",
|
|
"intro_suffix",
|
|
"intro_brand",
|
|
],
|
|
},
|
|
{
|
|
title: "关于",
|
|
keys: [
|
|
"about_p1",
|
|
"about_p2",
|
|
"about_p2_link_label",
|
|
"about_p2_link_href",
|
|
],
|
|
},
|
|
{
|
|
title: "页脚",
|
|
keys: ["footer_headline", "contact_email"],
|
|
},
|
|
{
|
|
title: "作品区空状态",
|
|
keys: [
|
|
"work_empty_headline",
|
|
"work_empty_note",
|
|
"work_empty_cta_prefix",
|
|
"work_empty_cta_suffix",
|
|
"work_empty_cta_action",
|
|
],
|
|
},
|
|
{
|
|
title: "服务页",
|
|
keys: ["services_hero", "services_submit_note"],
|
|
},
|
|
];
|
|
|
|
export function getSiteConfig(): SiteConfig {
|
|
const rows = db.prepare("SELECT key, value FROM site_config").all() as {
|
|
key: string;
|
|
value: string;
|
|
}[];
|
|
const map = Object.fromEntries(rows.map((r) => [r.key, r.value]));
|
|
const out = {} as SiteConfig;
|
|
for (const key of CONFIG_KEYS) {
|
|
out[key] = map[key] ?? "";
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function setSiteConfig(patch: Partial<Record<keyof SiteConfig, string>>) {
|
|
const stmt = db.prepare(
|
|
"INSERT INTO site_config (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value"
|
|
);
|
|
for (const [key, value] of Object.entries(patch)) {
|
|
if (CONFIG_KEYS.includes(key as keyof SiteConfig) && typeof value === "string") {
|
|
stmt.run(key, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 作品
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export type WorkRow = {
|
|
id: number;
|
|
slug: string;
|
|
name: string;
|
|
year: string;
|
|
type: "post" | "tools" | "event";
|
|
href: string;
|
|
image_url: string;
|
|
hover_image_url: string;
|
|
grid_class: string;
|
|
coding_project: number;
|
|
passcode_protected: number;
|
|
sort_order: number;
|
|
published: number;
|
|
article_date: string;
|
|
article_year: string;
|
|
article_iframe_src: string;
|
|
article_markdown: string;
|
|
};
|
|
|
|
const WORK_COLUMNS =
|
|
"id, slug, name, year, type, href, image_url, hover_image_url, grid_class, coding_project, passcode_protected, sort_order, published, article_date, article_year, article_iframe_src, article_markdown";
|
|
|
|
export function listWorks(onlyPublished = false): WorkRow[] {
|
|
const where = onlyPublished ? "WHERE published = 1" : "";
|
|
return db
|
|
.prepare(
|
|
`SELECT ${WORK_COLUMNS} FROM works ${where} ORDER BY sort_order ASC, id ASC`
|
|
)
|
|
.all() as unknown as WorkRow[];
|
|
}
|
|
|
|
export function getWork(id: number): WorkRow | undefined {
|
|
return db
|
|
.prepare(`SELECT ${WORK_COLUMNS} FROM works WHERE id = ?`)
|
|
.get(id) as WorkRow | undefined;
|
|
}
|
|
|
|
/** 站内详情路由可用的 slug(原 /work/[slug] 语义)。 */
|
|
export function upsertWork(
|
|
data: Partial<WorkRow> & { name: string }
|
|
): WorkRow {
|
|
const slug =
|
|
data.slug ||
|
|
data.name
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, "-")
|
|
.replace(/^-+|-+$/g, "") ||
|
|
`work-${Date.now()}`;
|
|
const existing = db.prepare("SELECT id FROM works WHERE slug = ?").get(slug) as
|
|
| { id: number }
|
|
| undefined;
|
|
|
|
const cols = {
|
|
name: data.name,
|
|
year: data.year ?? "",
|
|
type: (data.type ?? "post") as string,
|
|
href: data.href ?? "",
|
|
image_url: data.image_url ?? "",
|
|
hover_image_url: data.hover_image_url ?? "",
|
|
grid_class:
|
|
data.grid_class ?? "col-span-12 lg:col-span-6 xl:col-span-4",
|
|
coding_project: data.coding_project ?? 0,
|
|
passcode_protected: data.passcode_protected ?? 0,
|
|
sort_order: data.sort_order ?? 0,
|
|
published: data.published ?? 1,
|
|
article_date: data.article_date ?? "",
|
|
article_year: data.article_year ?? "",
|
|
article_iframe_src: data.article_iframe_src ?? "",
|
|
article_markdown: data.article_markdown ?? "",
|
|
};
|
|
|
|
if (existing) {
|
|
db.prepare(
|
|
`UPDATE works SET name=@name, year=@year, type=@type, href=@href,
|
|
image_url=@image_url, hover_image_url=@hover_image_url, grid_class=@grid_class,
|
|
coding_project=@coding_project, passcode_protected=@passcode_protected,
|
|
sort_order=@sort_order, published=@published, article_date=@article_date,
|
|
article_year=@article_year, article_iframe_src=@article_iframe_src,
|
|
article_markdown=@article_markdown, updated_at=datetime('now')
|
|
WHERE slug=@slug`
|
|
).run({ ...cols, slug });
|
|
} else {
|
|
db.prepare(
|
|
`INSERT INTO works (slug, name, year, type, href, image_url, hover_image_url,
|
|
grid_class, coding_project, passcode_protected, sort_order, published,
|
|
article_date, article_year, article_iframe_src, article_markdown)
|
|
VALUES (@slug, @name, @year, @type, @href, @image_url, @hover_image_url,
|
|
@grid_class, @coding_project, @passcode_protected, @sort_order, @published,
|
|
@article_date, @article_year, @article_iframe_src, @article_markdown)`
|
|
).run({ ...cols, slug });
|
|
}
|
|
const row = db
|
|
.prepare(`SELECT ${WORK_COLUMNS} FROM works WHERE slug = ?`)
|
|
.get(slug) as WorkRow;
|
|
return row;
|
|
}
|
|
|
|
export function deleteWork(id: number) {
|
|
db.prepare("DELETE FROM works WHERE id = ?").run(id);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 服务
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export type ServiceRow = {
|
|
id: number;
|
|
slug: string;
|
|
title: string;
|
|
description: string;
|
|
price_note: string;
|
|
icon: string;
|
|
sort_order: number;
|
|
enabled: number;
|
|
};
|
|
|
|
export function listServices(onlyEnabled = false): ServiceRow[] {
|
|
const where = onlyEnabled ? "WHERE enabled = 1" : "";
|
|
return db
|
|
.prepare(
|
|
`SELECT id, slug, title, description, price_note, icon, sort_order, enabled
|
|
FROM service_items ${where} ORDER BY sort_order ASC, id ASC`
|
|
)
|
|
.all() as unknown as ServiceRow[];
|
|
}
|
|
|
|
export function upsertService(data: {
|
|
id?: number;
|
|
slug?: string;
|
|
title: string;
|
|
description?: string;
|
|
price_note?: string;
|
|
sort_order?: number;
|
|
enabled?: number;
|
|
}): ServiceRow {
|
|
if (data.id) {
|
|
db.prepare(
|
|
`UPDATE service_items SET title=@title, description=@description,
|
|
price_note=@price_note, sort_order=@sort_order, enabled=@enabled
|
|
WHERE id=@id`
|
|
).run({
|
|
title: data.title,
|
|
description: data.description ?? "",
|
|
price_note: data.price_note ?? "",
|
|
sort_order: data.sort_order ?? 0,
|
|
enabled: data.enabled ?? 1,
|
|
id: data.id,
|
|
});
|
|
return db
|
|
.prepare("SELECT * FROM service_items WHERE id = ?")
|
|
.get(data.id) as ServiceRow;
|
|
}
|
|
const slug =
|
|
data.slug ||
|
|
`svc-${Date.now().toString(36)}`;
|
|
db.prepare(
|
|
`INSERT INTO service_items (slug, title, description, price_note, sort_order, enabled)
|
|
VALUES (?, ?, ?, ?, ?, ?)`
|
|
).run(
|
|
slug,
|
|
data.title,
|
|
data.description ?? "",
|
|
data.price_note ?? "",
|
|
data.sort_order ?? 0,
|
|
data.enabled ?? 1
|
|
);
|
|
return db
|
|
.prepare("SELECT * FROM service_items WHERE slug = ?")
|
|
.get(slug) as ServiceRow;
|
|
}
|
|
|
|
export function deleteService(id: number) {
|
|
db.prepare("DELETE FROM service_items WHERE id = ?").run(id);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 留言
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export type InquiryRow = {
|
|
id: number;
|
|
name: string;
|
|
contact: string;
|
|
service_slug: string;
|
|
service_title: string;
|
|
message: string;
|
|
status: "new" | "read" | "replied" | "archived";
|
|
email_sent: number;
|
|
email_error: string | null;
|
|
created_at: string;
|
|
};
|
|
|
|
export function createInquiry(data: {
|
|
name: string;
|
|
contact: string;
|
|
service_slug: string;
|
|
service_title: string;
|
|
message: string;
|
|
}): InquiryRow {
|
|
db.prepare(
|
|
`INSERT INTO inquiries (name, contact, service_slug, service_title, message)
|
|
VALUES (?, ?, ?, ?, ?)`
|
|
).run(data.name, data.contact, data.service_slug, data.service_title, data.message);
|
|
return db
|
|
.prepare("SELECT * FROM inquiries ORDER BY id DESC LIMIT 1")
|
|
.get() as InquiryRow;
|
|
}
|
|
|
|
export function listInquiries(limit = 100): InquiryRow[] {
|
|
return db
|
|
.prepare("SELECT * FROM inquiries ORDER BY id DESC LIMIT ?")
|
|
.all(limit) as unknown as InquiryRow[];
|
|
}
|
|
|
|
export function updateInquiryStatus(id: number, status: InquiryRow["status"]) {
|
|
db.prepare("UPDATE inquiries SET status = ? WHERE id = ?").run(status, id);
|
|
}
|
|
|
|
export function markInquiryEmail(id: number, sent: boolean, error?: string) {
|
|
db.prepare(
|
|
"UPDATE inquiries SET email_sent = ?, email_error = ? WHERE id = ?"
|
|
).run(sent ? 1 : 0, error ?? null, id);
|
|
}
|
|
|
|
export function deleteInquiry(id: number) {
|
|
db.prepare("DELETE FROM inquiries WHERE id = ?").run(id);
|
|
}
|