Files
personal/lib/siteConfig.ts
T
virtheart 520fbb1480 fix: Header 跨页锚点导航 + 后台移除固定 URL 编辑项
- Header 作品/联系:首页内直接平滑滚动;在 /services、/work/* 等
  非首页时写入 hq:pendingScrollAnchor 后经路由过渡回首页,
  SmoothScroller 消费并滚动到位(修复其它页面点击无反应)
- 服务按钮在 /services 页自身不再重复导航
- PENDING_SCROLL_ANCHOR_SESSION_KEY 收敛到 lib/scrollEnv 单一来源,
  work 页与 SmoothScroller 共用,消除三处魔法字符串
- 后台「关于」组移除 about_p2_link_href(站内结构性锚点 #selected-work
  不可配,只保留链接文字可编辑);seed/settings/类型同步清理
2026-09-02 20:10:05 +08:00

376 lines
11 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;
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;
/** ICP 备案号(部署国内时填,页脚展示;空则不显示) */
icp_beian: string;
seo_keywords: string;
/** 搜索引擎站长验证 meta content(百度/搜狗/360/Bing 通用) */
search_verification: 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",
"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",
"icp_beian",
"seo_keywords",
"search_verification",
] as const satisfies readonly (keyof SiteConfig)[];
/** 配置分组(后台表单渲染顺序)。 */
export const CONFIG_GROUPS: { title: string; keys: (keyof SiteConfig)[] }[] = [
{
title: "基础",
keys: ["site_title", "site_description", "seo_keywords", "search_verification", "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",
],
},
{
title: "页脚",
keys: ["footer_headline", "contact_email", "icp_beian"],
},
{
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);
}