'use client' import { useState } from 'react' import { useDocumentInfo } from '@payloadcms/ui' /** * 产品编辑页 — 淘宝信息同步按钮(仅填充空字段 / 强制覆盖) * API: POST /api/admin/taobao/sync-product * * 使用 useDocumentInfo,只能在 Product / PreorderProduct 编辑页使用。 */ export function TaobaoProductSync() { const { id, collectionSlug } = useDocumentInfo() const [loadingNormal, setLoadingNormal] = useState(false) const [loadingForce, setLoadingForce] = useState(false) const [message, setMessage] = useState(null) if (!id) return null const isValid = collectionSlug === 'products' || collectionSlug === 'preorder-products' if (!isValid) return null const busy = loadingNormal || loadingForce const run = async (force: boolean) => { const setLoading = force ? setLoadingForce : setLoadingNormal setLoading(true) setMessage(null) try { const res = await fetch('/api/admin/taobao/sync-product', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ productId: id, collection: collectionSlug, force }), }) const data = await res.json() if (!data.success) throw new Error(data.error || '请求失败') setMessage(`✅ ${data.message || '完成'}`) setTimeout(() => window.location.reload(), 1200) } catch (err: any) { setMessage(`❌ ${err?.message ?? '未知错误'}`) } finally { setLoading(false) } } return (
淘宝自动解析
🔄 更新:仅填充空白字段(标题、封面、价格)  ⚡ 强制更新:覆盖已有字段
{message && }
) } const btnStyle = (busy: boolean, color: string): React.CSSProperties => ({ padding: '0.4rem 0.9rem', background: busy ? '#9ca3af' : color, color: '#fff', border: 'none', borderRadius: '4px', cursor: busy ? 'not-allowed' : 'pointer', fontSize: '0.8rem', fontWeight: 500, whiteSpace: 'nowrap', }) function StatusMsg({ text }: { text: string }) { const isError = text.startsWith('❌') return (
{text}
) }