Update App.jsx via upload script - 2026-11-09 16:45:23

This commit is contained in:
mike 2026-11-09 16:45:37 +08:00
parent 1528d5643e
commit 5d2e8f7b61
1 changed files with 70 additions and 17 deletions

87
App.jsx
View File

@ -7348,7 +7348,7 @@ function DetailTemplateManager({ templates, setTemplates, products, setProducts,
)} )}
{(active.kind || 'images') === 'images' && <DetailHtmlBlocks tpl={active} update={(patch) => update(active.id, patch)} />} {(active.kind || 'images') === 'images' && <DetailHtmlBlocks tpl={active} update={(patch) => update(active.id, patch)} />}
{setProducts && <TemplateProductMatcher tpl={active} products={products} setProducts={setProducts} onNotify={onNotify} />} {setProducts && <TemplateProductMatcher tpl={active} products={products} setProducts={setProducts} onNotify={onNotify} />}
<TemplateModelPick tpl={active} products={products} update={(patch) => update(active.id, patch)} /> <TemplateModelPick tpl={active} products={products} update={(patch) => update(active.id, patch)} onNotify={onNotify} />
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', fontSize: 12.5, color: 'var(--ink-soft)' }}> <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', fontSize: 12.5, color: 'var(--ink-soft)' }}>
<span>{usage(active.id) ? `${usage(active.id)} 个产品在用这个模板;修改后约 3 分钟自动同步到网站` : '还没有产品使用这个模板'}</span> <span>{usage(active.id) ? `${usage(active.id)} 个产品在用这个模板;修改后约 3 分钟自动同步到网站` : '还没有产品使用这个模板'}</span>
<span style={{ display: 'flex', gap: 6 }}> <span style={{ display: 'flex', gap: 6 }}>
@ -7734,9 +7734,11 @@ function DetailHtmlBlocks({ tpl, update }) {
// 某一项没有 = "默认":全部都用、按模板顺序(模板以后新加的也自动带上); // 某一项没有 = "默认":全部都用、按模板顺序(模板以后新加的也自动带上);
// 有 = 只用点选的那些(主图按点选顺序排列,第一张是封面;详情图片和片段按模板里的顺序)。 // 有 = 只用点选的那些(主图按点选顺序排列,第一张是封面;详情图片和片段按模板里的顺序)。
// 旧数据 mainCover(单独指定封面)/ modelExclude(排除)在这里换算成同样的显示,改动后统一存进 modelPick。 // 旧数据 mainCover(单独指定封面)/ modelExclude(排除)在这里换算成同样的显示,改动后统一存进 modelPick。
// 型号自己上传的图片:modelFiles = { 产品id: { main: [文件名], images: [文件名] } }(文件存在模板目录下)
const tplOwnFiles = (tpl, pid, key) => { const m = (tpl.modelFiles || {})[pid]; return m && Array.isArray(m[key]) ? m[key] : []; };
function tplModelEffective(tpl, pid) { function tplModelEffective(tpl, pid) {
const mains = Array.isArray(tpl.mainImages) ? tpl.mainImages : []; const mains = [...(Array.isArray(tpl.mainImages) ? tpl.mainImages : []), ...tplOwnFiles(tpl, pid, 'main')];
const imgs = Array.isArray(tpl.images) ? tpl.images : []; const imgs = [...(Array.isArray(tpl.images) ? tpl.images : []), ...tplOwnFiles(tpl, pid, 'images')];
const blocks = (Array.isArray(tpl.detailHtml) ? tpl.detailHtml : []).filter(b => String(b.html || '').trim()).map(b => b.id); 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 hids = tplHtmlsFilled(tpl).map(h => h.id);
const pk = (tpl.modelPick || {})[pid] || {}; const pk = (tpl.modelPick || {})[pid] || {};
@ -7750,14 +7752,14 @@ function tplModelEffective(tpl, pid) {
htmls: Array.isArray(pk.htmls) ? pk.htmls.filter(id => hids.includes(id)) : pk.html === true ? hids : (pk.html === false || ex.html) ? [] : null, 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 }) { function TemplateModelPick({ tpl, products, update, onNotify }) {
const [upBusy, setUpBusy] = useState('');
const mains = Array.isArray(tpl.mainImages) ? tpl.mainImages : []; const mains = Array.isArray(tpl.mainImages) ? tpl.mainImages : [];
const imgs = Array.isArray(tpl.images) ? tpl.images : []; const imgs = Array.isArray(tpl.images) ? tpl.images : [];
const blocks = (Array.isArray(tpl.detailHtml) ? tpl.detailHtml : []).filter(b => String(b.html || '').trim()); const blocks = (Array.isArray(tpl.detailHtml) ? tpl.detailHtml : []).filter(b => String(b.html || '').trim());
const htmls = tplHtmlsFilled(tpl); const htmls = tplHtmlsFilled(tpl);
const users = (products || []).filter(p => Array.isArray(p.webDetailTemplateIds) && p.webDetailTemplateIds.includes(tpl.id)); const users = (products || []).filter(p => Array.isArray(p.webDetailTemplateIds) && p.webDetailTemplateIds.includes(tpl.id));
const parts = mains.length + imgs.length + blocks.length + htmls.length; if (users.length < 1) return null;
if (users.length < 1 || parts < 1) return null;
const coll = new Intl.Collator('zh', { numeric: true }); const coll = new Intl.Collator('zh', { numeric: true });
const label = (p) => p.name || p.model || '未命名'; const label = (p) => p.name || p.model || '未命名';
// 保存某个型号:把当前(含旧数据换算的)设置写进 modelPick,清掉这个型号的旧字段 // 保存某个型号:把当前(含旧数据换算的)设置写进 modelPick,清掉这个型号的旧字段
@ -7783,6 +7785,50 @@ function TemplateModelPick({ tpl, products, update }) {
setCat(pid, key, key === 'main' ? next : all.filter(x => next.includes(x))); 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 || e.htmls; }).length; const custom = users.filter(p => { const e = tplModelEffective(tpl, p.id); return e.main || e.images || e.blocks || e.htmls; }).length;
// 从电脑给某个型号上传自己的图片:上传后这个型号改为手动选择——默认状态下只用上传的图,已经是手动选择时接在后面
async function uploadOwn(pid, key, files) {
files = Array.from(files || []).filter(f => /^image\//.test(f.type));
if (!files.length) return;
setUpBusy(`${pid}:${key}`);
const added = [];
try {
for (const f of files) { const r = await uploadFile(tpl.id, f); added.push(r.fileName); }
} catch (e) { onNotify && onNotify(e.message || '上传失败'); }
setUpBusy('');
if (!added.length) return;
const mf = { ...(tpl.modelFiles || {}) };
mf[pid] = { ...(mf[pid] || {}), [key]: [...tplOwnFiles(tpl, pid, key), ...added] };
const eff = tplModelEffective({ ...tpl, modelFiles: mf }, pid);
const pick = {};
if (eff.main) pick.main = eff.main;
if (eff.images) pick.images = eff.images;
if (eff.blocks) pick.blocks = eff.blocks;
if (eff.htmls) pick.htmls = eff.htmls;
pick[key] = [...(eff[key] || []), ...added];
const mp = { ...(tpl.modelPick || {}), [pid]: pick };
const mc = { ...(tpl.mainCover || {}) }; delete mc[pid];
const me = { ...(tpl.modelExclude || {}) }; delete me[pid];
update({ modelFiles: mf, modelPick: mp, mainCover: mc, modelExclude: me });
}
function removeOwn(pid, key, f) {
if (!window.confirm('删除这张单独上传的图片?')) return;
// 和模板图片一样不删文件:网站同步之前还在用这张图
const mf = { ...(tpl.modelFiles || {}) };
mf[pid] = { ...(mf[pid] || {}), [key]: tplOwnFiles(tpl, pid, key).filter(x => x !== f) };
const mp = { ...(tpl.modelPick || {}) };
if (mp[pid] && Array.isArray(mp[pid][key])) mp[pid] = { ...mp[pid], [key]: mp[pid][key].filter(x => x !== f) };
update({ modelFiles: mf, modelPick: mp });
}
const upBtn = (pid, key, w, h) => (
<label title="从电脑上传这个型号自己的图片" style={{ width: w, height: h, border: '1px dashed var(--line)', borderRadius: 5, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', cursor: upBusy ? 'default' : 'pointer', fontSize: 11, color: 'var(--ink-soft)', background: '#fafafa' }}>
{upBusy === `${pid}:${key}` ? '…' : <Plus size={14} />}
<input type="file" accept="image/*" multiple style={{ display: 'none' }} disabled={!!upBusy} onChange={e => { uploadOwn(pid, key, e.target.files); e.target.value = ''; }} />
</label>
);
const ownMark = (pid, key, f) => (
<span onClick={ev => { ev.stopPropagation(); removeOwn(pid, key, f); }} title="删除这张上传的图片"
style={{ position: 'absolute', right: 1, top: 1, width: 14, height: 14, borderRadius: 7, background: 'rgba(0,0,0,.55)', color: '#fff', fontSize: 10, lineHeight: '14px', textAlign: 'center' }}>×</span>
);
const defBtn = (on, onClick) => <button type="button" className={`btn btn-sm${on ? ' btn-primary' : ''}`} style={{ padding: '3px 9px' }} onClick={onClick} title="默认:全部都用,按模板顺序">默认</button>; 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 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', const thumb = (on, isDef) => ({ padding: 0, borderRadius: 5, overflow: 'hidden', cursor: 'pointer', background: '#fff', position: 'relative',
@ -7794,6 +7840,7 @@ function TemplateModelPick({ tpl, products, update }) {
<div style={{ fontSize: 12, color: 'var(--ink-soft)', marginBottom: 8, lineHeight: 1.6 }}> <div style={{ fontSize: 12, color: 'var(--ink-soft)', marginBottom: 8, lineHeight: 1.6 }}>
每个型号每一项都可以单独选:<b>默认</b> = 全部都用,按模板里的顺序(拖动模板图片调整),模板以后新加的也自动带上。 每个型号每一项都可以单独选:<b>默认</b> = 全部都用,按模板里的顺序(拖动模板图片调整),模板以后新加的也自动带上。
不用默认时,直接点图片选择:只点一张就只用这一张;再点其它图加上,点已选的去掉。主图按点选顺序排列(数字),第 1 张是网站封面。 不用默认时,直接点图片选择:只点一张就只用这一张;再点其它图加上,点已选的去掉。主图按点选顺序排列(数字),第 1 张是网站封面。
点 <b>+</b> 可以从电脑给这个型号单独上传图片(只这个型号用):上传后改为手动选择,默认状态下只用上传的图,再点模板的图可以一起用;图片右上角 × 删除。
</div> </div>
<div style={{ display: 'grid', gap: 8, maxHeight: 520, overflowY: 'auto', paddingRight: 4 }}> <div style={{ display: 'grid', gap: 8, maxHeight: 520, overflowY: 'auto', paddingRight: 4 }}>
{[...users].sort((a, b) => coll.compare(label(a), label(b))).map(p => { {[...users].sort((a, b) => coll.compare(label(a), label(b))).map(p => {
@ -7806,38 +7853,44 @@ function TemplateModelPick({ tpl, products, update }) {
{isCustom ? <button type="button" className="btn btn-sm btn-ghost" style={{ marginLeft: 'auto' }} onClick={() => save(p.id, {})}>全部恢复默认</button> {isCustom ? <button type="button" className="btn btn-sm btn-ghost" style={{ marginLeft: 'auto' }} onClick={() => save(p.id, {})}>全部恢复默认</button>
: <span style={{ marginLeft: 'auto', fontSize: 12, color: 'var(--ink-soft)' }}>全部默认</span>} : <span style={{ marginLeft: 'auto', fontSize: 12, color: 'var(--ink-soft)' }}>全部默认</span>}
</div> </div>
{mains.length > 0 && ( {(() => { const allMain = [...mains, ...tplOwnFiles(tpl, p.id, 'main')]; return (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}> <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}>
<span style={rowL}>主图</span> <span style={rowL}>主图</span>
{defBtn(!e.main, () => setCat(p.id, 'main', null))} {defBtn(!e.main, () => setCat(p.id, 'main', null))}
{mains.map((f, i) => { {allMain.map((f, i) => {
const k = e.main ? e.main.indexOf(f) : i, on = !e.main || k >= 0; const own = i >= mains.length;
const k = e.main ? e.main.indexOf(f) : i, on = e.main ? k >= 0 : !own;
return ( return (
<button key={f} type="button" title={!e.main ? `默认第 ${i + 1} 张,点击改为只用这张` : on ? `第 ${k + 1} 张,点击去掉` : '点击加上'} onClick={() => clickItem(p.id, 'main', f, mains)} style={{ ...thumb(on, !e.main), width: 44, height: 44 }}> <button key={f} type="button" title={own ? (on ? `这个型号上传的图,第 ${k + 1} 张,点击去掉` : '这个型号上传的图,点击加上') : !e.main ? `默认第 ${i + 1} 张,点击改为只用这张` : on ? `第 ${k + 1} 张,点击去掉` : '点击加上'} onClick={() => clickItem(p.id, 'main', f, allMain)} style={{ ...thumb(on, !e.main), width: 44, height: 44, ...(own ? { borderColor: on ? 'var(--copper, #b4651e)' : undefined } : {}) }}>
<img src={`/uploads/${tpl.id}/${f}`} alt="" style={{ width: '100%', height: '100%', objectFit: 'contain' }} /> <img src={`/uploads/${tpl.id}/${f}`} alt="" style={{ width: '100%', height: '100%', objectFit: 'contain' }} />
{e.main && on && <span style={badge}>{k + 1}</span>} {e.main && on && <span style={badge}>{k + 1}</span>}
{own && ownMark(p.id, 'main', f)}
</button> </button>
); );
})} })}
{upBtn(p.id, 'main', 44, 44)}
{e.main && !e.main.length && <span style={{ fontSize: 11.5, color: 'var(--led-red)' }}>不用模板主图</span>} {e.main && !e.main.length && <span style={{ fontSize: 11.5, color: 'var(--led-red)' }}>不用模板主图</span>}
</div> </div>
)} ); })()}
{imgs.length > 0 && ( {(() => { const allImgs = [...imgs, ...tplOwnFiles(tpl, p.id, 'images')]; return (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}> <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}>
<span style={rowL}>详情图片</span> <span style={rowL}>详情图片</span>
{defBtn(!e.images, () => setCat(p.id, 'images', null))} {defBtn(!e.images, () => setCat(p.id, 'images', null))}
{imgs.map((f, i) => { {allImgs.map((f, i) => {
const on = !e.images || e.images.includes(f); const own = i >= imgs.length;
const on = e.images ? e.images.includes(f) : !own;
return ( return (
<button key={f} type="button" title={`第 ${i + 1} 张`} onClick={() => clickItem(p.id, 'images', f, imgs)} style={{ ...thumb(on, !e.images), width: 36, height: 50 }}> <button key={f} type="button" title={own ? '这个型号上传的图' : `第 ${i + 1} 张`} onClick={() => clickItem(p.id, 'images', f, allImgs)} style={{ ...thumb(on, !e.images), width: 36, height: 50 }}>
<img src={`/uploads/${tpl.id}/${f}`} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'top' }} /> <img src={`/uploads/${tpl.id}/${f}`} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'top' }} />
<span style={{ position: 'absolute', right: 2, bottom: 1, fontSize: 10, background: 'rgba(255,255,255,.85)', borderRadius: 3, padding: '0 2px' }}>{i + 1}</span> <span style={{ position: 'absolute', right: 2, bottom: 1, fontSize: 10, background: 'rgba(255,255,255,.85)', borderRadius: 3, padding: '0 2px' }}>{own ? '自' : i + 1}</span>
{own && ownMark(p.id, 'images', f)}
</button> </button>
); );
})} })}
{upBtn(p.id, 'images', 36, 50)}
{e.images && !e.images.length && <span style={{ fontSize: 11.5, color: 'var(--led-red)' }}>不用详情图片</span>} {e.images && !e.images.length && <span style={{ fontSize: 11.5, color: 'var(--led-red)' }}>不用详情图片</span>}
</div> </div>
)} ); })()}
{blocks.length > 0 && ( {blocks.length > 0 && (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}> <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}>
<span style={rowL}>HTML 片段</span> <span style={rowL}>HTML 片段</span>