此提交完成了以下核心功能与优化: 1. 新增站点公告全流程支持:包含公告模型、CRUD接口、管理后台页面、公告详情页与首页展示 2. 实现评论楼层化与回复嵌套:重构评论系统为树状结构,支持回复评论与层级限制 3. 添加用户在线状态追踪:异步更新用户最后活跃时间并做SQL限频 4. 优化用户体验:移动端浮动操作条、统一板块标签样式、用户页签到积分展示、注册即登录优化 5. 其他细节优化:SEO站点地图新增公告页、评论分页与统计、登录/注册表单增强等
124 lines
3.8 KiB
TypeScript
124 lines
3.8 KiB
TypeScript
"use client";
|
||
|
||
import { useState, useEffect } from "react";
|
||
import { useRouter } from "next/navigation";
|
||
import Link from "next/link";
|
||
import { UserPlus } from "lucide-react";
|
||
import { apiRegister, apiMe } from "@/lib/api";
|
||
import { hasAuthCookieHint } from "@/lib/cookies";
|
||
|
||
export default function RegisterPage() {
|
||
const router = useRouter();
|
||
const [username, setUsername] = useState("");
|
||
const [email, setEmail] = useState("");
|
||
const [password, setPassword] = useState("");
|
||
const [error, setError] = useState("");
|
||
const [loading, setLoading] = useState(false);
|
||
|
||
// 已登录用户不必再注册,直接回首页
|
||
useEffect(() => {
|
||
if (!hasAuthCookieHint()) return;
|
||
apiMe()
|
||
.then((res) => {
|
||
if (res.user) router.replace("/");
|
||
})
|
||
.catch(() => {});
|
||
}, [router]);
|
||
|
||
const handleSubmit = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
setError("");
|
||
setLoading(true);
|
||
try {
|
||
const res = await apiRegister(username, email, password);
|
||
if (res.user) {
|
||
// 后端注册即签发登录态 cookie:直接进入社区,无需再手动登录
|
||
router.push("/");
|
||
router.refresh();
|
||
} else if (res.id) {
|
||
// 极端情况:注册成功但自动签发失败,引导去登录
|
||
router.push("/login");
|
||
} else {
|
||
setError(res.error || "注册失败");
|
||
}
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="max-w-md mx-auto pt-8 sm:pt-14">
|
||
<div className="mb-7 text-center">
|
||
<span
|
||
className="inline-flex w-14 h-14 rounded-2xl items-center justify-center mb-4"
|
||
style={{ background: "var(--accent-soft)", color: "var(--accent)" }}
|
||
>
|
||
<UserPlus size={26} />
|
||
</span>
|
||
<h1 className="text-[28px] font-extrabold tracking-tight" style={{ color: "var(--ink)" }}>
|
||
创建账号
|
||
</h1>
|
||
<p className="text-sm mt-2" style={{ color: "var(--ink-2)" }}>
|
||
加入姜十三,开始你的第一帖
|
||
</p>
|
||
</div>
|
||
|
||
<form onSubmit={handleSubmit} className="panel p-6 sm:p-7 space-y-4">
|
||
{error && <div className="alert-error" role="alert">{error}</div>}
|
||
<div>
|
||
<label className="field-label" htmlFor="reg-username">用户名</label>
|
||
<input
|
||
id="reg-username"
|
||
type="text"
|
||
value={username}
|
||
onChange={(e) => setUsername(e.target.value)}
|
||
required
|
||
minLength={3}
|
||
maxLength={32}
|
||
autoFocus
|
||
autoComplete="username"
|
||
className="field"
|
||
placeholder="3–32 位"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="field-label" htmlFor="reg-email">邮箱(可选)</label>
|
||
<input
|
||
id="reg-email"
|
||
type="email"
|
||
value={email}
|
||
onChange={(e) => setEmail(e.target.value)}
|
||
autoComplete="email"
|
||
className="field"
|
||
placeholder="you@example.com"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="field-label" htmlFor="reg-password">密码</label>
|
||
<input
|
||
id="reg-password"
|
||
type="password"
|
||
value={password}
|
||
onChange={(e) => setPassword(e.target.value)}
|
||
required
|
||
minLength={6}
|
||
autoComplete="new-password"
|
||
className="field"
|
||
placeholder="至少 6 位"
|
||
/>
|
||
</div>
|
||
<button type="submit" disabled={loading} className="btn btn-primary w-full justify-center mt-2">
|
||
{loading ? "注册中..." : "注册"}
|
||
</button>
|
||
</form>
|
||
|
||
<p className="meta text-center mt-5">
|
||
已有账号?
|
||
<Link href="/login" className="ml-1 font-semibold" style={{ color: "var(--accent)" }}>
|
||
去登录
|
||
</Link>
|
||
</p>
|
||
</div>
|
||
);
|
||
}
|