'use client' import React, { useState, useEffect } from 'react' import { useDocumentInfo } from '@payloadcms/ui' /** * 产品编辑页 — 淘宝信息同步按钮 * * 放置在淘宝链接 Tab 顶部(UI 字段),显示两个操作按钮: * 🔄 更新淘宝信息 → 仅填充空字段 (force=false) * ⚡ 强制更新淘宝信息 → 覆盖所有字段 (force=true) * * 依赖 API:POST /api/admin/taobao/sync-product */ export const TaobaoSyncButtons: React.FC = () => { const { id, collectionSlug } = useDocumentInfo() const [loadingNormal, setLoadingNormal] = useState(false) const [loadingForce, setLoadingForce] = useState(false) const [message, setMessage] = useState(null) if (!id) return null // 新建文档时不显示 const isValidCollection = collectionSlug === 'products' || collectionSlug === 'preorder-products' if (!isValidCollection) return null 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) } } const busy = loadingNormal || loadingForce return (
淘宝自动解析
{/* 更新(非强制) */} {/* 强制全量更新 */}
{/* 说明文字 */}
🔄 更新:仅填充空白字段(标题、封面、价格)  ⚡ 强制更新:覆盖已有字段
{message && (
{message}
)}
) }