feat: user signature (设置页编辑 + 帖子底部显示)

- schema: users.signature column (upstream aligned)
- API: posts query JOIN signature, /auth/me returns signature, PUT /users/:id updates signature
- Settings: signature textarea (Markdown hint), saved with profile
- PostDetail: signature shown under post content with divider
- README: remove upstream DB schema note (fixed upstream)
This commit is contained in:
lezi-fun committed 2026-08-10 14:46:05 +08:00
1 parent c8dcf32dd4
commit 694fa2bfaf
6 files changed
+16 -8

No files matched your search

-2
View File
@@ -154,8 +154,6 @@ npx @cloudflare/next-on-pages
两个分支使用**完全相同的 `schema.sql`**,共用同一个数据库实例(已验证:main 的 Express 代码与 next 的 Next.js 代码可同时连接同一 PostgreSQL 库,表结构、数据互不冲突)。
> 注意:上游 main 的 `server.js` 引用了 `is_pinned` / `signature` 等列,但这些列在上游仓库的 `schema.sql` 中缺失(未同步)。本分支的 `schema.sql` 是完整超集,已包含全部列,可直接用于两个分支。
## 🏗️ 项目结构
```
+1 -1
View File
@@ -9,7 +9,7 @@ export async function GET(req) {
}
try {
const r = await pool.query(
'SELECT id, username, avatar_url, bio, role, created_at FROM users WHERE id = $1',
'SELECT id, username, avatar_url, bio, signature, role, created_at FROM users WHERE id = $1',
[user.id]
);
if (!r.rows[0]) {
+1 -1
View File
@@ -35,7 +35,7 @@ export async function PUT(req, { params }) {
return Response.json({ error: '无权限修改他人资料' }, { status: 403 });
}
try {
await pool.query('UPDATE users SET username = $1, bio = $2 WHERE id = $3', [username, bio || '', id]);
await pool.query('UPDATE users SET username = $1, bio = $2, signature = $3 WHERE id = $4', [username, bio || '', signature || '', id]);
return Response.json({ success: true });
} catch (err) {
if (err.code === '23505') {
+5
View File
@@ -140,6 +140,11 @@ export default function PostDetail({ postId }) {
))}
</div>
)}
{post.signature && (
<div style={{ marginTop: 16, paddingTop: 12, borderTop: '1px solid var(--border-light)', fontSize: 12, color: 'var(--text-secondary)' }}>
{post.signature}
</div>
)}
</div>
<ReplyList postId={postId} onRefresh={load} />
</div>
+7 -2
View File
@@ -12,6 +12,7 @@ export default function SettingsPage() {
const { currentUser, setCurrentUser } = useApp();
const [username, setUsername] = useState(currentUser?.username || '');
const [bio, setBio] = useState(currentUser?.bio || '');
const [signature, setSignature] = useState(currentUser?.signature || '');
const [avatarUrl, setAvatarUrl] = useState(currentUser?.avatar_url || '');
const [avatarStatus, setAvatarStatus] = useState(null);
const avatarInputRef = useRef(null);
@@ -35,9 +36,9 @@ export default function SettingsPage() {
if (!currentUser) { alert('请先登录'); return; }
if (!username.trim()) { alert('用户名不能为空'); return; }
try {
const data = await API.updateProfile(currentUser.id, username.trim(), bio.trim());
const data = await API.updateProfile(currentUser.id, username.trim(), bio.trim(), signature.trim());
if (data.error) throw new Error(data.error);
setCurrentUser({ ...currentUser, username: username.trim(), bio: bio.trim() });
setCurrentUser({ ...currentUser, username: username.trim(), bio: bio.trim(), signature: signature.trim() });
alert('保存成功!');
} catch (err) {
alert('保存失败:' + err.message);
@@ -82,6 +83,10 @@ export default function SettingsPage() {
<label style={{ fontWeight: 600, fontSize: 14, display: 'block', marginBottom: 4 }}>个人简介</label>
<textarea rows={3} value={bio} style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, fontSize: 14, background: 'var(--bg)', color: 'var(--text)', resize: 'vertical' }} onChange={(e) => setBio(e.target.value)} />
</div>
<div style={{ marginBottom: 16 }}>
<label style={{ fontWeight: 600, fontSize: 14, display: 'block', marginBottom: 4 }}>个性签名 <span style={{ color: 'var(--text-light)', fontWeight: 400 }}>(显示在每篇帖子底部,支持 Markdown)</span></label>
<textarea rows={2} value={signature} placeholder="签名..." style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, fontSize: 14, background: 'var(--bg)', color: 'var(--text)', resize: 'vertical', fontFamily: 'inherit' }} onChange={(e) => setSignature(e.target.value)} />
</div>
<button className="btn-primary" style={{ width: '100%', padding: 10 }} onClick={handleSave}>保存设置</button>
</div>
+2 -2
View File
@@ -78,8 +78,8 @@ export const API = {
updateUserRole(userId, role) {
return apiFetch('/users/' + userId + '/role', { method: 'PUT', body: JSON.stringify({ role }) });
},
updateProfile(userId, username, bio) {
return apiFetch('/users/' + userId, { method: 'PUT', body: JSON.stringify({ username, bio }) });
updateProfile(userId, username, bio, signature) {
return apiFetch('/users/' + userId, { method: 'PUT', body: JSON.stringify({ username, bio, signature }) });
},
updateAvatar(userId, avatar_url) {
return apiFetch('/users/' + userId + '/avatar', { method: 'PUT', body: JSON.stringify({ avatar_url }) });