From 15ff424f11b1f96648092ad64a68cc50cb44895f Mon Sep 17 00:00:00 2001 From: TradeMate Dev Date: Tue, 23 Jun 2026 21:23:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Admin=20=E5=B7=A5=E5=85=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=A1=B5=E6=96=B0=E5=A2=9E=E8=BF=94=E4=BD=A3=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - new/page.tsx: 表单添加 affiliateLink 输入字段 + 提示文案 - [id]/client.tsx: 从只读展示重写为可编辑表单,调用 PUT /admin/tools/:id 保存 添加 affiliateLink 字段,修复原页面不保存的 Bug Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- frontend/src/app/admin/tools/[id]/client.tsx | 93 ++++++++++++++++---- frontend/src/app/admin/tools/new/page.tsx | 9 +- 2 files changed, 86 insertions(+), 16 deletions(-) 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 /> +
+
+ +