diff --git a/frontend/src/app/admin/tools/[id]/client.tsx b/frontend/src/app/admin/tools/[id]/client.tsx index 440f9ce..2811b74 100755 --- a/frontend/src/app/admin/tools/[id]/client.tsx +++ b/frontend/src/app/admin/tools/[id]/client.tsx @@ -12,28 +12,38 @@ import { API_BASE } from '@/lib/config'; export default function EditToolPage() { const params = useParams(); const router = useRouter(); - const [form, setForm] = useState({ name: '', description: '', url: '', icon: '', tags: '' }); + const [form, setForm] = useState({ name: '', description: '', url: '', icon: '', affiliateLink: '', tags: '' }); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); + const [error, setError] = useState(''); useEffect(() => { loadTool(); }, [params.id]); - const base = API_BASE; function token() { return localStorage.getItem('adminToken'); } - function headers() { + function authHeaders(): Record { const t = token(); return { 'Content-Type': 'application/json', ...(t ? { Authorization: `Bearer ${t}` } : {}) }; } async function loadTool() { try { - const res = await fetch(`${base}/tools`, { headers: headers() }); + const res = await fetch(`${API_BASE}/admin/tools/${params.id}`, { headers: authHeaders() }); if (res.ok) { - const data = await res.json(); - const tool = (data.items || []).find((t: any) => t.id === Number(params.id)); - if (tool) setForm({ name: tool.name || '', description: tool.description || '', url: tool.url || '', icon: tool.icon || '', tags: tool.tags || '' }); + const tool = await res.json(); + setForm({ + name: tool.name || '', + description: tool.description || '', + url: tool.url || '', + icon: tool.icon || '', + affiliateLink: tool.affiliateLink || '', + tags: tool.tags || '', + }); + } else { + setError('加载工具信息失败'); } - } catch {} + } catch { + setError('网络错误'); + } setLoading(false); } @@ -41,7 +51,24 @@ export default function EditToolPage() { e.preventDefault(); if (!form.name.trim() || !form.url.trim()) return; setSubmitting(true); - router.push('/admin/tools'); + setError(''); + try { + const res = await fetch(`${API_BASE}/admin/tools/${params.id}`, { + method: 'PUT', + headers: authHeaders(), + body: JSON.stringify(form), + }); + if (res.ok) { + router.push('/admin/tools'); + } else { + const data = await res.json().catch(() => ({})); + setError(data.message || '保存失败'); + setSubmitting(false); + } + } catch { + setError('网络错误'); + setSubmitting(false); + } } if (loading) return ( @@ -58,12 +85,48 @@ export default function EditToolPage() {
-

工具信息展示

-
-

{form.name}

-

{form.description || '-'}

-

{form.url}

-
+
+ {error && ( +
+ {error} +
+ )} +
+ + setForm(f => ({ ...f, name: e.target.value }))} placeholder="工具名称" required /> +
+
+ +