Files
jiang13-bbs/frontend/app/settings/page.tsx
freefire 1eaf3903d4 feat: 新增大量社区功能与基础组件,优化站点整体体验
- 新增事件总线式toast通知系统
- 新增头像、阅读进度、404/500错误页等基础组件
- 重构页脚、布局、分页、点赞按钮等现有组件
- 新增用户个人页、通知中心、帖子编辑/发布/置顶/推荐管理功能
- 优化登录注册页面样式与交互
- 修复路径类型文件引用,更新站点元信息与字体配置
2026-09-12 04:30:02 +08:00

141 lines
4.3 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";
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { ChevronRight } from "lucide-react";
import { apiChangePassword, apiMe, type User } from "@/lib/api";
import Avatar from "@/components/Avatar";
export default function SettingsPage() {
const router = useRouter();
const [user, setUser] = useState<User | null>(null);
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState(false);
useEffect(() => {
apiMe()
.then((res) => {
if (!res.user) {
router.push("/login");
return;
}
setUser(res.user);
})
.catch(() => router.push("/login"));
}, [router]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError("");
if (newPassword.length < 6) {
setError("新密码至少 6 位");
return;
}
if (newPassword !== confirmPassword) {
setError("两次输入的新密码不一致");
return;
}
setSubmitting(true);
try {
const res = await apiChangePassword(oldPassword, newPassword);
if (res.message) {
setSuccess(true);
setTimeout(() => router.push("/login"), 1500);
} else {
setError(res.error || "修改失败");
}
} catch (e) {
setError("网络错误,请重试");
} finally {
setSubmitting(false);
}
};
if (!user) {
return (
<div className="max-w-md mx-auto text-center py-20">
<p className="meta">正在加载…</p>
</div>
);
}
return (
<div className="max-w-md mx-auto">
<nav className="meta mb-5 flex items-center gap-1.5 text-[13px]">
<Link href="/" className="hover:text-[var(--accent)] transition-colors">首页</Link>
<ChevronRight size={13} />
<span style={{ color: "var(--accent)" }}>账号设置</span>
</nav>
<div className="mb-7">
<h1 className="text-[26px] sm:text-[30px] font-extrabold tracking-tight" style={{ color: "var(--ink)" }}>
账号设置
</h1>
</div>
<div className="panel p-5 mb-7 flex items-center gap-4">
<Avatar name={user.nickname || user.username} size={46} />
<div className="min-w-0">
<div className="font-bold truncate" style={{ color: "var(--ink)" }}>
{user.nickname}
</div>
<div className="meta">@{user.username} · {user.role === "admin" ? "管理员" : "普通用户"}</div>
</div>
</div>
<p className="font-bold text-[15px] mb-3.5" style={{ color: "var(--ink)" }}>修改密码</p>
<form onSubmit={handleSubmit} className="panel p-5 sm:p-6 space-y-4">
{success && <div className="alert-ok">密码修改成功,正在跳转登录页...</div>}
{error && <div className="alert-error">{error}</div>}
<div>
<label className="field-label">当前密码</label>
<input
type="password"
value={oldPassword}
onChange={(e) => setOldPassword(e.target.value)}
required
className="field"
/>
</div>
<div>
<label className="field-label">新密码(至少 6 位)</label>
<input
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
required
minLength={6}
className="field"
/>
</div>
<div>
<label className="field-label">确认新密码</label>
<input
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
minLength={6}
className="field"
/>
</div>
<button
type="submit"
disabled={submitting || success}
className="btn btn-primary w-full justify-center mt-2"
>
{submitting ? "提交中..." : "修改密码"}
</button>
</form>
</div>
);
}