feat: Admin 工具管理页新增返佣链接字段
- 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 <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -12,28 +12,38 @@ import { API_BASE } from '@/lib/config';
|
|||||||
export default function EditToolPage() {
|
export default function EditToolPage() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const router = useRouter();
|
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 [loading, setLoading] = useState(true);
|
||||||
const [submitting, setSubmitting] = useState(false);
|
const [submitting, setSubmitting] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
useEffect(() => { loadTool(); }, [params.id]);
|
useEffect(() => { loadTool(); }, [params.id]);
|
||||||
|
|
||||||
const base = API_BASE;
|
|
||||||
function token() { return localStorage.getItem('adminToken'); }
|
function token() { return localStorage.getItem('adminToken'); }
|
||||||
function headers() {
|
function authHeaders(): Record<string, string> {
|
||||||
const t = token();
|
const t = token();
|
||||||
return { 'Content-Type': 'application/json', ...(t ? { Authorization: `Bearer ${t}` } : {}) };
|
return { 'Content-Type': 'application/json', ...(t ? { Authorization: `Bearer ${t}` } : {}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadTool() {
|
async function loadTool() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${base}/tools`, { headers: headers() });
|
const res = await fetch(`${API_BASE}/admin/tools/${params.id}`, { headers: authHeaders() });
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data = await res.json();
|
const tool = await res.json();
|
||||||
const tool = (data.items || []).find((t: any) => t.id === Number(params.id));
|
setForm({
|
||||||
if (tool) setForm({ name: tool.name || '', description: tool.description || '', url: tool.url || '', icon: tool.icon || '', tags: tool.tags || '' });
|
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);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +51,24 @@ export default function EditToolPage() {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!form.name.trim() || !form.url.trim()) return;
|
if (!form.name.trim() || !form.url.trim()) return;
|
||||||
setSubmitting(true);
|
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 (
|
if (loading) return (
|
||||||
@@ -58,12 +85,48 @@ export default function EditToolPage() {
|
|||||||
</div>
|
</div>
|
||||||
<div className="max-w-2xl mx-auto p-6">
|
<div className="max-w-2xl mx-auto p-6">
|
||||||
<Card className="p-6">
|
<Card className="p-6">
|
||||||
<p className="text-sm text-muted-foreground mb-4">工具信息展示</p>
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
<div className="space-y-3">
|
{error && (
|
||||||
<div><label className="text-sm font-medium text-foreground">名称</label><p className="text-sm text-muted-foreground">{form.name}</p></div>
|
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-700 dark:text-red-400">
|
||||||
<div><label className="text-sm font-medium text-foreground">描述</label><p className="text-sm text-muted-foreground">{form.description || '-'}</p></div>
|
{error}
|
||||||
<div><label className="text-sm font-medium text-foreground">链接</label><p className="text-sm text-muted-foreground">{form.url}</p></div>
|
</div>
|
||||||
</div>
|
)}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">名称</label>
|
||||||
|
<Input value={form.name} onChange={e => setForm(f => ({ ...f, name: e.target.value }))} placeholder="工具名称" required />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">描述</label>
|
||||||
|
<textarea value={form.description} onChange={e => setForm(f => ({ ...f, description: e.target.value }))}
|
||||||
|
rows={3} className="w-full px-3 py-2 bg-background border border-input rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-ring"
|
||||||
|
placeholder="工具简介" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">链接</label>
|
||||||
|
<Input value={form.url} onChange={e => setForm(f => ({ ...f, url: e.target.value }))} placeholder="https://..." required />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">图标链接</label>
|
||||||
|
<Input value={form.icon} onChange={e => setForm(f => ({ ...f, icon: e.target.value }))} placeholder="https://..." />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">
|
||||||
|
返佣链接 <span className="text-muted-foreground font-normal">(可选)</span>
|
||||||
|
</label>
|
||||||
|
<Input value={form.affiliateLink} onChange={e => setForm(f => ({ ...f, affiliateLink: e.target.value }))} placeholder="https://..." />
|
||||||
|
<p className="text-xs text-muted-foreground mt-1">通过此链接跳转可获得返佣收入</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">标签</label>
|
||||||
|
<Input value={form.tags} onChange={e => setForm(f => ({ ...f, tags: e.target.value }))} placeholder="AI,工具,效率" />
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2 pt-2">
|
||||||
|
<Button type="submit" disabled={submitting || !form.name.trim() || !form.url.trim()}>
|
||||||
|
{submitting ? '保存中...' : '保存'}
|
||||||
|
</Button>
|
||||||
|
<Link href="/admin/tools"><Button type="button" variant="outline">取消</Button></Link>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { API_BASE } from '@/lib/config';
|
|||||||
|
|
||||||
export default function NewToolPage() {
|
export default function NewToolPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [form, setForm] = useState({ name: '', description: '', url: '', icon: '', tags: '' });
|
const [form, setForm] = useState({ name: '', description: '', url: '', icon: '', affiliateLink: '', tags: '' });
|
||||||
const [submitting, setSubmitting] = useState(false);
|
const [submitting, setSubmitting] = useState(false);
|
||||||
|
|
||||||
async function handleSubmit(e: React.FormEvent) {
|
async function handleSubmit(e: React.FormEvent) {
|
||||||
@@ -55,6 +55,13 @@ export default function NewToolPage() {
|
|||||||
<label className="block text-sm font-medium text-foreground mb-1">图标链接</label>
|
<label className="block text-sm font-medium text-foreground mb-1">图标链接</label>
|
||||||
<Input value={form.icon} onChange={e => setForm(f => ({ ...f, icon: e.target.value }))} placeholder="https://..." />
|
<Input value={form.icon} onChange={e => setForm(f => ({ ...f, icon: e.target.value }))} placeholder="https://..." />
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-foreground mb-1">
|
||||||
|
返佣链接 <span className="text-muted-foreground font-normal">(可选)</span>
|
||||||
|
</label>
|
||||||
|
<Input value={form.affiliateLink} onChange={e => setForm(f => ({ ...f, affiliateLink: e.target.value }))} placeholder="https://..." />
|
||||||
|
<p className="text-xs text-muted-foreground mt-1">通过此链接跳转可获得返佣收入</p>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-foreground mb-1">标签</label>
|
<label className="block text-sm font-medium text-foreground mb-1">标签</label>
|
||||||
<Input value={form.tags} onChange={e => setForm(f => ({ ...f, tags: e.target.value }))} placeholder="AI,工具,效率" />
|
<Input value={form.tags} onChange={e => setForm(f => ({ ...f, tags: e.target.value }))} placeholder="AI,工具,效率" />
|
||||||
|
|||||||
Reference in New Issue
Block a user