Update App.jsx via upload script - 2026-11-11 15:15:13
This commit is contained in:
parent
292b17f907
commit
6c71a90f2d
46
App.jsx
46
App.jsx
|
|
@ -3229,6 +3229,48 @@ function MainApp({ username, onLogout }) {
|
||||||
notify('已保存');
|
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) {
|
function deleteProduct(id) {
|
||||||
if (!window.confirm('确定删除该产品档案?此操作不可恢复。')) return;
|
if (!window.confirm('确定删除该产品档案?此操作不可恢复。')) return;
|
||||||
setProducts(prev => prev.filter(p => p.id !== id));
|
setProducts(prev => prev.filter(p => p.id !== id));
|
||||||
|
|
@ -4233,6 +4275,7 @@ function MainApp({ username, onLogout }) {
|
||||||
) : selectedProduct ? (
|
) : selectedProduct ? (
|
||||||
<ProductDetail
|
<ProductDetail
|
||||||
product={selectedProduct} onEdit={() => startEditProduct(selectedProduct)} onDelete={() => deleteProduct(selectedProduct.id)}
|
product={selectedProduct} onEdit={() => startEditProduct(selectedProduct)} onDelete={() => deleteProduct(selectedProduct.id)}
|
||||||
|
onCopy={() => duplicateProduct(selectedProduct)} copying={productCopying}
|
||||||
categories={productCategories}
|
categories={productCategories}
|
||||||
exportLang={productExportLang} onExportLangChange={setProductExportLang}
|
exportLang={productExportLang} onExportLangChange={setProductExportLang}
|
||||||
exportLabels={productExportLabels} exportLoading={productExportLoading}
|
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 cat = (categories || []).find(c => c.id === p.category);
|
||||||
const showMachineVariant = isUpsCategory(cat) && p.machineVariant;
|
const showMachineVariant = isUpsCategory(cat) && p.machineVariant;
|
||||||
const L = (key) => (exportLabels && exportLabels[key]) || key;
|
const L = (key) => (exportLabels && exportLabels[key]) || key;
|
||||||
|
|
@ -9651,6 +9694,7 @@ function ProductDetail({ product: p, onEdit, onDelete, categories, exportLang, o
|
||||||
<div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', justifyContent: 'flex-end' }}>
|
<div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', justifyContent: 'flex-end' }}>
|
||||||
{p.publishToWeb && <SiteDeployButton />}
|
{p.publishToWeb && <SiteDeployButton />}
|
||||||
<button className="btn btn-sm" onClick={onEdit}><Edit2 size={12} />编辑</button>
|
<button className="btn btn-sm" onClick={onEdit}><Edit2 size={12} />编辑</button>
|
||||||
|
{onCopy && <button className="btn btn-sm" onClick={onCopy} disabled={copying} title="复制成一个新产品(参数和图片一起复制)"><Copy size={12} />{copying ? '复制中…' : '复制'}</button>}
|
||||||
<button className="btn btn-sm btn-ghost btn-danger" onClick={onDelete}><Trash2 size={12} /></button>
|
<button className="btn btn-sm btn-ghost btn-danger" onClick={onDelete}><Trash2 size={12} /></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue