Update App.jsx via upload script - 2026-11-04 11:53:49

This commit is contained in:
mike 2026-11-04 11:53:55 +08:00
parent 179af185a0
commit 942e3ded49
1 changed files with 42 additions and 141 deletions

183
App.jsx
View File

@ -6937,7 +6937,7 @@ function SiteAssetsEditor({ assets, setAssets, onNotify }) {
}); });
return ( return (
<div style={{ display: 'grid', gap: 22 }}> <div style={{ display: 'grid', gap: 22 }}>
<div className="disclaimer">在下面对应的位置上传图片,然后点右上角"更新到网站"。网站构建时会自动压缩图片;没上传的位置在网站上自动隐藏或显示图标,不会出现空白。建议照片宽度不小于 1200 像素。</div> <div className="disclaimer">在下面对应的位置上传图片,停止修改约 3 分钟后网站自动更新。网站构建时会自动压缩图片;没上传的位置在网站上自动隐藏或显示图标,不会出现空白。建议照片宽度不小于 1200 像素。</div>
{SITE_SLOT_GROUPS.map(pg => { {SITE_SLOT_GROUPS.map(pg => {
const all = pg.groups.flatMap(g => g.slots); const all = pg.groups.flatMap(g => g.slots);
const done = all.filter(sl => (assets[sl.id] || []).length).length; const done = all.filter(sl => (assets[sl.id] || []).length).length;
@ -7255,7 +7255,7 @@ function DetailTemplateManager({ templates, setTemplates, products, setProducts,
{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)} />
<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)} 个产品在用这个模板;修改后点"更新到网站"生效` : '还没有产品使用这个模板'}</span> <span>{usage(active.id) ? `${usage(active.id)} 个产品在用这个模板;修改后约 3 分钟自动同步到网站` : '还没有产品使用这个模板'}</span>
<span style={{ display: 'flex', gap: 6 }}> <span style={{ display: 'flex', gap: 6 }}>
<button type="button" className="btn btn-sm" disabled={copying} onClick={() => copyTemplate(active)}><Copy size={12} />{copying ? '复制中…' : '复制模板'}</button> <button type="button" className="btn btn-sm" disabled={copying} onClick={() => copyTemplate(active)}><Copy size={12} />{copying ? '复制中…' : '复制模板'}</button>
<button type="button" className="btn btn-sm btn-danger" onClick={() => removeTemplate(active)}><Trash2 size={12} />删除模板</button> <button type="button" className="btn btn-sm btn-danger" onClick={() => removeTemplate(active)}><Trash2 size={12} />删除模板</button>
@ -7286,23 +7286,15 @@ function DetailTemplateManager({ templates, setTemplates, products, setProducts,
// 模板管理(产品管理 → 模板管理):编辑模板 + 给模板勾选使用它的产品(一个系列一次选完,不用逐个产品去编辑) // 模板管理(产品管理 → 模板管理):编辑模板 + 给模板勾选使用它的产品(一个系列一次选完,不用逐个产品去编辑)
function TemplateCenter({ templates, setTemplates, products, setProducts, onNotify, savedTick }) { function TemplateCenter({ templates, setTemplates, products, setProducts, onNotify, savedTick }) {
// 每个模板是否已经同步到网站:synced / changed / new;deleted = 后台删了、网站上还在 // 每个模板是否已经同步到网站:synced / changed / new;deleted = 后台删了、网站上还在
const [sync, setSync] = useState({ status: {}, deleted: [], loaded: false }); const [sync] = useSiteSync(savedTick);
const loadSync = async () => {
try {
const r = await fetch('/api/site/templates-status');
const d = await r.json();
if (r.ok) setSync({ status: d.status || {}, deleted: d.deleted || [], loaded: true });
} catch (e) {}
};
useEffect(() => { const t = setTimeout(loadSync, savedTick ? 600 : 0); return () => clearTimeout(t); }, [savedTick]);
return ( return (
<div className="panel"> <div className="panel">
<div className="panel-header" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 10, flexWrap: 'wrap' }}> <div className="panel-header" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 10, flexWrap: 'wrap' }}>
<div className="panel-title">模板管理</div> <div className="panel-title">模板管理</div>
<TemplateSyncButton templates={templates} sync={sync} reload={loadSync} /> <SiteDeployButton sync={sync} />
</div> </div>
<div className="panel-body" style={{ padding: 18 }}> <div className="panel-body" style={{ padding: 18 }}>
<div className="disclaimer" style={{ marginBottom: 14 }}>每个模板可以包含三项内容,分开上传、互不重复:主图(同系列共用,接在产品自己的主图后面)、详情图片(工厂、认证、包装等,显示在详情图下方)、HTML(系列参数表)。选中一个模板后,在下面"使用这个模板的产品"里勾选型号;改完点右上角"更新到网站"。产品编辑页"网站发布"里也能单独添加或移除,两边是同一份数据。</div> <div className="disclaimer" style={{ marginBottom: 14 }}>每个模板可以包含三项内容,分开上传、互不重复:主图(同系列共用,接在产品自己的主图后面)、详情图片(工厂、认证、包装等,显示在详情图下方)、HTML(系列参数表)。选中一个模板后,在下面"使用这个模板的产品"里勾选型号。模板改完自动保存,停止修改约 3 分钟后自动同步到网站,不用手动更新。产品编辑页"网站发布"里也能单独添加或移除,两边是同一份数据。</div>
<DetailTemplateManager inline templates={templates} setTemplates={setTemplates} products={products} setProducts={setProducts} onNotify={onNotify} syncStatus={sync.loaded ? sync.status : null} /> <DetailTemplateManager inline templates={templates} setTemplates={setTemplates} products={products} setProducts={setProducts} onNotify={onNotify} syncStatus={sync.loaded ? sync.status : null} />
</div> </div>
</div> </div>
@ -7312,24 +7304,10 @@ function TemplateCenter({ templates, setTemplates, products, setProducts, onNoti
function TemplateProductMatcher({ tpl, products, setProducts, onNotify }) { function TemplateProductMatcher({ tpl, products, setProducts, onNotify }) {
const [q, setQ] = useState(''); const [q, setQ] = useState('');
const [onlySel, setOnlySel] = useState(false); const [onlySel, setOnlySel] = useState(false);
// 勾选型号后自动发布:产品打开"发布到网站",停手 15 秒后自动更新网站(多次勾选合并成一次) // 勾选型号后自动发布:产品打开"发布到网站",服务器停手约 3 分钟后自动更新网站(多次勾选合并成一次)
const [deployMsg, setDeployMsg] = useState(''); const [deployMsg, setDeployMsg] = useState('');
const deployTimer = useRef(null); // 不再从页面触发网站更新:产品保存后服务器停手约 3 分钟自动更新网站
useEffect(() => () => { if (deployTimer.current) clearTimeout(deployTimer.current); }, []); function scheduleDeploy() { setDeployMsg('已保存,约 3 分钟后自动更新网站'); }
function scheduleDeploy() {
if (deployTimer.current) clearTimeout(deployTimer.current);
setDeployMsg('已自动发布,15 秒后自动更新网站…');
deployTimer.current = setTimeout(async () => {
deployTimer.current = null;
setDeployMsg('正在提交网站更新…');
try {
const r = await fetch('/api/site/deploy', { method: 'POST' });
const d = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(d.error || `HTTP ${r.status}`);
setDeployMsg(d.note || '已通知网站更新,约1分钟后生效');
} catch (e) { setDeployMsg(`自动更新失败:${e.message},请点右上角"更新到网站"`); }
}, 15000);
}
const uses = (p) => Array.isArray(p.webDetailTemplateIds) && p.webDetailTemplateIds.includes(tpl.id); const uses = (p) => Array.isArray(p.webDetailTemplateIds) && p.webDetailTemplateIds.includes(tpl.id);
const label = (p) => p.name || p.model || '未命名'; const label = (p) => p.name || p.model || '未命名';
const setUse = (ids, on) => { const setUse = (ids, on) => {
@ -7995,7 +7973,7 @@ function PackSpecCenter({ specs, setSpecs, products, setProducts, onNotify }) {
<SiteDeployButton /> <SiteDeployButton />
</div> </div>
<div className="panel-body" style={{ padding: 18 }}> <div className="panel-body" style={{ padding: 18 }}>
<div className="disclaimer" style={{ marginBottom: 14 }}>一个箱规 = 每箱数量 + 每箱净重/毛重 + 外箱尺寸。选中一个箱规后,在下面勾选用这个箱规的型号(一个型号只能用一个箱规)。网站产品页会按客户填的数量自动生成箱单、算需要几个集装箱。改完箱规数值请点右上角"更新到网站"。</div> <div className="disclaimer" style={{ marginBottom: 14 }}>一个箱规 = 每箱数量 + 每箱净重/毛重 + 外箱尺寸。选中一个箱规后,在下面勾选用这个箱规的型号(一个型号只能用一个箱规)。网站产品页会按客户填的数量自动生成箱单、算需要几个集装箱。改完箱规数值,约 3 分钟后网站自动更新。</div>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 14 }}> <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 14 }}>
{specs.map(sp => ( {specs.map(sp => (
<button type="button" key={sp.id} className={`btn btn-sm${sp.id === activeId ? ' btn-primary' : ''}`} onClick={() => setActiveId(sp.id)}> <button type="button" key={sp.id} className={`btn btn-sm${sp.id === activeId ? ' btn-primary' : ''}`} onClick={() => setActiveId(sp.id)}>
@ -8029,22 +8007,8 @@ function PackProductMatcher({ spec, specs, products, setProducts }) {
const [q, setQ] = useState(''); const [q, setQ] = useState('');
const [onlySel, setOnlySel] = useState(false); const [onlySel, setOnlySel] = useState(false);
const [deployMsg, setDeployMsg] = useState(''); const [deployMsg, setDeployMsg] = useState('');
const deployTimer = useRef(null); // 不再从页面触发网站更新:产品保存后服务器停手约 3 分钟自动更新网站
useEffect(() => () => { if (deployTimer.current) clearTimeout(deployTimer.current); }, []); function scheduleDeploy() { setDeployMsg('已保存,约 3 分钟后自动更新网站'); }
function scheduleDeploy() {
if (deployTimer.current) clearTimeout(deployTimer.current);
setDeployMsg('15 秒后自动更新网站…');
deployTimer.current = setTimeout(async () => {
deployTimer.current = null;
setDeployMsg('正在提交网站更新…');
try {
const r = await fetch('/api/site/deploy', { method: 'POST' });
const d = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(d.error || `HTTP ${r.status}`);
setDeployMsg(d.note || '已通知网站更新,约1分钟后生效');
} catch (e) { setDeployMsg(`自动更新失败:${e.message},请点右上角"更新到网站"`); }
}, 15000);
}
const label = (p) => p.name || p.model || '未命名'; const label = (p) => p.name || p.model || '未命名';
const uses = (p) => p.webPackId === spec.id; const uses = (p) => p.webPackId === spec.id;
const otherName = (p) => { const o = p.webPackId && p.webPackId !== spec.id && specs.find(x => x.id === p.webPackId); return o ? o.name : ''; }; const otherName = (p) => { const o = p.webPackId && p.webPackId !== spec.id && specs.find(x => x.id === p.webPackId); return o ? o.name : ''; };
@ -8324,103 +8288,40 @@ function AiDescriptionPreview({ productId, hasManual, onUse, onClear }) {
); );
} }
// 模板管理右上角的"更新到网站":选择要同步的模板(默认勾选有改动 / 新建的),只把选中的模板同步到网站,然后重新构建网站。 // 网站自动更新的状态(原来的"更新到网站"按钮):产品、模板、箱规、网站图片、近期动态保存后,
// 没选的模板,网站上保持上次同步时的样子。 // 停止修改约 3 分钟,服务器自动同步并重新构建网站(连续修改只构建一次),关掉页面也照常进行。
function TemplateSyncButton({ templates, sync, reload }) { function useSiteSync(savedTick) {
const [open, setOpen] = useState(false); const [sync, setSync] = useState({ status: {}, deleted: [], siteChanged: false, autoAt: 0, running: false, loaded: false });
const [sel, setSel] = useState(new Set()); const load = async () => {
const [state, setState] = useState('idle');
const [msg, setMsg] = useState('');
const pending = (id) => sync.status[id] !== 'synced';
function openPicker() {
setSel(new Set([...templates.filter(t => pending(t.id)).map(t => t.id), ...sync.deleted.map(d => d.id)]));
setMsg(''); setOpen(true);
}
const toggle = (id) => setSel(s => { const n = new Set(s); n.has(id) ? n.delete(id) : n.add(id); return n; });
async function run() {
if (hasUnsavedChanges()) { setMsg('还有修改正在保存,请稍等几秒再点'); return; }
setState('busy'); setMsg('');
try { try {
if (sel.size) { const r = await fetch('/api/site/templates-status');
const r = await fetch('/api/site/templates-publish', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ids: [...sel] }) }); const d = await r.json();
const d = await r.json().catch(() => ({})); if (r.ok) setSync({ status: d.status || {}, deleted: d.deleted || [], siteChanged: !!d.siteChanged, autoAt: d.autoAt || 0, running: !!d.autoRunning, loaded: true });
if (!r.ok) throw new Error(d.error || `HTTP ${r.status}`); } catch (e) {}
} };
const r2 = await fetch('/api/site/deploy', { method: 'POST' }); useEffect(() => { const t = setTimeout(load, savedTick ? 600 : 0); return () => clearTimeout(t); }, [savedTick]);
const d2 = await r2.json().catch(() => ({})); useEffect(() => { const t = setInterval(load, 20000); return () => clearInterval(t); }, []);
if (!r2.ok) throw new Error(d2.error || `HTTP ${r2.status}`); return [sync, load];
setState('ok'); setOpen(false);
setMsg((sel.size ? `已同步 ${sel.size} 个模板,` : '') + (d2.note || '已通知网站更新,约1分钟后生效'));
reload();
} catch (e) { setState('err'); setMsg(`更新失败:${e.message}`); }
}
const nPending = templates.filter(t => pending(t.id)).length + sync.deleted.length;
return (
<>
{msg && !open && <span style={{ fontSize: 12, alignSelf: 'center', color: state === 'err' ? '#a5480b' : 'var(--ink-soft)' }}>{msg}</span>}
<button className="btn btn-sm" onClick={openPicker} disabled={state === 'busy' || !sync.loaded}><Globe size={12} />更新到网站{sync.loaded && nPending ? `(${nPending} 个模板未同步)` : ''}</button>
{open && (
<div className="modal-overlay" onClick={() => setOpen(false)}>
<div className="modal-box" style={{ maxWidth: 560, width: '94vw' }} onClick={e => e.stopPropagation()}>
<div className="modal-head">
<div className="panel-title">选择要同步到网站的模板</div>
<button className="btn btn-ghost btn-sm" onClick={() => setOpen(false)}><X size={14} /></button>
</div>
<div className="modal-body" style={{ display: 'grid', gap: 6, maxHeight: '60vh', overflow: 'auto' }}>
<div style={{ fontSize: 12.5, color: 'var(--ink-soft)', marginBottom: 4 }}>勾选的模板会把后台现在的内容同步到网站;没勾选的,网站上保持上次同步的样子。产品的修改(发布、价格、参数等)每次都会一起更新。</div>
<div style={{ display: 'flex', gap: 8, marginBottom: 4 }}>
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setSel(new Set([...templates.filter(t => pending(t.id)).map(t => t.id), ...sync.deleted.map(d => d.id)]))}>只选未同步的</button>
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setSel(new Set())}>全部不选</button>
</div>
{templates.map(t => (
<label key={t.id} style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '6px 8px', border: '1px solid var(--line)', borderRadius: 8, cursor: 'pointer' }}>
<input type="checkbox" checked={sel.has(t.id)} onChange={() => toggle(t.id)} />
<span style={{ flex: 1 }}>{t.name || '未命名模板'}</span>
<span style={{ fontSize: 12, padding: '1px 8px', borderRadius: 99, background: pending(t.id) ? 'rgba(127,127,127,.15)' : '#dcfce7', color: pending(t.id) ? 'var(--ink-soft)' : '#166534' }}>
{sync.status[t.id] === 'synced' ? '✓ 已同步' : sync.status[t.id] === 'new' ? '新模板' : '有修改'}
</span>
</label>
))}
{sync.deleted.map(d => (
<label key={d.id} style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '6px 8px', border: '1px dashed var(--line)', borderRadius: 8, cursor: 'pointer' }}>
<input type="checkbox" checked={sel.has(d.id)} onChange={() => toggle(d.id)} />
<span style={{ flex: 1, textDecoration: 'line-through', color: 'var(--ink-soft)' }}>{d.name || d.id}</span>
<span style={{ fontSize: 12, color: 'var(--ink-soft)' }}>已删除,同步后网站上去掉</span>
</label>
))}
</div>
<div className="modal-foot" style={{ display: 'flex', gap: 8, justifyContent: 'flex-end', alignItems: 'center' }}>
{msg && <span style={{ fontSize: 12, color: '#a5480b', marginRight: 'auto' }}>{msg}</span>}
<button className="btn" onClick={() => setOpen(false)}>取消</button>
<button className="btn btn-primary" onClick={run} disabled={state === 'busy'}>{state === 'busy' ? '提交中…' : sel.size ? `同步 ${sel.size} 个模板并更新网站` : '只更新产品(不同步模板)'}</button>
</div>
</div>
</div>
)}
</>
);
} }
function SiteDeployButton({ sync: given }) {
// 产品详情页右上角的"更新到网站":调用 public-api.js 里的 /api/site/deploy,触发 Cloudflare 重新构建 const [own] = useSiteSync(0);
function SiteDeployButton() { const sync = given || own;
const [state, setState] = useState('idle'); // idle | busy | ok | err const [, tick] = useState(0);
const [msg, setMsg] = useState(''); useEffect(() => { const t = setInterval(() => tick(n => n + 1), 15000); return () => clearInterval(t); }, []);
async function run() { if (!sync.loaded) return null;
setState('busy'); setMsg(''); const tplN = Object.values(sync.status).filter(v => v !== 'synced').length + sync.deleted.length;
try { const what = [tplN ? `${tplN} 个模板` : '', sync.siteChanged ? '产品 / 网站内容' : ''].filter(Boolean).join(' 和 ');
const r = await fetch('/api/site/deploy', { method: 'POST' }); const pending = !!what;
const d = await r.json().catch(() => ({})); const mins = sync.autoAt ? Math.max(1, Math.ceil((sync.autoAt - Date.now()) / 60000)) : 0;
if (!r.ok) throw new Error(d.error || `HTTP ${r.status}`); const text = !pending ? '网站已是最新'
setState('ok'); setMsg(d.note || '已通知网站更新,约1分钟后生效'); : sync.running ? `正在更新网站(${what})…`
} catch (e) { : sync.autoAt ? `${what}有修改,停止修改后约 ${mins} 分钟自动更新网站`
setState('err'); setMsg(`更新失败:${e.message}`); : `${what}有修改,即将自动更新网站`;
}
}
return ( return (
<> <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 12.5, color: 'var(--ink-soft)', alignSelf: 'center' }} title="修改会自动保存;停止修改约 3 分钟后,服务器自动更新网站(连续修改只更新一次),不用手动点。网站构建大约还要 1–2 分钟。只改进价、备注这类后台字段不会触发网站更新。">
{msg && <span style={{ fontSize: 12, alignSelf: 'center', color: state === 'err' ? '#a5480b' : 'var(--ink-soft)' }}>{msg}</span>} <span style={{ display: 'inline-grid', placeItems: 'center', width: 16, height: 16, borderRadius: 4, fontSize: 11, fontWeight: 700, background: pending ? 'rgba(127,127,127,.25)' : '#16a34a', color: pending ? 'rgba(127,127,127,.9)' : '#fff' }}>✓</span>
<button className="btn btn-sm" onClick={run} disabled={state === 'busy'}><Globe size={12} />{state === 'busy' ? '提交中…' : '更新到网站'}</button> {text}
</> </span>
); );
} }