Update App.jsx via upload script - 2026-10-31 00:13:50

This commit is contained in:
mike 2026-10-31 00:14:00 +08:00
parent c09613e43e
commit 8e122d3374
1 changed files with 72 additions and 19 deletions

91
App.jsx
View File

@ -7072,7 +7072,7 @@ function DetailTemplatePicker({ draft, set, templates, setTemplates, products, o
const imgs = [...(t.mainImages || []), ...(t.images || [])];
return (
<button type="button" key={t.id} onClick={() => toggle(t.id)} className={`tpl-chip${on ? ' on' : ''}`}>
{imgs[0] ? <img src={`/uploads/${t.id}/${imgs[0]}`} alt="" /> : <span className="tpl-chip-noimg">{String(t.html || '').trim() ? <FileText size={14} /> : <ImageIcon size={14} />}</span>}
{imgs[0] ? <img src={`/uploads/${t.id}/${imgs[0]}`} alt="" /> : <span className="tpl-chip-noimg">{tplHtmlsFilled(t).length ? <FileText size={14} /> : <ImageIcon size={14} />}</span>}
<span>{on ? '✓ ' : '+ '}{t.name || '未命名模板'}<em>{tplSummary(t)}</em></span>
</button>
);
@ -7138,7 +7138,7 @@ function DetailTemplateManager({ templates, setTemplates, products, setProducts,
const mainImages = await copyFiles(t.mainImages);
// 详情图片里的 HTML 片段按"排在第几张图后面"定位,图片顺序不变,直接复制
const detailHtml = (t.detailHtml || []).map(b => ({ ...b, id: 'dh_' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6) }));
const copy = { id, name: `${t.name || '模板'} 副本`, kind: t.kind || 'images', images, mainImages, detailHtml, html: t.html || '' };
const copy = { id, name: `${t.name || '模板'} 副本`, kind: t.kind || 'images', images, mainImages, detailHtml, html: '', htmls: tplHtmlsOf(t).map(h => ({ ...h })) };
setTemplates(list => {
const i = list.findIndex(x => x.id === t.id);
const next = [...list];
@ -7177,7 +7177,7 @@ function DetailTemplateManager({ templates, setTemplates, products, setProducts,
<span className="field-label" style={{ margin: 0 }}>模板内容:</span>
<button type="button" className={`btn btn-sm${active.kind === 'main' ? ' btn-primary' : ''}`} onClick={() => update(active.id, { kind: 'main' })}>主图({(active.mainImages || []).length})</button>
<button type="button" className={`btn btn-sm${(active.kind || 'images') === 'images' ? ' btn-primary' : ''}`} onClick={() => update(active.id, { kind: 'images' })}>详情图片({(active.images || []).length}{(active.detailHtml || []).length ? ` + HTML ${(active.detailHtml || []).length}` : ''})</button>
<button type="button" className={`btn btn-sm${active.kind === 'html' ? ' btn-primary' : ''}`} onClick={() => update(active.id, { kind: 'html' })}>HTML 内容{String(active.html || '').trim() ? '(已填)' : ''}</button>
<button type="button" className={`btn btn-sm${active.kind === 'html' ? ' btn-primary' : ''}`} onClick={() => update(active.id, { kind: 'html' })}>HTML 内容{tplHtmlsFilled(active).length ? `(${tplHtmlsFilled(active).length})` : ''}</button>
</div>
<div style={{ fontSize: 12.5, color: 'var(--ink-soft)', margin: '-6px 0 10px' }}>主图、详情图片、HTML 分开上传、分开保存,互不影响;一个模板可以只填其中一项,也可以三项都填(比如一个系列一个模板)。</div>
{active.kind === 'html' ? (
@ -7290,7 +7290,7 @@ function TemplateProductMatcher({ tpl, products, setProducts, onNotify }) {
const coll = new Intl.Collator('zh', { numeric: true });
const names = Object.keys(groups).sort(coll.compare);
// HTML 模板:按表格里的型号自动匹配
const tableKeys = String(tpl.html || '').trim() ? analyzeTplHtml(tpl.html || '').models.map(tplSeriesKey).filter(Boolean) : [];
const tableKeys = tplHtmlsFilled(tpl).flatMap(h => analyzeTplHtml(h.html || '').models.map(tplSeriesKey)).filter(Boolean);
const tableHits = tableKeys.length ? products.filter(p => tableKeys.includes(tplSeriesKey(label(p)))) : [];
return (
<div className="field" style={{ border: '1px solid var(--line)', borderRadius: 10, padding: 12, marginTop: 6 }}>
@ -7403,6 +7403,7 @@ function tplModelEffective(tpl, pid) {
const mains = Array.isArray(tpl.mainImages) ? tpl.mainImages : [];
const imgs = Array.isArray(tpl.images) ? tpl.images : [];
const blocks = (Array.isArray(tpl.detailHtml) ? tpl.detailHtml : []).filter(b => String(b.html || '').trim()).map(b => b.id);
const hids = tplHtmlsFilled(tpl).map(h => h.id);
const pk = (tpl.modelPick || {})[pid] || {};
const ex = (tpl.modelExclude || {})[pid] || {};
const cover = (tpl.mainCover || {})[pid];
@ -7410,16 +7411,17 @@ function tplModelEffective(tpl, pid) {
main: Array.isArray(pk.main) ? pk.main.filter(f => mains.includes(f)) : (cover && mains.includes(cover) ? [cover, ...mains.filter(f => f !== cover)] : null),
images: Array.isArray(pk.images) ? pk.images.filter(f => imgs.includes(f)) : (Array.isArray(ex.images) && ex.images.length ? imgs.filter(f => !ex.images.includes(f)) : null),
blocks: Array.isArray(pk.blocks) ? pk.blocks.filter(id => blocks.includes(id)) : (Array.isArray(ex.blocks) && ex.blocks.length ? blocks.filter(id => !ex.blocks.includes(id)) : null),
html: typeof pk.html === 'boolean' ? pk.html : (ex.html ? false : null),
// HTML 内容:htmls = 选中的那几段;旧数据 html:true/false 表示全部 / 都不用
htmls: Array.isArray(pk.htmls) ? pk.htmls.filter(id => hids.includes(id)) : pk.html === true ? hids : (pk.html === false || ex.html) ? [] : null,
};
}
function TemplateModelPick({ tpl, products, update }) {
const mains = Array.isArray(tpl.mainImages) ? tpl.mainImages : [];
const imgs = Array.isArray(tpl.images) ? tpl.images : [];
const blocks = (Array.isArray(tpl.detailHtml) ? tpl.detailHtml : []).filter(b => String(b.html || '').trim());
const hasHtml = !!String(tpl.html || '').trim();
const htmls = tplHtmlsFilled(tpl);
const users = (products || []).filter(p => Array.isArray(p.webDetailTemplateIds) && p.webDetailTemplateIds.includes(tpl.id));
const parts = mains.length + imgs.length + blocks.length + (hasHtml ? 1 : 0);
const parts = mains.length + imgs.length + blocks.length + htmls.length;
if (users.length < 1 || parts < 1) return null;
const coll = new Intl.Collator('zh', { numeric: true });
const label = (p) => p.name || p.model || '未命名';
@ -7429,7 +7431,7 @@ function TemplateModelPick({ tpl, products, update }) {
if (eff.main) pick.main = eff.main;
if (eff.images) pick.images = eff.images;
if (eff.blocks) pick.blocks = eff.blocks;
if (typeof eff.html === 'boolean') pick.html = eff.html;
if (eff.htmls) pick.htmls = eff.htmls;
const mp = { ...(tpl.modelPick || {}) };
if (Object.keys(pick).length) mp[pid] = pick; else delete mp[pid];
const mc = { ...(tpl.mainCover || {}) }; delete mc[pid];
@ -7445,7 +7447,7 @@ function TemplateModelPick({ tpl, products, update }) {
// 主图按点击顺序;详情图片 / 片段按模板顺序
setCat(pid, key, key === 'main' ? next : all.filter(x => next.includes(x)));
};
const custom = users.filter(p => { const e = tplModelEffective(tpl, p.id); return e.main || e.images || e.blocks || typeof e.html === 'boolean'; }).length;
const custom = users.filter(p => { const e = tplModelEffective(tpl, p.id); return e.main || e.images || e.blocks || e.htmls; }).length;
const defBtn = (on, onClick) => <button type="button" className={`btn btn-sm${on ? ' btn-primary' : ''}`} style={{ padding: '3px 9px' }} onClick={onClick} title="默认:全部都用,按模板顺序">默认</button>;
const rowL = { fontSize: 12, color: 'var(--ink-soft)', width: 64, flexShrink: 0 };
const thumb = (on, isDef) => ({ padding: 0, borderRadius: 5, overflow: 'hidden', cursor: 'pointer', background: '#fff', position: 'relative',
@ -7461,7 +7463,7 @@ function TemplateModelPick({ tpl, products, update }) {
<div style={{ display: 'grid', gap: 8, maxHeight: 520, overflowY: 'auto', paddingRight: 4 }}>
{[...users].sort((a, b) => coll.compare(label(a), label(b))).map(p => {
const e = tplModelEffective(tpl, p.id);
const isCustom = e.main || e.images || e.blocks || typeof e.html === 'boolean';
const isCustom = e.main || e.images || e.blocks || e.htmls;
return (
<div key={p.id} style={{ borderTop: '1px dashed var(--line)', paddingTop: 8, display: 'grid', gap: 6 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
@ -7513,15 +7515,16 @@ function TemplateModelPick({ tpl, products, update }) {
})}
</div>
)}
{hasHtml && (
{htmls.length > 0 && (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}>
<span style={rowL}>HTML 内容</span>
{defBtn(typeof e.html !== 'boolean', () => setCat(p.id, 'html', null))}
{(() => {
const on = e.html !== false, isDef = typeof e.html !== 'boolean';
return <button type="button" className="btn btn-sm" onClick={() => setCat(p.id, 'html', isDef ? true : !e.html)}
style={{ padding: '3px 9px', opacity: on ? 1 : 0.4, borderColor: on && !isDef ? 'var(--copper, #b4651e)' : undefined, color: on && !isDef ? 'var(--copper, #b4651e)' : undefined, borderStyle: on ? 'solid' : 'dashed' }}>{on ? '✓ ' : ''}参数表 / HTML 内容</button>;
})()}
{defBtn(!e.htmls, () => setCat(p.id, 'htmls', null))}
{htmls.map((h, i) => {
const on = !e.htmls || e.htmls.includes(h.id);
return <button key={h.id} type="button" className="btn btn-sm" onClick={() => clickItem(p.id, 'htmls', h.id, htmls.map(x => x.id))}
style={{ padding: '3px 9px', opacity: on ? 1 : 0.4, borderColor: on && e.htmls ? 'var(--copper, #b4651e)' : undefined, color: on && e.htmls ? 'var(--copper, #b4651e)' : undefined, borderStyle: on ? 'solid' : 'dashed' }}>{on ? '✓ ' : ''}{h.name || `HTML ${i + 1}`}</button>;
})}
{e.htmls && !e.htmls.length && <span style={{ fontSize: 11.5, color: 'var(--led-red)' }}>不显示 HTML 内容</span>}
</div>
)}
</div>
@ -7532,13 +7535,20 @@ function TemplateModelPick({ tpl, products, update }) {
);
}
// 模板的 HTML 内容可以有多段:htmls = [{ id, name, html }]。旧数据只有一段 html 字符串,换算成 id 为 h_main 的一段
function tplHtmlsOf(t) {
if (t && Array.isArray(t.htmls)) return t.htmls;
return t && String(t.html || '').trim() ? [{ id: 'h_main', name: 'HTML 1', html: t.html }] : [];
}
const tplHtmlsFilled = (t) => tplHtmlsOf(t).filter(h => String(h.html || '').trim());
// 模板内容摘要:主图2 · 详情5 · HTML
function tplSummary(t) {
const parts = [];
if ((t.mainImages || []).length) parts.push(`主图${t.mainImages.length}`);
if ((t.images || []).length) parts.push(`详情${t.images.length}`);
if ((t.detailHtml || []).length) parts.push(`详情HTML${t.detailHtml.length}`);
if (String(t.html || '').trim()) parts.push('HTML');
if (tplHtmlsFilled(t).length) parts.push(tplHtmlsFilled(t).length > 1 ? `HTML${tplHtmlsFilled(t).length}` : 'HTML');
return parts.length ? parts.join(' · ') : '空';
}
@ -7567,7 +7577,50 @@ function analyzeTplHtml(html) {
return r;
}
function HtmlTemplateEditor({ tpl, update, products, onNotify }) {
const html = tpl.html || '';
const list = tplHtmlsOf(tpl);
const [openId, setOpenId] = useState(list[0] ? list[0].id : '');
// 改动统一写进 htmls,旧的单段 html 字段清空
const save = (next) => update({ htmls: next, html: '' });
const patch = (id, p) => save(list.map(h => (h.id === id ? { ...h, ...p } : h)));
const add = () => { const h = { id: 'h_' + Date.now().toString(36) + Math.random().toString(36).slice(2, 5), name: `HTML ${list.length + 1}`, html: '' }; save([...list, h]); setOpenId(h.id); };
const remove = (h) => { if (String(h.html || '').trim() && !window.confirm(`删除"${h.name || 'HTML'}"?`)) return; save(list.filter(x => x.id !== h.id)); };
const move = (i, d) => { const j = i + d; if (j < 0 || j >= list.length) return; const next = [...list]; [next[i], next[j]] = [next[j], next[i]]; save(next); };
return (
<div>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8, flexWrap: 'wrap', marginBottom: 8 }}>
<span className="field-label" style={{ margin: 0 }}>HTML 内容({list.length} 段;网站上按这个顺序显示在详情图下方,每个型号可以在下面单独选用哪几段)</span>
<button type="button" className="btn btn-sm" onClick={add}><Plus size={12} />添加 HTML</button>
</div>
{list.length === 0 && <div style={{ fontSize: 12.5, color: 'var(--ink-soft)', padding: '6px 0 12px' }}>还没有 HTML 内容,点"添加 HTML"粘贴代码或上传 .html 文件(比如系列参数表)。</div>}
{list.map((h, i) => {
const isOpen = openId === h.id, len = String(h.html || '').trim().length;
return (
<div key={h.id} style={{ border: '1px solid var(--line)', borderRadius: 10, marginBottom: 8, background: '#fff' }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '8px 10px', flexWrap: 'wrap' }}>
<b style={{ fontSize: 12.5, color: 'var(--copper, #b4651e)' }}>#{i + 1}</b>
<input className="input" style={{ flex: '1 1 200px', padding: '5px 8px' }} value={h.name || ''} placeholder="名称(只在后台显示,如:两模块机柜参数表)" onChange={e => patch(h.id, { name: e.target.value })} />
<span style={{ fontSize: 12, color: 'var(--ink-soft)' }}>{len ? `${len.toLocaleString()} 字符` : '空'}</span>
<button type="button" className="btn btn-sm btn-ghost" disabled={i === 0} onClick={() => move(i, -1)} title="上移">↑</button>
<button type="button" className="btn btn-sm btn-ghost" disabled={i === list.length - 1} onClick={() => move(i, 1)} title="下移">↓</button>
<button type="button" className="btn btn-sm" onClick={() => setOpenId(isOpen ? '' : h.id)}>{isOpen ? '收起' : '编辑'}</button>
<button type="button" className="btn btn-sm btn-ghost btn-danger" onClick={() => remove(h)}><Trash2 size={12} /></button>
</div>
{isOpen && (
<div style={{ padding: '0 10px 4px' }}>
<HtmlOneEditor key={h.id} tplId={tpl.id} html={h.html || ''} setHtml={(v) => patch(h.id, { html: v })} products={products} onNotify={onNotify} />
</div>
)}
</div>
);
})}
</div>
);
}
// 单段 HTML 内容的编辑器(粘贴 / 上传 / 预览 / 发布前检查)
function HtmlOneEditor({ tplId, html, setHtml, products, onNotify }) {
const tpl = { id: tplId };
const update = (p) => setHtml(p.html);
const [preview, setPreview] = useState(false);
const fileRef = useRef(null);
const info = useMemo(() => analyzeTplHtml(html), [html]);