Files
personal/app/(site)/services/ServicesClient.tsx
T

324 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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("");
// 蜜罐:人类不可见(CSS 藏 + tabIndex=-1 + autocomplete 关闭),
// 群发机器人会把它当普通字段填上 —— 提交接口静默丢弃。
const [honeypot, setHoneypot] = 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(),
website: honeypot,
}),
});
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, honeypot]
);
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-[1240px]">
<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>
{/* 双栏:左服务菜单 / 右表单(lg+ 并排,移动端上下堆叠) */}
<div className="mt-12 grid grid-cols-1 lg:grid-cols-12 gap-10 lg:gap-8 items-start">
{/* 服务菜单(左栏) */}
<section className="lg:col-span-6 xl:col-span-5" 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>
{/* 留言表单(右栏,lg+ sticky 跟随) */}
<section
className="lg:col-span-6 xl:col-span-7 flex flex-col gap-5 lg:sticky lg:top-24"
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>
{/* 蜜罐字段 — 人类不可见;机器人填了就被服务端静默丢弃 */}
<div aria-hidden="true" className="absolute left-[-9999px] top-auto h-px w-px overflow-hidden">
<label htmlFor="svc-website">网站(勿填)</label>
<input
id="svc-website"
type="text"
name="website"
tabIndex={-1}
autoComplete="off"
value={honeypot}
onChange={(e) => setHoneypot(e.target.value)}
/>
</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>
</div>
<SliderCaptcha
open={captchaOpen}
onClose={() => !submitting && setCaptchaOpen(false)}
onVerified={onCaptchaVerified}
/>
</article>
);
}