diff --git a/app/api/users/profile/[username]/route.js b/app/api/users/profile/[username]/route.js new file mode 100644 index 0000000..bedd00d --- /dev/null +++ b/app/api/users/profile/[username]/route.js @@ -0,0 +1,21 @@ +// GET /api/users/profile/:username — 用户公开信息(无需登录,用于用户主页) +import pool from '@/lib/db'; + +export async function GET(req, { params }) { + const { username } = await params; + if (!username) { + return Response.json({ error: '缺少用户名' }, { status: 400 }); + } + try { + const r = await pool.query( + 'SELECT id, username, avatar_url, bio, role, signature, created_at FROM users WHERE username = $1', + [username] + ); + if (r.rows.length === 0) { + return Response.json({ error: '用户不存在' }, { status: 404 }); + } + return Response.json(r.rows[0]); + } catch { + return Response.json({ error: '服务器错误' }, { status: 500 }); + } +} diff --git a/components/SettingsPage.js b/components/SettingsPage.js index 295020e..b82ebf4 100644 --- a/components/SettingsPage.js +++ b/components/SettingsPage.js @@ -1,7 +1,9 @@ 'use client'; -// 个人设置页:侧边栏布局(个人资料 / 安全设置 / 恢复码) -import { useState } from 'react'; +// 个人设置页:侧边栏布局 + 上下滑动平滑切换 +// - 三个面板纵向排列在滚动容器里,scroll-snap 每次滑动对齐一个面板 +// - 点侧边栏导航 → 平滑滚动到对应面板;滚动结束 → 自动更新导航高亮 +import { useState, useRef, useCallback } from 'react'; import { Icon } from './Icons'; import SettingsProfile from './SettingsProfile'; import AccountSecurity from './AccountSecurity'; @@ -15,6 +17,31 @@ const NAV = [ export default function SettingsPage() { const [tab, setTab] = useState('profile'); + const scrollRef = useRef(null); + const scrollTicking = useRef(false); + + // 滚动结束(或对齐后)更新高亮 + const handleScroll = useCallback(() => { + const el = scrollRef.current; + if (!el || scrollTicking.current) return; + scrollTicking.current = true; + requestAnimationFrame(() => { + scrollTicking.current = false; + const idx = Math.round(el.scrollTop / el.clientHeight); + const key = NAV[Math.min(Math.max(idx, 0), NAV.length - 1)]?.key; + if (key) setTab(key); + }); + }, []); + + // 点击导航 → 平滑滚动到对应面板 + const goTo = (key, e) => { + if (e) e.preventDefault(); + setTab(key); + const el = scrollRef.current; + if (!el) return; + const idx = NAV.findIndex((n) => n.key === key); + el.scrollTo({ top: idx * el.clientHeight, behavior: 'smooth' }); + }; return (
@@ -30,7 +57,7 @@ export default function SettingsPage() { href="#" className={'settings-nav-item' + (tab === n.key ? ' active' : '')} data-settings-tab={n.key} - onClick={(e) => { e.preventDefault(); setTab(n.key); }} + onClick={(e) => goTo(n.key, e)} > {n.label} @@ -38,10 +65,29 @@ export default function SettingsPage() { ))} -
- {tab === 'profile' && } - {tab === 'security' && } - {tab === 'recovery' && } +
+
+
+ +
+
+ +
+
+ +
+
diff --git a/components/UserProfile.js b/components/UserProfile.js index 67fb7e5..6499471 100644 --- a/components/UserProfile.js +++ b/components/UserProfile.js @@ -22,10 +22,9 @@ export default function UserProfile({ username }) { const load = useCallback(async () => { setError(null); try { - const users = await API.getUsersByUsername(username); - const u = users && users.length > 0 ? users[0] : null; - if (!u) { - setError('用户不存在'); + const u = await API.getUserProfile(username); + if (u.error || !u.id) { + setError(u.error || '用户不存在'); return; } setUser(u); diff --git a/lib/api.js b/lib/api.js index 686cbfd..9ab2c34 100644 --- a/lib/api.js +++ b/lib/api.js @@ -75,6 +75,7 @@ export const API = { // 用户管理 getUsers() { return apiFetch('/users'); }, getUsersByUsername(username) { return apiFetch('/users?username=' + encodeURIComponent(username)); }, + getUserProfile(username) { return apiFetch('/users/profile/' + encodeURIComponent(username)); }, updateUserRole(userId, role) { return apiFetch('/users/' + userId + '/role', { method: 'PUT', body: JSON.stringify({ role }) }); },