diff --git a/App.jsx b/App.jsx index 65bbc08..cc13b62 100644 --- a/App.jsx +++ b/App.jsx @@ -3229,6 +3229,48 @@ function MainApp({ username, onLogout }) { notify('已保存'); } } + // 复制产品:全部参数 + 图片 / 彩页文件(复制到新产品自己的目录,之后改新产品不影响原产品), + // 型号名后面加"副本",直接打开编辑表单改型号;默认不发布到网站,确认无误后再勾选 + const [productCopying, setProductCopying] = useState(false); + async function duplicateProduct(p) { + if (productCopying || !p) return; + setProductCopying(true); + try { + const copy = { ...blankProduct(), ...JSON.parse(JSON.stringify(p)) }; + copy.id = blankProduct().id; + let failed = 0; + const done = new Map(); // 同一个文件只复制一次(主图常常也在网站主图里) + const copyOne = async (f) => { + if (!f) return f; + if (done.has(f)) return done.get(f); + try { + const r = await fetch(`/uploads/${encodeURIComponent(p.id)}/${encodeURIComponent(f)}`); + if (!r.ok) throw new Error('HTTP ' + r.status); + const blob = await r.blob(); + const up = await uploadFile(copy.id, new File([blob], f, { type: blob.type || 'application/octet-stream' })); + done.set(f, up.fileName); + return up.fileName; + } catch (e) { failed++; done.set(f, null); return null; } + }; + copy.productImageFileName = (await copyOne(p.productImageFileName)) || ''; + if (!copy.productImageFileName) copy.productImageOriginalName = ''; + copy.brochureFileName = (await copyOne(p.brochureFileName)) || ''; + if (!copy.brochureFileName) copy.brochureOriginalName = ''; + const br = {}; + for (const [lang, b] of Object.entries(p.brochures || {})) { const f = b && (await copyOne(b.fileName)); if (f) br[lang] = { ...b, fileName: f }; } + copy.brochures = br; + copy.webImages = []; for (const f of p.webImages || []) { const n = await copyOne(f); if (n) copy.webImages.push(n); } + copy.webDetailImages = []; for (const f of p.webDetailImages || []) { const n = await copyOne(f); if (n) copy.webDetailImages.push(n); } + // 型号不能和现有产品重名(同名保存会覆盖原产品),自动加"副本 / 副本2…" + let name = `${p.name} 副本`, k = 2; + while (products.some(x => sameModel(x, { ...copy, name }))) name = `${p.name} 副本${k++}`; + copy.name = name; + copy.publishToWeb = false; + setProductDraft({ ...copy, extraNames: [] }); + setSelectedProductId(null); + notify(failed ? `已复制,但有 ${failed} 个图片/文件复制失败,请在新产品里补传。改好型号后点保存` : '已复制,请修改型号等参数后点保存(默认不发布到网站)'); + } finally { setProductCopying(false); } + } function deleteProduct(id) { if (!window.confirm('确定删除该产品档案?此操作不可恢复。')) return; setProducts(prev => prev.filter(p => p.id !== id)); @@ -4233,6 +4275,7 @@ function MainApp({ username, onLogout }) { ) : selectedProduct ? ( startEditProduct(selectedProduct)} onDelete={() => deleteProduct(selectedProduct.id)} + onCopy={() => duplicateProduct(selectedProduct)} copying={productCopying} categories={productCategories} exportLang={productExportLang} onExportLangChange={setProductExportLang} exportLabels={productExportLabels} exportLoading={productExportLoading} @@ -9635,7 +9678,7 @@ function BrochureBulkUpload({ products, setProducts, onNotify }) { ); } -function ProductDetail({ product: p, onEdit, onDelete, categories, exportLang, onExportLangChange, exportLabels, exportLoading, onExportPdf, exportPdfBusy, backPanelTemplates, onPatch, onNotify }) { +function ProductDetail({ product: p, onEdit, onDelete, onCopy, copying, categories, exportLang, onExportLangChange, exportLabels, exportLoading, onExportPdf, exportPdfBusy, backPanelTemplates, onPatch, onNotify }) { const cat = (categories || []).find(c => c.id === p.category); const showMachineVariant = isUpsCategory(cat) && p.machineVariant; const L = (key) => (exportLabels && exportLabels[key]) || key; @@ -9651,6 +9694,7 @@ function ProductDetail({ product: p, onEdit, onDelete, categories, exportLang, o
{p.publishToWeb && } + {onCopy && }