Update App.jsx via upload script - 2026-10-21 11:22:06

This commit is contained in:
mike 2026-10-21 11:22:11 +08:00
parent adcdaeda55
commit f8426c5994
1 changed files with 60 additions and 5 deletions

65
App.jsx
View File

@ -6160,6 +6160,24 @@ 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 秒后自动更新网站(多次勾选合并成一次)
const [deployMsg, setDeployMsg] = useState('');
const deployTimer = useRef(null);
useEffect(() => () => { if (deployTimer.current) clearTimeout(deployTimer.current); }, []);
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) => {
@ -6168,9 +6186,11 @@ function TemplateProductMatcher({ tpl, products, setProducts, onNotify }) {
if (!set.has(p.id)) return p; if (!set.has(p.id)) return p;
const cur = Array.isArray(p.webDetailTemplateIds) ? p.webDetailTemplateIds : []; const cur = Array.isArray(p.webDetailTemplateIds) ? p.webDetailTemplateIds : [];
const has = cur.includes(tpl.id); const has = cur.includes(tpl.id);
if (on === has) return p; if (on === has && (!on || p.publishToWeb)) return p;
return { ...p, webDetailTemplateIds: on ? [...cur, tpl.id] : cur.filter(x => x !== tpl.id) }; // 选中 = 同时发布到网站;取消选中只移除模板,不会取消发布
return { ...p, webDetailTemplateIds: on ? (has ? cur : [...cur, tpl.id]) : cur.filter(x => x !== tpl.id), ...(on ? { publishToWeb: true } : {}) };
})); }));
scheduleDeploy();
}; };
const selected = products.filter(uses); const selected = products.filter(uses);
const kw = q.trim().toLowerCase(); const kw = q.trim().toLowerCase();
@ -6186,7 +6206,7 @@ function TemplateProductMatcher({ tpl, products, setProducts, onNotify }) {
return ( return (
<div className="field" style={{ border: '1px solid var(--line)', borderRadius: 10, padding: 12, marginTop: 6 }}> <div className="field" style={{ border: '1px solid var(--line)', borderRadius: 10, padding: 12, marginTop: 6 }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8, flexWrap: 'wrap', marginBottom: 8 }}> <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8, flexWrap: 'wrap', marginBottom: 8 }}>
<span className="field-label" style={{ margin: 0 }}>使用这个模板的产品(已选 {selected.length} 个)</span> <span className="field-label" style={{ margin: 0 }}>使用这个模板的产品(已选 {selected.length} 个;选中即自动发布到网站){deployMsg && <span style={{ marginLeft: 8, color: '#1d7a3e', fontWeight: 400 }}>{deployMsg}</span>}</span>
<span style={{ display: 'flex', gap: 6, flexWrap: 'wrap' }}> <span style={{ display: 'flex', gap: 6, flexWrap: 'wrap' }}>
{tableHits.length > 0 && ( {tableHits.length > 0 && (
<button type="button" className="btn btn-sm" onClick={() => { setUse(tableHits.map(p => p.id), true); onNotify && onNotify(`已按表格型号选中 ${tableHits.length} 个产品`); }}> <button type="button" className="btn btn-sm" onClick={() => { setUse(tableHits.map(p => p.id), true); onNotify && onNotify(`已按表格型号选中 ${tableHits.length} 个产品`); }}>
@ -6405,13 +6425,16 @@ function WebPublishSection({ draft, set, onNotify, templates, setTemplates, prod
</div> </div>
<div className="field"> <div className="field">
<span className="field-label">网站产品介绍(英文)——西班牙语、俄语在更新网站时自动翻译</span> <span className="field-label">网站产品介绍(英文)——留空则更新网站时由 AI 根据参数自动生成(参数改了会重新生成);填了就用你写的。西班牙语、俄语自动翻译</span>
<textarea <textarea
className="textarea" style={{ fontFamily: 'inherit', fontSize: 13 }} rows={4} className="textarea" style={{ fontFamily: 'inherit', fontSize: 13 }} rows={4}
value={desc.en || ''} value={desc.en || ''}
onChange={e => set({ webDescription: { ...desc, en: e.target.value } })} onChange={e => set({ webDescription: { ...desc, en: e.target.value } })}
placeholder="如:Mini UPS with 24V/48V PoE output for routers, ONTs and IP cameras." placeholder="留空 = AI 根据参数自动生成。想自己写就直接在这里填英文介绍。"
/> />
<AiDescriptionPreview productId={draft.id} hasManual={!!String(desc.en || '').trim()}
onUse={(text) => set({ webDescription: { ...desc, en: text } })}
onClear={() => set({ webDescription: { ...desc, en: '' } })} />
<button type="button" className="btn btn-sm btn-ghost" style={{ alignSelf: 'flex-start' }} onClick={() => setShowManual(v => !v)}> <button type="button" className="btn btn-sm btn-ghost" style={{ alignSelf: 'flex-start' }} onClick={() => setShowManual(v => !v)}>
{showManual ? '收起手动译文' : '手动修改译文(可选)'} {showManual ? '收起手动译文' : '手动修改译文(可选)'}
</button> </button>
@ -6481,6 +6504,38 @@ function WebPublishSection({ draft, set, onNotify, templates, setTemplates, prod
); );
} }
// 网站产品介绍:看 AI 根据参数写的介绍(网站上留空时显示的就是这段);可以拿来作为手动修改的起点
function AiDescriptionPreview({ productId, hasManual, onUse, onClear }) {
const [state, setState] = useState('idle'); // idle | busy | done | err
const [text, setText] = useState('');
async function load() {
setState('busy');
try {
const r = await fetch(`/api/site/ai-description/${encodeURIComponent(productId)}`);
const d = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(d.error || `HTTP ${r.status}`);
setText(d.text); setState('done');
} catch (e) { setText(e.message); setState('err'); }
}
return (
<div style={{ display: 'grid', gap: 6, marginTop: 6 }}>
<div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', alignItems: 'center' }}>
<span style={{ fontSize: 12, color: hasManual ? '#9a6700' : '#1d7a3e' }}>{hasManual ? '当前:使用你手写的介绍' : '当前:AI 自动生成'}</span>
<button type="button" className="btn btn-sm btn-ghost" disabled={state === 'busy' || !productId} onClick={load}>{state === 'busy' ? 'AI 生成中…' : '查看 AI 生成的介绍'}</button>
{hasManual && <button type="button" className="btn btn-sm btn-ghost" onClick={() => { if (window.confirm('清空手写介绍,改回 AI 自动生成?')) onClear(); }}>改回 AI 自动生成</button>}
</div>
{state === 'done' && (
<div style={{ fontSize: 12.5, background: 'var(--bg-soft, #f6f8fa)', borderRadius: 8, padding: '8px 10px', lineHeight: 1.6 }}>
{text}
<div style={{ marginTop: 6 }}><button type="button" className="btn btn-sm" onClick={() => onUse(text)}>用这段作为起点手动修改</button></div>
</div>
)}
{state === 'err' && <div style={{ fontSize: 12, color: '#a5480b' }}>{text}</div>}
{!productId && <div style={{ fontSize: 12, color: 'var(--ink-soft)' }}>新产品保存后可以预览 AI 介绍</div>}
</div>
);
}
// 产品详情页右上角的"更新到网站":调用 public-api.js 里的 /api/site/deploy,触发 Cloudflare 重新构建 // 产品详情页右上角的"更新到网站":调用 public-api.js 里的 /api/site/deploy,触发 Cloudflare 重新构建
function SiteDeployButton() { function SiteDeployButton() {
const [state, setState] = useState('idle'); // idle | busy | ok | err const [state, setState] = useState('idle'); // idle | busy | ok | err