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,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 });
|
||||
}
|
||||
Reference in New Issue
Block a user