feat: Next.js 后端 + SQLite + 暗门后台 /virtheart + 服务咨询页
后端(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 均为存量)。
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import SmoothScroller from "@/components/dom/SmoothScroller";
|
||||
import Header from "@/components/dom/Header";
|
||||
import BackgroundGrid from "@/components/dom/BackgroundGrid";
|
||||
import CustomScrollbar from "@/components/dom/CustomScrollbar";
|
||||
import LoadingOverlay from "@/components/dom/LoadingOverlay";
|
||||
import { HeroScene } from "@/components/canvas/HeroScene";
|
||||
import { PasscodeAccessProvider } from "@/components/dom/PasscodeAccess";
|
||||
import { FullscreenTransitionProvider } from "@/lib/routeTransition";
|
||||
import RouteTransitionOverlay from "@/components/dom/RouteTransitionOverlay";
|
||||
import { getPasscodeAccessServer } from "@/lib/passcodeServer";
|
||||
import { getSiteConfig } from "@/lib/siteConfig";
|
||||
import type { Metadata } from "next";
|
||||
|
||||
/**
|
||||
* (site) 布局 — 公开站点的完整壳层:WebGL 画布、网格、平滑滚动、
|
||||
* Header、路由过渡。/virtheart 后台不套用此壳层(纯净布局)。
|
||||
* 站点文案从 site_config 读出后以 props 下发(Header 品牌/版权、
|
||||
* 自我介绍槽位等),后台改完即时生效。
|
||||
*/
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const config = getSiteConfig();
|
||||
return {
|
||||
title: config.site_title,
|
||||
description: config.site_description,
|
||||
};
|
||||
}
|
||||
|
||||
export default async function SiteLayout({
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const [initialAccess, config] = await Promise.all([
|
||||
getPasscodeAccessServer(),
|
||||
Promise.resolve(getSiteConfig()),
|
||||
]);
|
||||
return (
|
||||
<PasscodeAccessProvider initialAccess={initialAccess}>
|
||||
<FullscreenTransitionProvider>
|
||||
{/* Original stacking (1:1): the WebGL canvas host is a plain `fixed`
|
||||
z-auto element first in the body; the grid overlay (z-20) comes
|
||||
AFTER it in DOM order so it paints above the canvas, exactly like
|
||||
the original's main > canvas + z-20 grid sibling. */}
|
||||
<HeroScene />
|
||||
<BackgroundGrid />
|
||||
<SmoothScroller />
|
||||
<Header brandName={config.brand_name} copyrightName={config.copyright_name} />
|
||||
{children}
|
||||
<CustomScrollbar />
|
||||
<RouteTransitionOverlay />
|
||||
<LoadingOverlay />
|
||||
</FullscreenTransitionProvider>
|
||||
</PasscodeAccessProvider>
|
||||
);
|
||||
}
|
||||
@@ -4,14 +4,28 @@ import FooterSection from "@/components/dom/FooterSection";
|
||||
import WorkSection from "@/components/dom/WorkSection";
|
||||
import HyperSpaceSection from "@/components/dom/HyperSpaceSection";
|
||||
import PasscodeRevealText from "@/components/dom/PasscodeRevealText";
|
||||
import { getSiteConfig, listWorks } from "@/lib/siteConfig";
|
||||
import type { WorkItem } from "@/lib/workItems";
|
||||
|
||||
/**
|
||||
* Page content matching the original DOM (haoqi_body.html):
|
||||
* - banner section (h-dvh) with meta row + big title, all scramble-revealed
|
||||
* - signature/about section (kept simple)
|
||||
* 首页 — 结构复刻原站(haoqi_body.html),文案全部来自 site_config
|
||||
*(/virtheart 后台「配置」可改,保存即生效);作品来自 works 表。
|
||||
*/
|
||||
|
||||
export default function Home() {
|
||||
export default async function Home() {
|
||||
const config = getSiteConfig();
|
||||
const items: WorkItem[] = listWorks(true)
|
||||
.filter((w) => w.name && (w.image_url || w.href))
|
||||
.map((w) => ({
|
||||
name: w.name,
|
||||
imageUrl: w.image_url,
|
||||
hoverImageUrl: w.hover_image_url,
|
||||
href: w.href || `/work/${w.slug}`,
|
||||
year: w.year,
|
||||
type: w.type,
|
||||
gridClass: w.grid_class || "col-span-12 lg:col-span-6 xl:col-span-4",
|
||||
codingProject: w.coding_project === 1,
|
||||
passcodeProtected: w.passcode_protected === 1,
|
||||
}));
|
||||
return (
|
||||
// NOTE: the original scrolls INSIDE <main> (its Lenis wraps the main
|
||||
// container), which is what lets its position:sticky children pin. Our
|
||||
@@ -26,25 +40,30 @@ export default function Home() {
|
||||
>
|
||||
<div className="flex flex-col order-2 lg:order-1 lg:grid lg:grid-cols-12 col-span-12 font-mono text-base">
|
||||
<span className="hidden lg:block lg:col-span-3 xl:col-span-2 lg:col-start-1 xl:col-start-1 p-2 font-sans font-medium text-[4svw] sm:text-2xl lg:text-3xl leading-tight">
|
||||
<ScrambleReveal text="设计与" startDelayMs={300} letterDelayMs={10} />
|
||||
<ScrambleReveal text={config.hero_line1} startDelayMs={300} letterDelayMs={10} />
|
||||
<br />
|
||||
<ScrambleReveal text="工程" startDelayMs={300} letterDelayMs={10} />
|
||||
<ScrambleReveal text={config.hero_line2} startDelayMs={300} letterDelayMs={10} />
|
||||
</span>
|
||||
<ScrambleReveal
|
||||
className="hidden lg:block lg:col-span-3 xl:col-span-2 lg:col-start-4 xl:col-start-5 p-2 text-balance"
|
||||
text="以系统思维思考,用心做出设计。"
|
||||
text={config.hero_tagline}
|
||||
startDelayMs={300}
|
||||
letterDelayMs={10}
|
||||
/>
|
||||
<PasscodeRevealText className="col-span-12 lg:col-span-6 xl:col-span-4 lg:col-start-7 xl:col-start-9 mt-auto lg:mt-0 p-2" />
|
||||
<PasscodeRevealText
|
||||
className="col-span-12 lg:col-span-6 xl:col-span-4 lg:col-start-7 xl:col-start-9 mt-auto lg:mt-0 p-2"
|
||||
prefix={config.intro_prefix}
|
||||
suffix={config.intro_suffix}
|
||||
brandName={config.brand_name}
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
className="flex flex-col self-end order-1 lg:order-2 col-span-12 px-2 font-bold text-[7.2svw] lg:text-[6svw] 2xl:text-[5svw] xl:text-[5.6svw] uppercase leading-none"
|
||||
style={{ fontVariationSettings: '"wdth" 120' }}
|
||||
>
|
||||
<ScrambleReveal text="我把匠心" startDelayMs={300} />
|
||||
<ScrambleReveal text="与品味" startDelayMs={500} />
|
||||
<ScrambleReveal text="带进数字作品" startDelayMs={700} />
|
||||
<ScrambleReveal text={config.hero_big_1} startDelayMs={300} />
|
||||
<ScrambleReveal text={config.hero_big_2} startDelayMs={500} />
|
||||
<ScrambleReveal text={config.hero_big_3} startDelayMs={700} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -59,10 +78,7 @@ export default function Home() {
|
||||
'var(--font-tiktok), "MiSans", sans-serif',
|
||||
}}
|
||||
>
|
||||
<span>
|
||||
全栈开发者,融合产品设计与工程能力,负责从产品构思、界面设计到前后端、跨端客户端的完整应用交付,现阶段深耕
|
||||
AI 应用开发。
|
||||
</span>
|
||||
<span className="select-text">{config.about_p1}</span>
|
||||
</p>
|
||||
<p
|
||||
className="p-2 w-full text-l2 md:text-[4.2svw] text-xl leading-[1.3] md:leading-none select-text"
|
||||
@@ -71,22 +87,34 @@ export default function Home() {
|
||||
'var(--font-tiktok), "MiSans", sans-serif',
|
||||
}}
|
||||
>
|
||||
<span>
|
||||
我相信工程塑造可靠的产品,设计赋予它们灵魂;
|
||||
<span className="select-text">
|
||||
{config.about_p2}
|
||||
<a
|
||||
href="#selected-work"
|
||||
href={config.about_p2_link_href || "#selected-work"}
|
||||
className="inline text-l1 underline underline-offset-[0.08em] decoration-solid decoration-(--label-3) transition-[text-decoration-color] duration-150 ease-out lg:[@media(hover:hover)]:hover:decoration-(--label-1)"
|
||||
>
|
||||
我的作品
|
||||
{config.about_p2_link_label}
|
||||
</a>
|
||||
里记录了这些探索的痕迹。
|
||||
{config.about_p2.endsWith(";") || config.about_p2.endsWith(",")
|
||||
? "里记录了这些探索的痕迹。"
|
||||
: ""}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Selected Work grid (id="selected-work", WebGL image layers) */}
|
||||
<WorkSection />
|
||||
<WorkSection
|
||||
items={items}
|
||||
emptyText={{
|
||||
headline: config.work_empty_headline,
|
||||
note: config.work_empty_note,
|
||||
ctaPrefix: config.work_empty_cta_prefix,
|
||||
ctaSuffix: config.work_empty_cta_suffix,
|
||||
ctaAction: config.work_empty_cta_action,
|
||||
}}
|
||||
contactEmail={config.contact_email}
|
||||
/>
|
||||
|
||||
{/* Hyper-space scroll section (id="hyper-space"): the WebGL
|
||||
hyperspace arrow (cursor.glb stripes) tracks this section's
|
||||
@@ -94,7 +122,7 @@ export default function Home() {
|
||||
<HyperSpaceSection />
|
||||
|
||||
{/* Contact / Footer Section (#contact, data-section="footer") */}
|
||||
<FooterSection />
|
||||
<FooterSection contactEmail={config.contact_email} />
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
@@ -0,0 +1,299 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* 服务页(client)— 与主站同一设计语言:
|
||||
* 大字 scramble 标题、编号服务菜单(NO.01…)、选择后展开留言表单,
|
||||
* 提交触发全屏居中滑块验证码,通过后 POST /api/inquiry(后端入库 +
|
||||
* 邮件通知 chenchun@virtheart.com)。
|
||||
*/
|
||||
import { useCallback, useMemo, useState } from "react";
|
||||
import ScrambleReveal from "@/components/dom/ScrambleReveal";
|
||||
import SliderCaptcha from "@/components/dom/SliderCaptcha";
|
||||
import { useRouteTransitionController } from "@/lib/routeTransition";
|
||||
import { CONTACT_EMAIL } from "@/lib/socials";
|
||||
|
||||
export type ServiceOption = {
|
||||
slug: string;
|
||||
title: string;
|
||||
description: string;
|
||||
priceNote: string;
|
||||
};
|
||||
|
||||
const inputCls =
|
||||
"w-full border border-line bg-b1 px-3 py-2.5 font-mono text-sm text-l1 outline-none transition-colors focus:border-l1 placeholder:text-l3";
|
||||
const labelCls = "mb-1.5 block font-mono-2 text-xs uppercase tracking-widest text-l2";
|
||||
const dotted =
|
||||
"relative before:content-[''] before:absolute before:inset-0 before:border-2 before:border-dotted before:border-transparent before:pointer-events-none before:transition-colors before:duration-200 lg:hover:before:border-l1";
|
||||
|
||||
export default function ServicesClient({
|
||||
headline,
|
||||
submitNote,
|
||||
services,
|
||||
}: {
|
||||
headline: string;
|
||||
submitNote: string;
|
||||
services: ServiceOption[];
|
||||
}) {
|
||||
const { startNavigation } = useRouteTransitionController();
|
||||
const headlineLines = useMemo(
|
||||
() => (headline || "可提供的服务|选择你需要的服务,留言告诉我").split("|"),
|
||||
[headline]
|
||||
);
|
||||
const [selected, setSelected] = useState<ServiceOption | null>(null);
|
||||
const [name, setName] = useState("");
|
||||
const [contact, setContact] = useState("");
|
||||
const [message, setMessage] = useState("");
|
||||
const [captchaOpen, setCaptchaOpen] = useState(false);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [doneId, setDoneId] = useState<number | null>(null);
|
||||
|
||||
const canSubmit = selected && name.trim() && contact.trim() && message.trim();
|
||||
|
||||
const startSubmit = useCallback(() => {
|
||||
if (!canSubmit || submitting) return;
|
||||
setError(null);
|
||||
setCaptchaOpen(true);
|
||||
}, [canSubmit, submitting]);
|
||||
|
||||
const onCaptchaVerified = useCallback(
|
||||
async (submitToken: string) => {
|
||||
if (!selected) return;
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch("/api/inquiry", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
submitToken,
|
||||
name: name.trim(),
|
||||
contact: contact.trim(),
|
||||
serviceSlug: selected.slug,
|
||||
serviceTitle: selected.title,
|
||||
message: message.trim(),
|
||||
}),
|
||||
});
|
||||
const data = (await res.json()) as { id?: number; error?: string };
|
||||
if (!res.ok) {
|
||||
setError(data.error ?? "提交失败,请重试");
|
||||
setCaptchaOpen(false);
|
||||
return;
|
||||
}
|
||||
setDoneId(data.id ?? 0);
|
||||
setCaptchaOpen(false);
|
||||
} catch {
|
||||
setError("网络错误,请重试");
|
||||
setCaptchaOpen(false);
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
},
|
||||
[selected, name, contact, message]
|
||||
);
|
||||
|
||||
if (doneId !== null) {
|
||||
return (
|
||||
<article className="mx-auto px-6 py-18 lg:py-24 w-full max-w-[880px]">
|
||||
<div className="pt-[14vh] flex flex-col gap-4">
|
||||
<h1
|
||||
className="font-bold text-[7.2svw] lg:text-[4.6svw] leading-none text-l1"
|
||||
style={{ fontVariationSettings: '"wdth" 120' }}
|
||||
>
|
||||
<ScrambleReveal text="已收到" startDelayMs={200} scrambleColors={false} />
|
||||
</h1>
|
||||
<p className="font-mono-2 text-sm lg:text-base text-l2">
|
||||
<ScrambleReveal
|
||||
text={`留言编号 #${doneId} —— 我会尽快通过你留下的联系方式回复。`}
|
||||
startDelayMs={400}
|
||||
letterDelayMs={10}
|
||||
/>
|
||||
</p>
|
||||
<div className="mt-4 flex gap-6 font-mono text-sm">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => startNavigation("/")}
|
||||
className={`${dotted} p-2 uppercase cursor-pointer text-l1`}
|
||||
>
|
||||
<ScrambleReveal text="← 返回首页" startDelayMs={300} scrambleColors={false} />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setDoneId(null);
|
||||
setName("");
|
||||
setContact("");
|
||||
setMessage("");
|
||||
setSelected(null);
|
||||
}}
|
||||
className={`${dotted} p-2 uppercase cursor-pointer text-l2`}
|
||||
>
|
||||
<ScrambleReveal text="再提交一条" startDelayMs={300} scrambleColors={false} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<article className="mx-auto px-6 py-18 lg:py-24 w-full max-w-[880px]">
|
||||
<header className="pt-[10vh]">
|
||||
<h1
|
||||
className="font-bold text-[7.2svw] lg:text-[4.6svw] leading-none text-l1"
|
||||
style={{ fontVariationSettings: '"wdth" 120' }}
|
||||
>
|
||||
{headlineLines.map((line, i) => (
|
||||
<span key={i} className={i > 0 ? "block text-right lg:text-right" : "block"}>
|
||||
<ScrambleReveal
|
||||
text={line}
|
||||
startDelayMs={200 + i * 220}
|
||||
reverse={i > 0}
|
||||
scrambleColors={false}
|
||||
/>
|
||||
</span>
|
||||
))}
|
||||
</h1>
|
||||
<div className="mt-2 border-line border-b" />
|
||||
<p className="pt-3 font-mono-2 text-xs text-l3">
|
||||
<ScrambleReveal
|
||||
text={`服务咨询 · 邮件直达 ${CONTACT_EMAIL}`}
|
||||
startDelayMs={500}
|
||||
letterDelayMs={10}
|
||||
/>
|
||||
</p>
|
||||
</header>
|
||||
|
||||
{/* 服务菜单 */}
|
||||
<section className="mt-12" aria-label="服务列表">
|
||||
{services.length === 0 ? (
|
||||
<p className="font-mono-2 text-sm text-l2 border border-dashed border-line p-8 text-center">
|
||||
服务清单整理中——有需要可直接写邮件给我。
|
||||
</p>
|
||||
) : (
|
||||
<ul className="flex flex-col divide-y divide-line border-y border-line">
|
||||
{services.map((s, i) => {
|
||||
const active = selected?.slug === s.slug;
|
||||
return (
|
||||
<li key={s.slug}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSelected(active ? null : s)}
|
||||
aria-pressed={active}
|
||||
className={`${dotted} w-full text-left cursor-pointer px-2 py-5 flex items-baseline gap-4 transition-colors ${
|
||||
active ? "bg-[rgba(var(--label-d),0.04)]" : ""
|
||||
}`}
|
||||
>
|
||||
<span className="font-mono-2 text-xs text-l3 tabular-nums shrink-0">
|
||||
NO.{String(i + 1).padStart(2, "0")}
|
||||
</span>
|
||||
<span
|
||||
className={`font-semibold text-lg lg:text-xl ${
|
||||
active ? "text-l1" : "text-l2"
|
||||
}`}
|
||||
style={{ fontFamily: 'var(--font-tiktok), "MiSans", sans-serif' }}
|
||||
>
|
||||
{s.title}
|
||||
</span>
|
||||
{s.priceNote && (
|
||||
<span className="ml-auto font-mono-2 text-xs text-l3 shrink-0">
|
||||
{s.priceNote}
|
||||
</span>
|
||||
)}
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={`font-mono text-sm shrink-0 transition-transform ${
|
||||
active ? "rotate-45 text-l1" : "text-l3"
|
||||
}`}
|
||||
>
|
||||
+
|
||||
</span>
|
||||
</button>
|
||||
{active && s.description && (
|
||||
<p className="px-2 pb-5 pl-12 font-mono-2 text-sm leading-relaxed text-l2 whitespace-pre-wrap">
|
||||
{s.description}
|
||||
</p>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
|
||||
{/* 留言表单 */}
|
||||
<section className="mt-12 flex flex-col gap-5" aria-label="咨询表单">
|
||||
<p className="font-mono-2 text-xs text-l3 uppercase tracking-widest">
|
||||
{selected ? `咨询:${selected.title}` : "留言"}
|
||||
</p>
|
||||
<div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
|
||||
<div>
|
||||
<label className={labelCls} htmlFor="svc-name">
|
||||
怎么称呼你 *
|
||||
</label>
|
||||
<input
|
||||
id="svc-name"
|
||||
className={inputCls}
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
maxLength={40}
|
||||
placeholder="姓名 / 昵称"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls} htmlFor="svc-contact">
|
||||
联系方式 *
|
||||
</label>
|
||||
<input
|
||||
id="svc-contact"
|
||||
className={inputCls}
|
||||
value={contact}
|
||||
onChange={(e) => setContact(e.target.value)}
|
||||
maxLength={80}
|
||||
placeholder="邮箱 / 微信 / 手机号"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls} htmlFor="svc-message">
|
||||
具体需求 *
|
||||
</label>
|
||||
<textarea
|
||||
id="svc-message"
|
||||
className={`${inputCls} min-h-32`}
|
||||
value={message}
|
||||
onChange={(e) => setMessage(e.target.value)}
|
||||
maxLength={2000}
|
||||
placeholder={
|
||||
selected
|
||||
? `关于「${selected.title}」,简单描述一下你的场景、周期和预算范围…`
|
||||
: "简单描述一下你的场景、周期和预算范围…"
|
||||
}
|
||||
/>
|
||||
<p className="mt-1.5 font-mono-2 text-xs text-l3">{submitNote}</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="border-l-2 border-selection pl-2 text-sm text-l1" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={startSubmit}
|
||||
disabled={!canSubmit || submitting}
|
||||
className="w-full border border-l1 bg-l1 py-3.5 font-mono text-sm uppercase tracking-widest text-b1 transition-opacity hover:opacity-85 disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer"
|
||||
>
|
||||
{submitting ? "提交中…" : "提交(需完成滑块验证)"}
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<SliderCaptcha
|
||||
open={captchaOpen}
|
||||
onClose={() => !submitting && setCaptchaOpen(false)}
|
||||
onVerified={onCaptchaVerified}
|
||||
/>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { Metadata } from "next";
|
||||
import ServicesClient from "./ServicesClient";
|
||||
import { getSiteConfig, listServices } from "@/lib/siteConfig";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "服务 · 北极新一",
|
||||
description: "可提供的服务 — 选择你需要的服务,留言告诉我",
|
||||
};
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/** /services — 服务列表(server 读 DB)+ 咨询表单(滑块验证码)。 */
|
||||
export default function ServicesPage() {
|
||||
const config = getSiteConfig();
|
||||
const services = listServices(true).map((s) => ({
|
||||
slug: s.slug,
|
||||
title: s.title,
|
||||
description: s.description,
|
||||
priceNote: s.price_note,
|
||||
}));
|
||||
return (
|
||||
<ServicesClient
|
||||
headline={config.services_hero}
|
||||
submitNote={config.services_submit_note}
|
||||
services={services}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/** /api/admin/config — 站点配置读写。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSession, verifyCsrf } from "@/lib/auth";
|
||||
import { CONFIG_KEYS, getSiteConfig, setSiteConfig, type SiteConfig } from "@/lib/siteConfig";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
if (!(await getSession())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
return NextResponse.json({ config: getSiteConfig() });
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
let body: Partial<SiteConfig>;
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const patch: Partial<Record<keyof SiteConfig, string>> = {};
|
||||
for (const key of CONFIG_KEYS) {
|
||||
const v = (body as Record<string, unknown>)[key];
|
||||
if (typeof v === "string") patch[key] = v;
|
||||
}
|
||||
setSiteConfig(patch);
|
||||
return NextResponse.json({ ok: true, config: getSiteConfig() });
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/** /api/admin/inquiries — 留言箱:列表 / 状态更新 / 删除。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSession, verifyCsrf } from "@/lib/auth";
|
||||
import {
|
||||
deleteInquiry,
|
||||
listInquiries,
|
||||
updateInquiryStatus,
|
||||
type InquiryRow,
|
||||
} from "@/lib/siteConfig";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
if (!(await getSession())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
return NextResponse.json({ inquiries: listInquiries(200) });
|
||||
}
|
||||
|
||||
export async function PATCH(req: Request) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
let body: { id?: number; status?: InquiryRow["status"] };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const valid: InquiryRow["status"][] = ["new", "read", "replied", "archived"];
|
||||
if (
|
||||
!Number.isInteger(body.id) ||
|
||||
!body.status ||
|
||||
!valid.includes(body.status)
|
||||
) {
|
||||
return NextResponse.json({ error: "参数错误" }, { status: 400 });
|
||||
}
|
||||
updateInquiryStatus(body.id as number, body.status);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
export async function DELETE(req: Request) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
const { searchParams } = new URL(req.url);
|
||||
const id = Number(searchParams.get("id"));
|
||||
if (!Number.isInteger(id)) {
|
||||
return NextResponse.json({ error: "无效 ID" }, { status: 400 });
|
||||
}
|
||||
deleteInquiry(id);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* /api/admin/login — 后台登录。
|
||||
* POST { username, password } → 设置 httpOnly 会话 cookie。
|
||||
*/
|
||||
import { NextResponse } from "next/server";
|
||||
import { createSession, SESSION_COOKIE } from "@/lib/auth";
|
||||
import { verifyAdminPassword } from "@/lib/auth";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
let body: { username?: string; password?: string };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const username = String(body.username ?? "").trim();
|
||||
const password = String(body.password ?? "");
|
||||
if (!username || !password) {
|
||||
return NextResponse.json({ error: "请输入用户名和密码" }, { status: 400 });
|
||||
}
|
||||
|
||||
const userId = verifyAdminPassword(username, password);
|
||||
if (userId === null) {
|
||||
// 统一错误信息,不区分用户名/密码错误(防枚举)
|
||||
return NextResponse.json({ error: "用户名或密码不正确" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { token, csrf } = createSession(userId);
|
||||
const res = NextResponse.json({ ok: true, csrf });
|
||||
res.cookies.set(SESSION_COOKIE, token, {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
path: "/",
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/** /api/admin/logout — 清除会话。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { destroySession, getSession, SESSION_COOKIE } from "@/lib/auth";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST() {
|
||||
const session = await getSession();
|
||||
if (session) destroySession(session.token);
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set(SESSION_COOKIE, "", { path: "/", maxAge: 0 });
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/** /api/admin/password — 修改后台密码。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { changeAdminPassword, getSession, verifyCsrf } from "@/lib/auth";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
const session = await getSession();
|
||||
if (!session || !(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
let body: { password?: string };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const password = String(body.password ?? "");
|
||||
if (password.length < 8) {
|
||||
return NextResponse.json({ error: "密码至少 8 位" }, { status: 400 });
|
||||
}
|
||||
await changeAdminPassword(session.user_id, password);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/** /api/admin/services/[id] — 删除服务。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { verifyCsrf } from "@/lib/auth";
|
||||
import { deleteService } from "@/lib/siteConfig";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function DELETE(
|
||||
_req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
const { id } = await params;
|
||||
const numId = Number(id);
|
||||
if (!Number.isInteger(numId)) {
|
||||
return NextResponse.json({ error: "无效 ID" }, { status: 400 });
|
||||
}
|
||||
deleteService(numId);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/** /api/admin/services — 服务项目管理。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSession, verifyCsrf } from "@/lib/auth";
|
||||
import { listServices, upsertService } from "@/lib/siteConfig";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
if (!(await getSession())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
return NextResponse.json({ services: listServices(false) });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
let body: {
|
||||
id?: number;
|
||||
title?: string;
|
||||
description?: string;
|
||||
price_note?: string;
|
||||
sort_order?: number;
|
||||
enabled?: number;
|
||||
};
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const title = String(body.title ?? "").trim();
|
||||
if (!title) {
|
||||
return NextResponse.json({ error: "服务名称必填" }, { status: 400 });
|
||||
}
|
||||
const row = upsertService({
|
||||
id: body.id,
|
||||
title,
|
||||
description: body.description,
|
||||
price_note: body.price_note,
|
||||
sort_order: body.sort_order,
|
||||
enabled: body.enabled,
|
||||
});
|
||||
return NextResponse.json({ ok: true, service: row });
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* /api/admin/upload — 图片上传(作品封面 / 服务配图)。
|
||||
* multipart/form-data: file → 保存到 public/work/,返回可访问路径。
|
||||
* 文件类型白名单 png/jpg/webp,大小 ≤ 8MB。
|
||||
*/
|
||||
import { NextResponse } from "next/server";
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { verifyCsrf } from "@/lib/auth";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const ALLOWED: Record<string, string> = {
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpg",
|
||||
"image/webp": "webp",
|
||||
};
|
||||
const MAX_SIZE = 8 * 1024 * 1024;
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
let form: FormData;
|
||||
try {
|
||||
form = await req.formData();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const file = form.get("file");
|
||||
if (!(file instanceof File)) {
|
||||
return NextResponse.json({ error: "缺少文件" }, { status: 400 });
|
||||
}
|
||||
const ext = ALLOWED[file.type];
|
||||
if (!ext) {
|
||||
return NextResponse.json(
|
||||
{ error: "仅支持 PNG / JPG / WebP" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (file.size > MAX_SIZE) {
|
||||
return NextResponse.json({ error: "文件不能超过 8MB" }, { status: 400 });
|
||||
}
|
||||
|
||||
const dir = path.join(process.cwd(), "public", "work");
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
const name = `${Date.now().toString(36)}-${crypto
|
||||
.randomBytes(4)
|
||||
.toString("hex")}.${ext}`;
|
||||
await fs.writeFile(
|
||||
path.join(dir, name),
|
||||
Buffer.from(await file.arrayBuffer())
|
||||
);
|
||||
return NextResponse.json({ ok: true, url: `/work/${name}` });
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/** /api/admin/works/[id] — 删除作品。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { verifyCsrf } from "@/lib/auth";
|
||||
import { deleteWork } from "@/lib/siteConfig";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function DELETE(
|
||||
_req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
const { id } = await params;
|
||||
const numId = Number(id);
|
||||
if (!Number.isInteger(numId)) {
|
||||
return NextResponse.json({ error: "无效 ID" }, { status: 400 });
|
||||
}
|
||||
deleteWork(numId);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/** /api/admin/works — 作品列表 + 新增/更新(upsert by slug)。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSession, verifyCsrf } from "@/lib/auth";
|
||||
import { listWorks, upsertWork, type WorkRow } from "@/lib/siteConfig";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
if (!(await getSession())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
return NextResponse.json({ works: listWorks(false) });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (!(await verifyCsrf())) {
|
||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
}
|
||||
let body: Partial<WorkRow> & { name?: string };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const name = String(body.name ?? "").trim();
|
||||
if (!name) {
|
||||
return NextResponse.json({ error: "作品名称必填" }, { status: 400 });
|
||||
}
|
||||
const row = upsertWork({ ...body, name });
|
||||
return NextResponse.json({ ok: true, work: row });
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/** /api/captcha/new — 生成滑块挑战(公开)。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { createCaptchaChallenge } from "@/lib/captcha";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST() {
|
||||
const challenge = createCaptchaChallenge();
|
||||
return NextResponse.json(challenge);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/** /api/captcha/verify — 校验滑块位置,通过则签发一次性提交令牌。 */
|
||||
import { NextResponse } from "next/server";
|
||||
import { verifyCaptcha } from "@/lib/captcha";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
let body: { token?: string; x?: number };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
const token = String(body.token ?? "");
|
||||
const x = Number(body.x);
|
||||
if (!token || !Number.isFinite(x)) {
|
||||
return NextResponse.json({ error: "参数错误" }, { status: 400 });
|
||||
}
|
||||
const result = verifyCaptcha(token, Math.round(x));
|
||||
if (!result.ok) {
|
||||
return NextResponse.json({ error: result.error }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ ok: true, submitToken: result.submitToken });
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* /api/inquiry — 服务咨询提交(公开)。
|
||||
* 必须携带滑块验证通过后签发的一次性 submitToken。
|
||||
* 入库后异步发通知邮件到 chenchun@virtheart.com(失败不影响入库,
|
||||
* 状态记录在 inquiries.email_sent / email_error,后台留言箱可见)。
|
||||
*/
|
||||
import { NextResponse } from "next/server";
|
||||
import { consumeSubmitToken } from "@/lib/captcha";
|
||||
import { createInquiry, markInquiryEmail } from "@/lib/siteConfig";
|
||||
import { sendInquiryNotification } from "@/lib/mailer";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const NAME_MAX = 40;
|
||||
const CONTACT_MAX = 80;
|
||||
const MESSAGE_MAX = 2000;
|
||||
|
||||
export async function POST(req: Request) {
|
||||
let body: {
|
||||
submitToken?: string;
|
||||
name?: string;
|
||||
contact?: string;
|
||||
serviceSlug?: string;
|
||||
serviceTitle?: string;
|
||||
message?: string;
|
||||
};
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
|
||||
const name = String(body.name ?? "").trim();
|
||||
const contact = String(body.contact ?? "").trim();
|
||||
const message = String(body.message ?? "").trim();
|
||||
const serviceSlug = String(body.serviceSlug ?? "").slice(0, 80);
|
||||
const serviceTitle = String(body.serviceTitle ?? "").slice(0, 120);
|
||||
|
||||
if (!name || !contact || !message) {
|
||||
return NextResponse.json(
|
||||
{ error: "姓名、联系方式和留言内容都不能为空" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (name.length > NAME_MAX || contact.length > CONTACT_MAX || message.length > MESSAGE_MAX) {
|
||||
return NextResponse.json({ error: "内容超出长度限制" }, { status: 400 });
|
||||
}
|
||||
|
||||
// 滑块一次性令牌校验(消费即作废,防重放)
|
||||
const submitToken = String(body.submitToken ?? "");
|
||||
if (!submitToken || !consumeSubmitToken(submitToken)) {
|
||||
return NextResponse.json(
|
||||
{ error: "验证已失效,请重新完成滑块验证" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const inquiry = createInquiry({
|
||||
name,
|
||||
contact,
|
||||
service_slug: serviceSlug,
|
||||
service_title: serviceTitle,
|
||||
message,
|
||||
});
|
||||
|
||||
// 邮件通知:失败不阻塞提交
|
||||
try {
|
||||
await sendInquiryNotification({
|
||||
id: inquiry.id,
|
||||
name: inquiry.name,
|
||||
contact: inquiry.contact,
|
||||
service_title: inquiry.service_title,
|
||||
message: inquiry.message,
|
||||
created_at: inquiry.created_at,
|
||||
});
|
||||
markInquiryEmail(inquiry.id, true);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
markInquiryEmail(inquiry.id, false, msg.slice(0, 300));
|
||||
console.error("[inquiry] 邮件通知失败:", msg);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, id: inquiry.id });
|
||||
}
|
||||
@@ -140,6 +140,26 @@ html::-webkit-scrollbar, body::-webkit-scrollbar {
|
||||
animation: caret-blink 1s step-end infinite;
|
||||
}
|
||||
|
||||
/* 滑块验证码 — 校验失败抖动。 */
|
||||
@keyframes vh-shake {
|
||||
0%,
|
||||
100% {
|
||||
transform: translateX(0);
|
||||
}
|
||||
20% {
|
||||
transform: translateX(-6px);
|
||||
}
|
||||
40% {
|
||||
transform: translateX(6px);
|
||||
}
|
||||
60% {
|
||||
transform: translateX(-4px);
|
||||
}
|
||||
80% {
|
||||
transform: translateX(4px);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes hsstFadeIn {
|
||||
0% {
|
||||
opacity: 0;
|
||||
|
||||
+8
-32
@@ -1,16 +1,6 @@
|
||||
import type { Metadata } from "next";
|
||||
import localFont from "next/font/local";
|
||||
import "./globals.css";
|
||||
import SmoothScroller from "@/components/dom/SmoothScroller";
|
||||
import Header from "@/components/dom/Header";
|
||||
import BackgroundGrid from "@/components/dom/BackgroundGrid";
|
||||
import CustomScrollbar from "@/components/dom/CustomScrollbar";
|
||||
import LoadingOverlay from "@/components/dom/LoadingOverlay";
|
||||
import { HeroScene } from "@/components/canvas/HeroScene";
|
||||
import { PasscodeAccessProvider } from "@/components/dom/PasscodeAccess";
|
||||
import { FullscreenTransitionProvider } from "@/lib/routeTransition";
|
||||
import RouteTransitionOverlay from "@/components/dom/RouteTransitionOverlay";
|
||||
import { getPasscodeAccessServer } from "@/lib/passcodeServer";
|
||||
|
||||
const tiktokSans = localFont({
|
||||
src: "./fonts/TikTokSans.ttf",
|
||||
@@ -34,18 +24,21 @@ const departureMono = localFont({
|
||||
// 以 <link> 引入 —— 见下方 <head>;不进 next/font 管线(整包 5MB/字重)。
|
||||
|
||||
export const metadata: Metadata = {
|
||||
// Detail pages override the title via generateMetadata in
|
||||
// app/work/[slug]/layout.tsx (original: per-page title).
|
||||
// 站点标题/描述可在 /virtheart 后台「配置」中修改(site_config 表)。
|
||||
title: "北极新一©2026",
|
||||
description: "设计与工程 / 数字产品与 AI 应用 © 2026",
|
||||
};
|
||||
|
||||
export default async function RootLayout({
|
||||
/**
|
||||
* 根布局 — 仅承载 html/body/字体/全局样式。
|
||||
* 站点壳层(画布、Header、滚动)在 app/(site)/layout.tsx;
|
||||
* 后台 /virtheart 使用独立布局,不加载站点壳层。
|
||||
*/
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const initialAccess = await getPasscodeAccessServer();
|
||||
return (
|
||||
<html
|
||||
lang="zh-CN"
|
||||
@@ -85,24 +78,7 @@ export default async function RootLayout({
|
||||
suppressHydrationWarning
|
||||
/>
|
||||
</head>
|
||||
<body className="min-h-screen relative">
|
||||
<PasscodeAccessProvider initialAccess={initialAccess}>
|
||||
<FullscreenTransitionProvider>
|
||||
{/* Original stacking (1:1): the WebGL canvas host is a plain `fixed`
|
||||
z-auto element first in the body; the grid overlay (z-20) comes
|
||||
AFTER it in DOM order so it paints above the canvas, exactly like
|
||||
the original's main > canvas + z-20 grid sibling. */}
|
||||
<HeroScene />
|
||||
<BackgroundGrid />
|
||||
<SmoothScroller />
|
||||
<Header />
|
||||
{children}
|
||||
<CustomScrollbar />
|
||||
<RouteTransitionOverlay />
|
||||
<LoadingOverlay />
|
||||
</FullscreenTransitionProvider>
|
||||
</PasscodeAccessProvider>
|
||||
</body>
|
||||
<body className="min-h-screen relative">{children}</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* AdminShell — 后台框架:顶栏(品牌 + 导航 + 退出)+ 内容区。
|
||||
* 设计与主站一致:点阵底、mono 标签、无圆角卡片堆砌。
|
||||
*/
|
||||
import Link from "next/link";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useCallback } from "react";
|
||||
|
||||
const NAV = [
|
||||
{ href: "/virtheart/dashboard", label: "概览" },
|
||||
{ href: "/virtheart/works", label: "作品" },
|
||||
{ href: "/virtheart/services", label: "服务" },
|
||||
{ href: "/virtheart/inquiries", label: "留言箱" },
|
||||
{ href: "/virtheart/settings", label: "配置" },
|
||||
];
|
||||
|
||||
export default function AdminShell({
|
||||
csrf,
|
||||
children,
|
||||
}: {
|
||||
csrf: string;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
await fetch("/api/admin/logout", { method: "POST" });
|
||||
router.replace("/virtheart/login");
|
||||
}, [router]);
|
||||
|
||||
return (
|
||||
<AdminCsrfContext.Provider value={csrf}>
|
||||
<div
|
||||
className="min-h-screen bg-b1"
|
||||
style={{
|
||||
backgroundImage:
|
||||
"radial-gradient(var(--grid-dot) 1px, transparent 1px)",
|
||||
backgroundSize: "22px 22px",
|
||||
}}
|
||||
>
|
||||
<header className="sticky top-0 z-40 border-b border-line bg-b1/95 backdrop-blur-sm">
|
||||
<div className="mx-auto flex max-w-6xl flex-wrap items-center gap-x-6 gap-y-2 px-4 py-3 lg:px-8">
|
||||
<Link
|
||||
href="/virtheart/dashboard"
|
||||
className="font-mono-2 text-xs uppercase tracking-widest text-l2"
|
||||
>
|
||||
virtheart · control
|
||||
</Link>
|
||||
<nav className="flex flex-wrap items-center gap-x-4 gap-y-1">
|
||||
{NAV.map((item) => {
|
||||
const active = pathname.startsWith(item.href);
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className={`font-mono text-sm transition-colors ${
|
||||
active
|
||||
? "text-l1 underline underline-offset-4"
|
||||
: "text-l2 hover:text-l1"
|
||||
}`}
|
||||
>
|
||||
{item.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
<div className="ml-auto flex items-center gap-4">
|
||||
<Link
|
||||
href="/"
|
||||
target="_blank"
|
||||
className="font-mono-2 text-xs text-l3 hover:text-l2"
|
||||
>
|
||||
查看站点 ↗
|
||||
</Link>
|
||||
<button
|
||||
type="button"
|
||||
onClick={logout}
|
||||
className="cursor-pointer font-mono-2 text-xs text-l3 hover:text-l1"
|
||||
>
|
||||
退出
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<main className="mx-auto max-w-6xl px-4 py-8 lg:px-8">{children}</main>
|
||||
</div>
|
||||
</AdminCsrfContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
const AdminCsrfContext = createContext("");
|
||||
|
||||
/** 后台 mutation 统一带上 CSRF 头。 */
|
||||
export function useAdminFetch() {
|
||||
const csrf = useContext(AdminCsrfContext);
|
||||
return useCallback(
|
||||
(input: string, init?: RequestInit) => {
|
||||
const headers = new Headers(init?.headers);
|
||||
headers.set(CSRF_HEADER_NAME, csrf);
|
||||
return fetch(input, { ...init, headers });
|
||||
},
|
||||
[csrf]
|
||||
);
|
||||
}
|
||||
|
||||
export const CSRF_HEADER_NAME = "x-vh-csrf";
|
||||
@@ -0,0 +1,75 @@
|
||||
/** /virtheart/dashboard — 概览:统计 + 最近留言。 */
|
||||
import Link from "next/link";
|
||||
import { listInquiries, listServices, listWorks } from "@/lib/siteConfig";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
function StatCard({ label, value, href }: { label: string; value: number | string; href: string }) {
|
||||
return (
|
||||
<Link
|
||||
href={href}
|
||||
className="block border border-line bg-be p-5 transition-colors hover:border-l2"
|
||||
>
|
||||
<div className="font-mono-2 text-xs uppercase tracking-widest text-l3">
|
||||
{label}
|
||||
</div>
|
||||
<div className="mt-2 font-bold text-3xl text-l1 tabular-nums">{value}</div>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
export default function DashboardPage() {
|
||||
const works = listWorks(false);
|
||||
const services = listServices(false);
|
||||
const inquiries = listInquiries(200);
|
||||
const newCount = inquiries.filter((i) => i.status === "new").length;
|
||||
const latest = inquiries.slice(0, 5);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-8">
|
||||
<div>
|
||||
<h1 className="font-bold text-2xl text-l1">概览</h1>
|
||||
<p className="mt-1 font-mono-2 text-sm text-l3">
|
||||
站点运行状态一目了然
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4 lg:grid-cols-4">
|
||||
<StatCard label="作品" value={works.length} href="/virtheart/works" />
|
||||
<StatCard label="已发布" value={works.filter((w) => w.published).length} href="/virtheart/works" />
|
||||
<StatCard label="服务" value={services.length} href="/virtheart/services" />
|
||||
<StatCard label="新留言" value={newCount} href="/virtheart/inquiries" />
|
||||
</div>
|
||||
|
||||
<section className="border border-line bg-be p-5">
|
||||
<div className="mb-4 flex items-center justify-between">
|
||||
<h2 className="font-semibold text-l1">最近留言</h2>
|
||||
<Link href="/virtheart/inquiries" className="font-mono-2 text-xs text-l2 hover:text-l1">
|
||||
全部 →
|
||||
</Link>
|
||||
</div>
|
||||
{latest.length === 0 ? (
|
||||
<p className="font-mono-2 text-sm text-l3">
|
||||
暂无留言——有人通过服务页联系你时会出现在这里。
|
||||
</p>
|
||||
) : (
|
||||
<ul className="flex flex-col divide-y divide-line">
|
||||
{latest.map((i) => (
|
||||
<li key={i.id} className="flex flex-wrap items-center gap-x-4 gap-y-1 py-3">
|
||||
<span className="font-mono-2 text-xs text-l3">#{i.id}</span>
|
||||
<span className="text-sm text-l1">{i.name}</span>
|
||||
<span className="text-sm text-l2">{i.service_title || "未选择服务"}</span>
|
||||
{i.status === "new" && (
|
||||
<span className="bg-selection px-1.5 text-xs text-black">NEW</span>
|
||||
)}
|
||||
<span className="ml-auto font-mono-2 text-xs text-l3">
|
||||
{i.created_at}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
"use client";
|
||||
|
||||
/** /virtheart/inquiries — 留言箱:查看 / 标记状态 / 删除。 */
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useAdminFetch } from "../AdminShell";
|
||||
import type { InquiryRow } from "@/lib/siteConfig";
|
||||
|
||||
const STATUS_LABELS: Record<InquiryRow["status"], string> = {
|
||||
new: "新",
|
||||
read: "已读",
|
||||
replied: "已回复",
|
||||
archived: "归档",
|
||||
};
|
||||
|
||||
const NEXT_STATUS: Record<InquiryRow["status"], InquiryRow["status"]> = {
|
||||
new: "read",
|
||||
read: "replied",
|
||||
replied: "archived",
|
||||
archived: "new",
|
||||
};
|
||||
|
||||
export default function InquiriesAdminPage() {
|
||||
const adminFetch = useAdminFetch();
|
||||
const [items, setItems] = useState<InquiryRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// 数据加载(async 内 setState,避免 effect 同步路径触发级联渲染)。
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const res = await adminFetch("/api/admin/inquiries");
|
||||
const data = (await res.json()) as { inquiries?: InquiryRow[] };
|
||||
if (cancelled) return;
|
||||
setItems(data.inquiries ?? []);
|
||||
setLoading(false);
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [adminFetch]);
|
||||
|
||||
// mutation 后刷新列表(不回置 loading 骨架)。
|
||||
const load = useCallback(async () => {
|
||||
const res = await adminFetch("/api/admin/inquiries");
|
||||
const data = (await res.json()) as { inquiries?: InquiryRow[] };
|
||||
setItems(data.inquiries ?? []);
|
||||
}, [adminFetch]);
|
||||
|
||||
const setStatus = useCallback(
|
||||
async (id: number, status: InquiryRow["status"]) => {
|
||||
await adminFetch("/api/admin/inquiries", {
|
||||
method: "PATCH",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ id, status }),
|
||||
});
|
||||
void load();
|
||||
},
|
||||
[adminFetch, load]
|
||||
);
|
||||
|
||||
const remove = useCallback(
|
||||
async (id: number) => {
|
||||
if (!window.confirm(`确定删除留言 #${id}?`)) return;
|
||||
await adminFetch(`/api/admin/inquiries?id=${id}`, { method: "DELETE" });
|
||||
void load();
|
||||
},
|
||||
[adminFetch, load]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6">
|
||||
<div>
|
||||
<h1 className="font-bold text-2xl text-l1">留言箱</h1>
|
||||
<p className="mt-1 font-mono-2 text-sm text-l3">
|
||||
服务页提交的咨询都会记录在这里,并同步邮件通知到联系邮箱
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<p className="font-mono-2 text-sm text-l3">加载中…</p>
|
||||
) : items.length === 0 ? (
|
||||
<div className="border border-dashed border-line p-8 text-center font-mono-2 text-sm text-l2">
|
||||
暂无留言。
|
||||
</div>
|
||||
) : (
|
||||
<ul className="flex flex-col gap-4">
|
||||
{items.map((i) => (
|
||||
<li key={i.id} className="border border-line bg-be p-4">
|
||||
<div className="flex flex-wrap items-center gap-x-3 gap-y-1">
|
||||
<span className="font-mono-2 text-xs text-l3">#{i.id}</span>
|
||||
<span className="text-sm text-l1">{i.name}</span>
|
||||
<a
|
||||
href={i.contact.includes("@") ? `mailto:${i.contact}` : "#"}
|
||||
className="font-mono text-sm text-l2 underline underline-offset-2"
|
||||
>
|
||||
{i.contact}
|
||||
</a>
|
||||
<span className="font-mono-2 text-xs text-l2">
|
||||
{i.service_title || "未选择服务"}
|
||||
</span>
|
||||
{i.status === "new" && (
|
||||
<span className="bg-selection px-1.5 font-mono-2 text-xs text-black">
|
||||
NEW
|
||||
</span>
|
||||
)}
|
||||
<span className="ml-auto font-mono-2 text-xs text-l3">
|
||||
{i.created_at}
|
||||
</span>
|
||||
</div>
|
||||
<p className="mt-3 text-sm leading-relaxed whitespace-pre-wrap text-l1">
|
||||
{i.message}
|
||||
</p>
|
||||
<div className="mt-3 flex flex-wrap items-center gap-x-4 gap-y-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStatus(i.id, NEXT_STATUS[i.status])}
|
||||
className="cursor-pointer font-mono text-sm text-l2 hover:text-l1"
|
||||
>
|
||||
标记为「{STATUS_LABELS[NEXT_STATUS[i.status]]}」
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => remove(i.id)}
|
||||
className="cursor-pointer font-mono text-sm text-l3 hover:text-l1"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
<span className="ml-auto font-mono-2 text-xs text-l3">
|
||||
{i.email_sent ? "邮件已通知" : i.email_error ? `邮件失败:${i.email_error}` : ""}
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { getSession, isAdmin } from "@/lib/auth";
|
||||
import type { Metadata } from "next";
|
||||
import AdminShell from "./AdminShell";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "控制台",
|
||||
robots: { index: false, follow: false },
|
||||
};
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/**
|
||||
* /virtheart/(protected) 布局 — 服务端鉴权:未登录一律跳 /virtheart/login。
|
||||
* 会话中的 csrf_token 传给 AdminShell,mutation 请求头统一回传。
|
||||
*/
|
||||
export default async function ProtectedLayout({
|
||||
children,
|
||||
}: Readonly<{ children: React.ReactNode }>) {
|
||||
if (!(await isAdmin())) {
|
||||
redirect("/virtheart/login");
|
||||
}
|
||||
const session = await getSession();
|
||||
return <AdminShell csrf={session?.csrf_token ?? ""}>{children}</AdminShell>;
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
"use client";
|
||||
|
||||
/** /virtheart/services — 服务项目管理(增删改 + 启用开关)。 */
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useAdminFetch } from "../AdminShell";
|
||||
import type { ServiceRow } from "@/lib/siteConfig";
|
||||
|
||||
const inputCls =
|
||||
"w-full border border-line bg-b1 px-3 py-2 font-mono text-sm text-l1 outline-none transition-colors focus:border-l1";
|
||||
|
||||
type Draft = {
|
||||
id?: number;
|
||||
title: string;
|
||||
description: string;
|
||||
price_note: string;
|
||||
sort_order: number;
|
||||
enabled: number;
|
||||
};
|
||||
|
||||
const EMPTY: Draft = { title: "", description: "", price_note: "", sort_order: 0, enabled: 1 };
|
||||
|
||||
export default function ServicesAdminPage() {
|
||||
const adminFetch = useAdminFetch();
|
||||
const [services, setServices] = useState<ServiceRow[]>([]);
|
||||
const [draft, setDraft] = useState<Draft | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// 数据加载(async 内 setState,避免 effect 同步路径触发级联渲染)。
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const res = await adminFetch("/api/admin/services");
|
||||
const data = (await res.json()) as { services?: ServiceRow[] };
|
||||
if (cancelled) return;
|
||||
setServices(data.services ?? []);
|
||||
setLoading(false);
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [adminFetch]);
|
||||
|
||||
// mutation 后刷新列表(不回置 loading 骨架)。
|
||||
const load = useCallback(async () => {
|
||||
const res = await adminFetch("/api/admin/services");
|
||||
const data = (await res.json()) as { services?: ServiceRow[] };
|
||||
setServices(data.services ?? []);
|
||||
}, [adminFetch]);
|
||||
|
||||
const save = useCallback(async () => {
|
||||
if (!draft) return;
|
||||
setError(null);
|
||||
const res = await adminFetch("/api/admin/services", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(draft),
|
||||
});
|
||||
const data = (await res.json()) as { error?: string };
|
||||
if (!res.ok) {
|
||||
setError(data.error ?? "保存失败");
|
||||
return;
|
||||
}
|
||||
setDraft(null);
|
||||
void load();
|
||||
}, [draft, adminFetch, load]);
|
||||
|
||||
const remove = useCallback(
|
||||
async (id: number) => {
|
||||
if (!window.confirm(`确定删除服务 #${id}?`)) return;
|
||||
await adminFetch(`/api/admin/services/${id}`, { method: "DELETE" });
|
||||
void load();
|
||||
},
|
||||
[adminFetch, load]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h1 className="font-bold text-2xl text-l1">服务</h1>
|
||||
<p className="mt-1 font-mono-2 text-sm text-l3">
|
||||
/services 页面展示已启用的服务条目
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDraft({ ...EMPTY })}
|
||||
className="cursor-pointer border border-l1 bg-l1 px-4 py-2 font-mono text-sm text-b1 hover:opacity-85"
|
||||
>
|
||||
+ 新服务
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="border-l-2 border-selection pl-2 text-sm text-l1">{error}</p>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<p className="font-mono-2 text-sm text-l3">加载中…</p>
|
||||
) : services.length === 0 ? (
|
||||
<div className="border border-dashed border-line p-8 text-center font-mono-2 text-sm text-l2">
|
||||
暂无服务——服务页将只显示联系引导。
|
||||
</div>
|
||||
) : (
|
||||
<ul className="flex flex-col divide-y divide-line border border-line bg-be">
|
||||
{services.map((s) => (
|
||||
<li key={s.id} className="flex flex-wrap items-center gap-x-4 gap-y-2 p-4">
|
||||
<div className="min-w-0">
|
||||
<div className="text-sm text-l1">
|
||||
{s.title}
|
||||
{!s.enabled && (
|
||||
<span className="ml-2 border border-line px-1 font-mono-2 text-xs text-l3">
|
||||
已停用
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="max-w-xl truncate font-mono-2 text-xs text-l3">
|
||||
{s.description}
|
||||
</div>
|
||||
</div>
|
||||
<div className="ml-auto flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setDraft({
|
||||
id: s.id,
|
||||
title: s.title,
|
||||
description: s.description,
|
||||
price_note: s.price_note,
|
||||
sort_order: s.sort_order,
|
||||
enabled: s.enabled,
|
||||
})
|
||||
}
|
||||
className="cursor-pointer font-mono text-sm text-l2 hover:text-l1"
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => remove(s.id)}
|
||||
className="cursor-pointer font-mono text-sm text-l3 hover:text-l1"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{draft && (
|
||||
<div className="inset-0 z-50 fixed flex justify-center items-center overflow-y-auto bg-b1/80 p-4">
|
||||
<div className="w-full max-w-xl border border-line bg-be p-6 my-8">
|
||||
<h2 className="mb-5 font-bold text-lg text-l1">
|
||||
{draft.id ? "编辑服务" : "新增服务"}
|
||||
</h2>
|
||||
<div className="flex flex-col gap-4">
|
||||
<div>
|
||||
<label className="mb-1 block font-mono-2 text-xs text-l2">名称 *</label>
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.title}
|
||||
onChange={(e) => setDraft({ ...draft, title: e.target.value })}
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="mb-1 block font-mono-2 text-xs text-l2">描述</label>
|
||||
<textarea
|
||||
className={`${inputCls} min-h-20`}
|
||||
value={draft.description}
|
||||
onChange={(e) => setDraft({ ...draft, description: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="mb-1 block font-mono-2 text-xs text-l2">
|
||||
价格说明
|
||||
</label>
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.price_note}
|
||||
onChange={(e) => setDraft({ ...draft, price_note: e.target.value })}
|
||||
placeholder="¥X,XXX 起 / 面议"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="mb-1 block font-mono-2 text-xs text-l2">排序</label>
|
||||
<input
|
||||
type="number"
|
||||
className={inputCls}
|
||||
value={draft.sort_order}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, sort_order: Number(e.target.value) || 0 })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<label className="flex cursor-pointer items-center gap-2 text-sm text-l1">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={draft.enabled === 1}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, enabled: e.target.checked ? 1 : 0 })
|
||||
}
|
||||
/>
|
||||
启用(在服务页展示)
|
||||
</label>
|
||||
</div>
|
||||
<div className="mt-6 flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={save}
|
||||
disabled={!draft.title.trim()}
|
||||
className="cursor-pointer border border-l1 bg-l1 px-5 py-2 font-mono text-sm text-b1 hover:opacity-85 disabled:opacity-40"
|
||||
>
|
||||
保存
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDraft(null)}
|
||||
className="cursor-pointer border border-line px-5 py-2 font-mono text-sm text-l2 hover:text-l1"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* /virtheart/settings — 站点配置编辑(分组字段 + 修改密码)。
|
||||
* 改动保存到 site_config 表,前台页面即时读取生效。
|
||||
*/
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useAdminFetch } from "../AdminShell";
|
||||
import type { SiteConfig } from "@/lib/siteConfig";
|
||||
|
||||
const inputCls =
|
||||
"w-full border border-line bg-b1 px-3 py-2 font-mono text-sm text-l1 outline-none transition-colors focus:border-l1";
|
||||
const labelCls = "mb-1 block font-mono-2 text-xs text-l2";
|
||||
|
||||
const FIELD_LABELS: Record<keyof SiteConfig, string> = {
|
||||
site_title: "浏览器标题",
|
||||
site_description: "站点描述(SEO)",
|
||||
brand_name: "Header 品牌名",
|
||||
copyright_name: "页脚版权名",
|
||||
hero_line1: "首屏标签 · 第一段",
|
||||
hero_line2: "首屏标签 · 第二段",
|
||||
hero_tagline: "首屏标语",
|
||||
hero_big_1: "首屏大字 · 行一",
|
||||
hero_big_2: "首屏大字 · 行二",
|
||||
hero_big_3: "首屏大字 · 行三",
|
||||
intro_prefix: "自我介绍 · 前半句",
|
||||
intro_suffix: "自我介绍 · 后半句",
|
||||
intro_brand: "自我介绍 · 品牌位(口令解锁前显示 ■■■)",
|
||||
about_p1: "关于 · 第一段",
|
||||
about_p2: "关于 · 第二段(链接前)",
|
||||
about_p2_link_label: "关于 · 链接文字",
|
||||
about_p2_link_href: "关于 · 链接地址",
|
||||
footer_headline: "页脚大字(用 | 分行)",
|
||||
contact_email: "联系邮箱",
|
||||
work_empty_headline: "作品区空状态 · 大字(用 | 分行)",
|
||||
work_empty_note: "作品区空状态 · 说明",
|
||||
work_empty_cta_prefix: "作品区空状态 · CTA 前缀",
|
||||
work_empty_cta_suffix: "作品区空状态 · CTA 中缀",
|
||||
work_empty_cta_action: "作品区空状态 · CTA 动作文字",
|
||||
services_hero: "服务页大字(用 | 分行)",
|
||||
services_submit_note: "服务页 · 提交说明",
|
||||
};
|
||||
|
||||
const MULTILINE: (keyof SiteConfig)[] = [
|
||||
"about_p1",
|
||||
"about_p2",
|
||||
"intro_prefix",
|
||||
"intro_suffix",
|
||||
"work_empty_note",
|
||||
"services_submit_note",
|
||||
];
|
||||
|
||||
const GROUP_TITLES: Record<string, string> = {
|
||||
basic: "基础",
|
||||
hero: "首屏",
|
||||
about: "关于",
|
||||
footer: "页脚",
|
||||
empty: "作品区空状态",
|
||||
services: "服务页",
|
||||
};
|
||||
|
||||
const GROUPS: { id: string; keys: (keyof SiteConfig)[] }[] = [
|
||||
{ id: "basic", keys: ["site_title", "site_description", "brand_name", "copyright_name"] },
|
||||
{
|
||||
id: "hero",
|
||||
keys: [
|
||||
"hero_line1",
|
||||
"hero_line2",
|
||||
"hero_tagline",
|
||||
"hero_big_1",
|
||||
"hero_big_2",
|
||||
"hero_big_3",
|
||||
"intro_prefix",
|
||||
"intro_suffix",
|
||||
"intro_brand",
|
||||
],
|
||||
},
|
||||
{ id: "about", keys: ["about_p1", "about_p2", "about_p2_link_label", "about_p2_link_href"] },
|
||||
{ id: "footer", keys: ["footer_headline", "contact_email"] },
|
||||
{
|
||||
id: "empty",
|
||||
keys: [
|
||||
"work_empty_headline",
|
||||
"work_empty_note",
|
||||
"work_empty_cta_prefix",
|
||||
"work_empty_cta_suffix",
|
||||
"work_empty_cta_action",
|
||||
],
|
||||
},
|
||||
{ id: "services", keys: ["services_hero", "services_submit_note"] },
|
||||
];
|
||||
|
||||
export default function SettingsAdminPage() {
|
||||
const adminFetch = useAdminFetch();
|
||||
const [config, setConfig] = useState<SiteConfig | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saved, setSaved] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [newPassword, setNewPassword] = useState("");
|
||||
const [pwMsg, setPwMsg] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
void (async () => {
|
||||
const res = await adminFetch("/api/admin/config");
|
||||
const data = (await res.json()) as { config?: SiteConfig };
|
||||
setConfig(data.config ?? null);
|
||||
})();
|
||||
}, [adminFetch]);
|
||||
|
||||
const save = useCallback(async () => {
|
||||
if (!config) return;
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
setSaved(false);
|
||||
const res = await adminFetch("/api/admin/config", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(config),
|
||||
});
|
||||
setSaving(false);
|
||||
if (!res.ok) {
|
||||
const data = (await res.json()) as { error?: string };
|
||||
setError(data.error ?? "保存失败");
|
||||
return;
|
||||
}
|
||||
setSaved(true);
|
||||
window.setTimeout(() => setSaved(false), 2000);
|
||||
}, [config, adminFetch]);
|
||||
|
||||
const changePassword = useCallback(async () => {
|
||||
setPwMsg(null);
|
||||
if (newPassword.length < 8) {
|
||||
setPwMsg("密码至少 8 位");
|
||||
return;
|
||||
}
|
||||
const res = await adminFetch("/api/admin/password", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ password: newPassword }),
|
||||
});
|
||||
const data = (await res.json()) as { error?: string };
|
||||
if (!res.ok) {
|
||||
setPwMsg(data.error ?? "修改失败");
|
||||
return;
|
||||
}
|
||||
setPwMsg("密码已更新,下次登录生效");
|
||||
setNewPassword("");
|
||||
}, [newPassword, adminFetch]);
|
||||
|
||||
if (!config) {
|
||||
return <p className="font-mono-2 text-sm text-l3">加载中…</p>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-8">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h1 className="font-bold text-2xl text-l1">配置</h1>
|
||||
<p className="mt-1 font-mono-2 text-sm text-l3">
|
||||
站点文案与信息,保存后前台即时生效
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
{saved && (
|
||||
<span className="font-mono-2 text-xs text-l2">已保存 ✓</span>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={save}
|
||||
disabled={saving}
|
||||
className="cursor-pointer border border-l1 bg-l1 px-4 py-2 font-mono text-sm text-b1 hover:opacity-85 disabled:opacity-40"
|
||||
>
|
||||
{saving ? "保存中…" : "保存全部"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="border-l-2 border-selection pl-2 text-sm text-l1">{error}</p>
|
||||
)}
|
||||
|
||||
{GROUPS.map((group) => (
|
||||
<section key={group.id} className="border border-line bg-be p-5">
|
||||
<h2 className="mb-4 font-semibold text-l1">
|
||||
{GROUP_TITLES[group.id]}
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
|
||||
{group.keys.map((key) => (
|
||||
<div
|
||||
key={key}
|
||||
className={MULTILINE.includes(key) ? "lg:col-span-2" : ""}
|
||||
>
|
||||
<label className={labelCls}>{FIELD_LABELS[key]}</label>
|
||||
{MULTILINE.includes(key) ? (
|
||||
<textarea
|
||||
className={`${inputCls} min-h-20`}
|
||||
value={config[key]}
|
||||
onChange={(e) => setConfig({ ...config, [key]: e.target.value })}
|
||||
/>
|
||||
) : (
|
||||
<input
|
||||
className={inputCls}
|
||||
value={config[key]}
|
||||
onChange={(e) => setConfig({ ...config, [key]: e.target.value })}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
))}
|
||||
|
||||
<section className="border border-line bg-be p-5">
|
||||
<h2 className="mb-4 font-semibold text-l1">修改密码</h2>
|
||||
<div className="flex flex-wrap items-end gap-3">
|
||||
<div className="w-full max-w-xs">
|
||||
<label className={labelCls}>新密码(至少 8 位)</label>
|
||||
<input
|
||||
type="password"
|
||||
className={inputCls}
|
||||
value={newPassword}
|
||||
onChange={(e) => setNewPassword(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={changePassword}
|
||||
disabled={!newPassword}
|
||||
className="cursor-pointer border border-line px-4 py-2 font-mono text-sm text-l2 hover:text-l1 hover:border-l2 disabled:opacity-40"
|
||||
>
|
||||
更新密码
|
||||
</button>
|
||||
{pwMsg && <span className="font-mono-2 text-xs text-l2">{pwMsg}</span>}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,401 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* /virtheart/works — 作品管理:列表 + 新增/编辑。
|
||||
* 单页内联编辑(终端风),字段与 lib/siteConfig 的 works 表对应。
|
||||
*/
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useAdminFetch } from "../AdminShell";
|
||||
import type { WorkRow } from "@/lib/siteConfig";
|
||||
|
||||
const GRID_PRESETS = [
|
||||
{ label: "大卡(8列居中)", value: "col-span-12 lg:col-span-8 lg:col-start-3" },
|
||||
{ label: "中卡(6列)", value: "col-span-12 lg:col-span-6 xl:col-span-4" },
|
||||
{ label: "宽左(5列)", value: "col-span-12 lg:col-start-1 lg:col-span-6 xl:col-span-5" },
|
||||
{ label: "宽右(5列)", value: "col-span-12 lg:col-span-6 xl:col-span-5 lg:col-start-7 xl:col-start-7" },
|
||||
{ label: "小卡(3列)", value: "col-span-6 lg:col-span-4 xl:col-span-3" },
|
||||
{ label: "小卡右(3列)", value: "col-span-6 lg:col-start-9 lg:col-span-4 xl:col-start-10 xl:col-span-3" },
|
||||
];
|
||||
|
||||
const TYPE_OPTIONS = [
|
||||
{ label: "文章", value: "post" },
|
||||
{ label: "工具", value: "tools" },
|
||||
{ label: "活动", value: "event" },
|
||||
];
|
||||
|
||||
type Draft = {
|
||||
id?: number;
|
||||
slug?: string;
|
||||
name: string;
|
||||
year: string;
|
||||
type: string;
|
||||
href: string;
|
||||
image_url: string;
|
||||
hover_image_url: string;
|
||||
grid_class: string;
|
||||
coding_project: number;
|
||||
sort_order: number;
|
||||
published: number;
|
||||
};
|
||||
|
||||
const EMPTY_DRAFT: Draft = {
|
||||
name: "",
|
||||
year: new Date().getFullYear().toString(),
|
||||
type: "post",
|
||||
href: "",
|
||||
image_url: "",
|
||||
hover_image_url: "",
|
||||
grid_class: GRID_PRESETS[1].value,
|
||||
coding_project: 0,
|
||||
sort_order: 0,
|
||||
published: 1,
|
||||
};
|
||||
|
||||
const inputCls =
|
||||
"w-full border border-line bg-b1 px-3 py-2 font-mono text-sm text-l1 outline-none transition-colors focus:border-l1";
|
||||
const labelCls = "mb-1 block font-mono-2 text-xs text-l2";
|
||||
|
||||
export default function WorksAdminPage() {
|
||||
const adminFetch = useAdminFetch();
|
||||
const [works, setWorks] = useState<WorkRow[]>([]);
|
||||
const [draft, setDraft] = useState<Draft | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
|
||||
// 数据加载(async 内 setState,避免 effect 同步路径触发级联渲染)。
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const res = await adminFetch("/api/admin/works");
|
||||
const data = (await res.json()) as { works?: WorkRow[] };
|
||||
if (cancelled) return;
|
||||
setWorks(data.works ?? []);
|
||||
setLoading(false);
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [adminFetch]);
|
||||
|
||||
// mutation 后刷新列表(不回置 loading 骨架)。
|
||||
const load = useCallback(async () => {
|
||||
const res = await adminFetch("/api/admin/works");
|
||||
const data = (await res.json()) as { works?: WorkRow[] };
|
||||
setWorks(data.works ?? []);
|
||||
}, [adminFetch]);
|
||||
|
||||
const save = useCallback(async () => {
|
||||
if (!draft) return;
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
const res = await adminFetch("/api/admin/works", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(draft),
|
||||
});
|
||||
const data = (await res.json()) as { error?: string };
|
||||
setSaving(false);
|
||||
if (!res.ok) {
|
||||
setError(data.error ?? "保存失败");
|
||||
return;
|
||||
}
|
||||
setDraft(null);
|
||||
void load();
|
||||
}, [draft, adminFetch, load]);
|
||||
|
||||
const remove = useCallback(
|
||||
async (id: number) => {
|
||||
if (!window.confirm(`确定删除作品 #${id}?`)) return;
|
||||
await adminFetch(`/api/admin/works/${id}`, { method: "DELETE" });
|
||||
void load();
|
||||
},
|
||||
[adminFetch, load]
|
||||
);
|
||||
|
||||
const upload = useCallback(
|
||||
async (file: File, field: "image_url" | "hover_image_url") => {
|
||||
setUploading(true);
|
||||
setError(null);
|
||||
const form = new FormData();
|
||||
form.append("file", file);
|
||||
const res = await adminFetch("/api/admin/upload", {
|
||||
method: "POST",
|
||||
body: form,
|
||||
});
|
||||
const data = (await res.json()) as { url?: string; error?: string };
|
||||
setUploading(false);
|
||||
if (!res.ok || !data.url) {
|
||||
setError(data.error ?? "上传失败");
|
||||
return;
|
||||
}
|
||||
setDraft((d) => (d ? { ...d, [field]: data.url as string } : d));
|
||||
},
|
||||
[adminFetch]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h1 className="font-bold text-2xl text-l1">作品</h1>
|
||||
<p className="mt-1 font-mono-2 text-sm text-l3">
|
||||
首页「精选作品」网格按排序展示已发布条目
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDraft({ ...EMPTY_DRAFT })}
|
||||
className="cursor-pointer border border-l1 bg-l1 px-4 py-2 font-mono text-sm text-b1 hover:opacity-85"
|
||||
>
|
||||
+ 新作品
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="border-l-2 border-selection pl-2 text-sm text-l1">{error}</p>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<p className="font-mono-2 text-sm text-l3">加载中…</p>
|
||||
) : works.length === 0 && !draft ? (
|
||||
<div className="border border-dashed border-line p-8 text-center">
|
||||
<p className="font-mono-2 text-sm text-l2">
|
||||
暂无作品——首页作品区当前展示「制作中」占位。
|
||||
</p>
|
||||
<p className="mt-1 font-mono-2 text-xs text-l3">
|
||||
添加第一个作品后,占位自动消失。
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
works.length > 0 && (
|
||||
<ul className="flex flex-col divide-y divide-line border border-line bg-be">
|
||||
{works.map((w) => (
|
||||
<li key={w.id} className="flex flex-wrap items-center gap-x-4 gap-y-2 p-4">
|
||||
{w.image_url && (
|
||||
// eslint-disable-next-line @next/next/no-img-element -- 后台缩略图
|
||||
<img
|
||||
src={w.image_url}
|
||||
alt=""
|
||||
className="h-12 w-16 border border-line object-cover"
|
||||
/>
|
||||
)}
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-sm text-l1">
|
||||
{w.name}
|
||||
{!w.published && (
|
||||
<span className="ml-2 border border-line px-1 font-mono-2 text-xs text-l3">
|
||||
未发布
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="font-mono-2 text-xs text-l3">
|
||||
{w.year} · {w.slug} · 排序 {w.sort_order}
|
||||
</div>
|
||||
</div>
|
||||
<div className="ml-auto flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setDraft({
|
||||
id: w.id,
|
||||
slug: w.slug,
|
||||
name: w.name,
|
||||
year: w.year,
|
||||
type: w.type,
|
||||
href: w.href,
|
||||
image_url: w.image_url,
|
||||
hover_image_url: w.hover_image_url,
|
||||
grid_class: w.grid_class,
|
||||
coding_project: w.coding_project,
|
||||
sort_order: w.sort_order,
|
||||
published: w.published,
|
||||
})
|
||||
}
|
||||
className="cursor-pointer font-mono text-sm text-l2 hover:text-l1"
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => remove(w.id)}
|
||||
className="cursor-pointer font-mono text-sm text-l3 hover:text-l1"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
)}
|
||||
|
||||
{draft && (
|
||||
<div className="fixed inset-0 z-50 flex justify-center items-center overflow-y-auto bg-b1/80 p-4">
|
||||
<div className="w-full max-w-2xl border border-line bg-be p-6 my-8">
|
||||
<h2 className="mb-5 font-bold text-lg text-l1">
|
||||
{draft.id ? `编辑作品 #${draft.id}` : "新增作品"}
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<div className="sm:col-span-2">
|
||||
<label className={labelCls}>名称 *</label>
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.name}
|
||||
onChange={(e) => setDraft({ ...draft, name: e.target.value })}
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>年份</label>
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.year}
|
||||
onChange={(e) => setDraft({ ...draft, year: e.target.value })}
|
||||
placeholder="2026"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>类型</label>
|
||||
<select
|
||||
className={inputCls}
|
||||
value={draft.type}
|
||||
onChange={(e) => setDraft({ ...draft, type: e.target.value })}
|
||||
>
|
||||
{TYPE_OPTIONS.map((o) => (
|
||||
<option key={o.value} value={o.value}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="sm:col-span-2">
|
||||
<label className={labelCls}>链接(外链 https://… 或站内 /path,留空则卡片不可点击)</label>
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.href}
|
||||
onChange={(e) => setDraft({ ...draft, href: e.target.value })}
|
||||
placeholder="https://example.com"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>封面图</label>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.image_url}
|
||||
onChange={(e) => setDraft({ ...draft, image_url: e.target.value })}
|
||||
placeholder="/work/xxx.png"
|
||||
/>
|
||||
<label className="shrink-0 cursor-pointer border border-line px-3 py-2 font-mono-2 text-xs text-l2 hover:text-l1 hover:border-l2">
|
||||
{uploading ? "…" : "上传"}
|
||||
<input
|
||||
type="file"
|
||||
accept="image/png,image/jpeg,image/webp"
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0];
|
||||
if (f) void upload(f, "image_url");
|
||||
e.target.value = "";
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>悬停图(可选)</label>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
className={inputCls}
|
||||
value={draft.hover_image_url}
|
||||
onChange={(e) => setDraft({ ...draft, hover_image_url: e.target.value })}
|
||||
placeholder="/work/xxx-hover.png"
|
||||
/>
|
||||
<label className="shrink-0 cursor-pointer border border-line px-3 py-2 font-mono-2 text-xs text-l2 hover:text-l1 hover:border-l2">
|
||||
{uploading ? "…" : "上传"}
|
||||
<input
|
||||
type="file"
|
||||
accept="image/png,image/jpeg,image/webp"
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0];
|
||||
if (f) void upload(f, "hover_image_url");
|
||||
e.target.value = "";
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div className="sm:col-span-2">
|
||||
<label className={labelCls}>网格位置</label>
|
||||
<select
|
||||
className={inputCls}
|
||||
value={draft.grid_class}
|
||||
onChange={(e) => setDraft({ ...draft, grid_class: e.target.value })}
|
||||
>
|
||||
{GRID_PRESETS.map((o) => (
|
||||
<option key={o.value} value={o.value}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>排序(小的在前)</label>
|
||||
<input
|
||||
type="number"
|
||||
className={inputCls}
|
||||
value={draft.sort_order}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, sort_order: Number(e.target.value) || 0 })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-end gap-6 pb-1">
|
||||
<label className="flex cursor-pointer items-center gap-2 text-sm text-l1">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={draft.published === 1}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, published: e.target.checked ? 1 : 0 })
|
||||
}
|
||||
/>
|
||||
发布
|
||||
</label>
|
||||
<label className="flex cursor-pointer items-center gap-2 text-sm text-l1">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={draft.coding_project === 1}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, coding_project: e.target.checked ? 1 : 0 })
|
||||
}
|
||||
/>
|
||||
代码项目角标
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={save}
|
||||
disabled={saving || !draft.name.trim()}
|
||||
className="cursor-pointer border border-l1 bg-l1 px-5 py-2 font-mono text-sm text-b1 hover:opacity-85 disabled:opacity-40"
|
||||
>
|
||||
{saving ? "保存中…" : "保存"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDraft(null)}
|
||||
className="cursor-pointer border border-line px-5 py-2 font-mono text-sm text-l2 hover:text-l1"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { Metadata } from "next";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "登录",
|
||||
robots: { index: false, follow: false },
|
||||
};
|
||||
|
||||
export default function VirtheartLayout({
|
||||
children,
|
||||
}: Readonly<{ children: React.ReactNode }>) {
|
||||
return children;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* /virtheart/login — 后台登录页。
|
||||
* 设计语言与主站对齐:暗底、等宽字体、点阵网格、scramble 式聚焦;
|
||||
* 不用传统后台模板。登录成功 → /virtheart/dashboard。
|
||||
*/
|
||||
import { useCallback, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function VirtheartLoginPage() {
|
||||
const router = useRouter();
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const submit = useCallback(
|
||||
async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch("/api/admin/login", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
const data = (await res.json()) as { error?: string };
|
||||
if (!res.ok) {
|
||||
setError(data.error ?? "登录失败");
|
||||
return;
|
||||
}
|
||||
router.replace("/virtheart/dashboard");
|
||||
} catch {
|
||||
setError("网络错误,请重试");
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
},
|
||||
[busy, username, password, router]
|
||||
);
|
||||
|
||||
return (
|
||||
<main
|
||||
className="min-h-screen flex justify-center items-center bg-b1 px-4"
|
||||
style={{
|
||||
backgroundImage: "radial-gradient(var(--grid-dot) 1px, transparent 1px)",
|
||||
backgroundSize: "22px 22px",
|
||||
}}
|
||||
>
|
||||
<form
|
||||
onSubmit={submit}
|
||||
className="w-full max-w-sm border border-line bg-be p-8"
|
||||
>
|
||||
<div className="mb-8 flex flex-col gap-1">
|
||||
<span className="font-mono-2 text-xs text-l3 uppercase tracking-widest">
|
||||
virtheart · control
|
||||
</span>
|
||||
<h1 className="font-bold text-2xl text-l1 leading-tight">
|
||||
后台登录
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
<label className="mb-1 block font-mono-2 text-xs text-l2">
|
||||
用户名
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
autoComplete="username"
|
||||
autoFocus
|
||||
className="mb-4 w-full border border-line bg-b1 px-3 py-2.5 font-mono text-sm text-l1 outline-none transition-colors focus:border-l1"
|
||||
/>
|
||||
|
||||
<label className="mb-1 block font-mono-2 text-xs text-l2">
|
||||
密码
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
autoComplete="current-password"
|
||||
className="mb-2 w-full border border-line bg-b1 px-3 py-2.5 font-mono text-sm text-l1 outline-none transition-colors focus:border-l1"
|
||||
/>
|
||||
|
||||
{error && (
|
||||
<p className="mt-2 border-l-2 border-selection pl-2 text-sm text-l1" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={busy || !username || !password}
|
||||
className="mt-6 w-full border border-l1 bg-l1 py-3 font-mono text-sm text-b1 uppercase tracking-widest transition-opacity disabled:opacity-40 cursor-pointer disabled:cursor-not-allowed hover:opacity-85"
|
||||
>
|
||||
{busy ? "验证中…" : "进入控制台"}
|
||||
</button>
|
||||
|
||||
<p className="mt-6 font-mono-2 text-xs text-l3">
|
||||
仅限站点所有者访问 · 会话 7 天有效
|
||||
</p>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { isAdmin } from "@/lib/auth";
|
||||
|
||||
/**
|
||||
* /virtheart — 后台暗门入口。
|
||||
* 未登录 → /virtheart/login;已登录 → /virtheart/dashboard。
|
||||
* 该路径不进站点地图、不进导航,仅手动访问。
|
||||
*/
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function VirtheartEntry() {
|
||||
if (await isAdmin()) {
|
||||
redirect("/virtheart/dashboard");
|
||||
}
|
||||
redirect("/virtheart/login");
|
||||
}
|
||||
Reference in New Issue
Block a user