feat: 完成头像上传、站点主题配置与UI优化
- 新增附件模型与上传服务,拆分头像上传/管理逻辑 - 重构用户资料接口,移除直接修改头像字段,改用专用上传接口 - 添加站点公开设置API与管理员外观配置页 - 全局替换hover背景为accent-soft,优化交互一致性 - 完善头像组件,支持图片加载失败回退与src更新重置 - 新增用户状态事件总线,解决同页头像更新不及时问题 - 调整依赖包顺序与CI依赖版本 - 重构板块列表接口,新增帖子计数 - 迁移账号设置到用户主页tab,合并重复代码
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { apiChangePassword } from "@/lib/api";
|
||||
|
||||
/**
|
||||
* 修改密码表单:提交成功后后端已递增 TokenVersion 并清除全部登录 cookie,
|
||||
* 刷新 layout 使 Header 回到游客态,随后跳转登录页重新登录。
|
||||
*/
|
||||
export default function PasswordForm() {
|
||||
const router = useRouter();
|
||||
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);
|
||||
|
||||
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);
|
||||
// 后端已使全部 token 失效并清 cookie,刷新 layout 让 Header 回到游客态
|
||||
router.refresh();
|
||||
setTimeout(() => router.push("/login"), 1500);
|
||||
} else {
|
||||
setError(res.error || "修改失败");
|
||||
}
|
||||
} catch {
|
||||
setError("网络错误,请重试");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<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" role="status">密码修改成功,正在跳转登录页...</div>}
|
||||
{error && <div className="alert-error" role="alert">{error}</div>}
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pwd-old">当前密码</label>
|
||||
<input
|
||||
id="pwd-old"
|
||||
type="password"
|
||||
value={oldPassword}
|
||||
onChange={(e) => setOldPassword(e.target.value)}
|
||||
required
|
||||
autoComplete="current-password"
|
||||
className="field"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pwd-new">新密码(至少 6 位)</label>
|
||||
<input
|
||||
id="pwd-new"
|
||||
type="password"
|
||||
value={newPassword}
|
||||
onChange={(e) => setNewPassword(e.target.value)}
|
||||
required
|
||||
minLength={6}
|
||||
autoComplete="new-password"
|
||||
className="field"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pwd-confirm">确认新密码</label>
|
||||
<input
|
||||
id="pwd-confirm"
|
||||
type="password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
required
|
||||
minLength={6}
|
||||
autoComplete="new-password"
|
||||
className="field"
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitting || success}
|
||||
className="btn btn-primary w-full justify-center mt-2"
|
||||
>
|
||||
{submitting ? "提交中..." : "修改密码"}
|
||||
</button>
|
||||
</form>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { apiUpdateProfile, type User } from "@/lib/api";
|
||||
|
||||
interface ProfileFormProps {
|
||||
user: User;
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人资料编辑表单:昵称、头像 URL、邮箱、签名。
|
||||
* 提交成功后刷新 layout 使 Header 同步最新昵称/头像,并在页面内显示成功提示。
|
||||
*/
|
||||
export default function ProfileForm({ user }: ProfileFormProps) {
|
||||
const router = useRouter();
|
||||
const [nickname, setNickname] = useState(user.nickname || "");
|
||||
const [avatar, setAvatar] = useState(user.avatar || "");
|
||||
const [email, setEmail] = useState(user.email || "");
|
||||
const [signature, setSignature] = useState(user.signature || "");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [success, setSuccess] = useState("");
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError("");
|
||||
setSuccess("");
|
||||
|
||||
const trimmed = nickname.trim();
|
||||
if (!trimmed) {
|
||||
setError("昵称不能为空");
|
||||
return;
|
||||
}
|
||||
if (trimmed.length > 64) {
|
||||
setError("昵称不能超过 64 个字符");
|
||||
return;
|
||||
}
|
||||
if (signature.length > 255) {
|
||||
setError("签名不能超过 255 个字符");
|
||||
return;
|
||||
}
|
||||
|
||||
setSubmitting(true);
|
||||
try {
|
||||
const res = await apiUpdateProfile({
|
||||
nickname: trimmed,
|
||||
avatar: avatar.trim(),
|
||||
email: email.trim(),
|
||||
signature,
|
||||
});
|
||||
if (res.user) {
|
||||
setSuccess("资料已保存");
|
||||
// 刷新 layout 让 Header 同步最新昵称/头像
|
||||
router.refresh();
|
||||
} else {
|
||||
setError(res.error || "保存失败");
|
||||
}
|
||||
} catch {
|
||||
setError("网络错误,请重试");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<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-5">
|
||||
{success && <div className="alert-ok" role="status">{success}</div>}
|
||||
{error && <div className="alert-error" role="alert">{error}</div>}
|
||||
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pf-nickname">昵称</label>
|
||||
<input
|
||||
id="pf-nickname"
|
||||
type="text"
|
||||
value={nickname}
|
||||
onChange={(e) => setNickname(e.target.value)}
|
||||
required
|
||||
maxLength={64}
|
||||
className="field"
|
||||
placeholder="请输入昵称"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pf-avatar">头像链接</label>
|
||||
<input
|
||||
id="pf-avatar"
|
||||
type="url"
|
||||
value={avatar}
|
||||
onChange={(e) => setAvatar(e.target.value)}
|
||||
maxLength={512}
|
||||
className="field"
|
||||
placeholder="https://example.com/avatar.png"
|
||||
/>
|
||||
<p className="meta mt-2">填写图片 URL,留空则使用默认字母头像</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pf-email">邮箱</label>
|
||||
<input
|
||||
id="pf-email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
maxLength={128}
|
||||
className="field"
|
||||
placeholder="your@email.com"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="field-label" htmlFor="pf-signature">
|
||||
个性签名
|
||||
<span className="font-normal ml-1.5" style={{ color: "var(--ink-3)" }}>
|
||||
({signature.length}/255)
|
||||
</span>
|
||||
</label>
|
||||
<textarea
|
||||
id="pf-signature"
|
||||
value={signature}
|
||||
onChange={(e) => setSignature(e.target.value)}
|
||||
maxLength={255}
|
||||
rows={3}
|
||||
className="field"
|
||||
placeholder="介绍一下自己吧"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitting}
|
||||
className="btn btn-primary w-full justify-center mt-1"
|
||||
>
|
||||
{submitting ? "保存中..." : "保存资料"}
|
||||
</button>
|
||||
</form>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,50 +1,15 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import { cookies } from "next/headers";
|
||||
import { ChevronRight } from "lucide-react";
|
||||
import { redirect } from "next/navigation";
|
||||
import { authCookieHeader } from "@/lib/cookies";
|
||||
import { getMeCached } from "@/lib/serverData";
|
||||
import Avatar from "@/components/Avatar";
|
||||
import ProfileForm from "./ProfileForm";
|
||||
import PasswordForm from "./PasswordForm";
|
||||
|
||||
// SSR 直出账号信息:未登录直接跳登录页,登录后 F5 不再有"正在加载…"闪动
|
||||
export default async function SettingsPage() {
|
||||
// /settings 已融合到用户主页 /u/[id]?tab=settings
|
||||
// 未登录跳登录页,登录后跳自己的主页 settings tab
|
||||
export default async function SettingsRedirect() {
|
||||
const cookie = authCookieHeader(await cookies());
|
||||
const me = await getMeCached(cookie || undefined);
|
||||
const user = me.user;
|
||||
if (!user) {
|
||||
if (!me.user) {
|
||||
redirect(`/login?redirect=${encodeURIComponent(`/settings`)}`);
|
||||
}
|
||||
|
||||
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} src={user.avatar || undefined} 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>
|
||||
|
||||
<div className="space-y-7">
|
||||
<ProfileForm user={user} />
|
||||
<PasswordForm />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
redirect(`/u/${me.user.id}?tab=settings`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user