Update App.jsx via upload script - 2026-10-12 11:45:26

This commit is contained in:
mike 2026-10-12 11:45:29 +08:00
parent 0e41e96110
commit be005c2a35
1 changed files with 213 additions and 2 deletions

215
App.jsx
View File

@ -845,6 +845,12 @@ function upsertProductByName(list, product) {
sellingPoints: existing.sellingPoints, sellingPoints: existing.sellingPoints,
costPrice: existing.costPrice, costPrice: existing.costPrice,
notes: existing.notes, notes: existing.notes,
// 网站字段:AI导入的数据里没有这几项,沿用旧记录的;图片跟着旧记录的id目录走
publishToWeb: product.publishToWeb ?? existing.publishToWeb,
webImages: existing.webImages,
youtubeUrl: product.youtubeUrl ?? existing.youtubeUrl,
webDescription: product.webDescription ?? existing.webDescription,
webSale: product.webSale ?? existing.webSale,
}; };
let copy = list.map((p, i) => (i === dupIdx ? merged : p)); let copy = list.map((p, i) => (i === dupIdx ? merged : p));
if (selfIdx !== -1 && selfIdx !== dupIdx) copy = copy.filter((_, i) => i !== selfIdx); if (selfIdx !== -1 && selfIdx !== dupIdx) copy = copy.filter((_, i) => i !== selfIdx);
@ -1023,6 +1029,12 @@ const blankProduct = () => ({
brochureFileName: '', brochureOriginalName: '', // 原版彩页(厂家原始彩页PDF/图片),只做保存和下载,不参与PDF导出排版 brochureFileName: '', brochureOriginalName: '', // 原版彩页(厂家原始彩页PDF/图片),只做保存和下载,不参与PDF导出排版
sellingPoints: '', // 对客卖点/主要特点,每行一条,专门给客户看的(和下面的costPrice/notes内部信息彻底分开,不会一起导出) sellingPoints: '', // 对客卖点/主要特点,每行一条,专门给客户看的(和下面的costPrice/notes内部信息彻底分开,不会一起导出)
costPrice: '', notes: '', costPrice: '', notes: '',
// —— 网站发布(public-api.js 只会对外输出这一组 + 规格字段,costPrice/notes 永远不输出)——
publishToWeb: false, // 是否发布到网站
webImages: [], // 网站主图文件名,最多6张,第一张为封面;为空时网站用 productImageFileName
youtubeUrl: '', // 网站产品视频(YouTube链接)
webDescription: { es: '', en: '', ru: '' }, // 网站产品介绍,三种语言
webSale: { mode: 'stock', stock: '', tiers: [] }, // 网站销售状态:stock现货 / group拼单;tiers阶梯价 [{qty, price}](USD)
}); });
function isUpsCategory(cat) { function isUpsCategory(cat) {
@ -1869,6 +1881,16 @@ const Styles = () => (
.catalog-filter-row { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; } .catalog-filter-row { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.catalog-filter-row .select { width: auto; min-width: 160px; } .catalog-filter-row .select { width: auto; min-width: 160px; }
.field-label-row { display: flex; align-items: center; justify-content: space-between; } .field-label-row { display: flex; align-items: center; justify-content: space-between; }
.web-section { border-top: 1px dashed var(--line); margin-top: 6px; padding-top: 14px; display: flex; flex-direction: column; gap: 14px; }
.web-section-title { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.web-thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.web-thumb { position: relative; width: 88px; height: 88px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; cursor: grab; background: #fff; }
.web-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.web-thumb .cover-tag { position: absolute; left: 4px; top: 4px; font-size: 10.5px; background: rgba(0,0,0,.65); color: #fff; padding: 1px 6px; border-radius: 4px; }
.web-thumb button { position: absolute; right: 3px; top: 3px; border: 0; border-radius: 4px; background: rgba(0,0,0,.6); color: #fff; width: 22px; height: 22px; display: flex; align-items: center; justify-content: center; cursor: pointer; padding: 0; }
.web-thumb-add { width: 88px; height: 88px; border: 1px dashed var(--line); border-radius: 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; font-size: 12px; color: var(--ink-soft); cursor: pointer; }
.web-badge { display: inline-flex; align-items: center; gap: 3px; margin-left: 8px; font-size: 11px; font-weight: 500; color: #1d7a3e; background: #dcf1e2; padding: 2px 8px; border-radius: 99px; }
.web-tier-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.field-manage-link { .field-manage-link {
font-size: 11.5px; font-weight: 600; color: var(--blue); cursor: pointer; font-size: 11.5px; font-weight: 600; color: var(--blue); cursor: pointer;
display: inline-flex; align-items: center; gap: 3px; text-transform: none; letter-spacing: 0; display: inline-flex; align-items: center; gap: 3px; text-transform: none; letter-spacing: 0;
@ -2742,6 +2764,7 @@ function MainApp({ username, onLogout }) {
...primary, ...primary,
id: 'p_' + Date.now() + '_' + i + '_' + Math.random().toString(36).slice(2, 7), id: 'p_' + Date.now() + '_' + i + '_' + Math.random().toString(36).slice(2, 7),
name: n, name: n,
publishToWeb: false, webImages: [], // 网站图片存在原产品的目录下,复制出来的型号需要单独上传、单独发布
})); }));
let working = products; let working = products;
let newCount = 0; let newCount = 0;
@ -5420,6 +5443,191 @@ function BackPanelReadOnly({ template }) {
); );
} }
/* ------------------------- 网站发布(对接 public-api.js) ------------------------- */
function youtubeIdOf(url) {
const m = String(url || '').match(/(?:youtu\.be\/|v=|embed\/|shorts\/)([\w-]{11})/);
return m ? m[1] : null;
}
const WEB_DESC_LANGS = [['es', '西班牙语'], ['en', '英语'], ['ru', '俄语']];
const WEB_MAX_IMAGES = 6;
function WebPublishSection({ draft, set, onNotify }) {
const images = Array.isArray(draft.webImages) ? draft.webImages : [];
const desc = draft.webDescription || {};
const sale = draft.webSale || { mode: 'stock', stock: '', tiers: [] };
const tiers = Array.isArray(sale.tiers) ? sale.tiers : [];
const [uploading, setUploading] = useState(false);
const [dragIdx, setDragIdx] = useState(null);
const ytId = youtubeIdOf(draft.youtubeUrl);
const setSale = (patch) => set({ webSale: { ...sale, ...patch } });
async function handlePick(fileList) {
const picked = Array.from(fileList || []);
const room = WEB_MAX_IMAGES - images.length;
if (picked.length > room && onNotify) onNotify(`网站主图最多${WEB_MAX_IMAGES}张,只上传了前${Math.max(room, 0)}张`);
const files = picked.slice(0, Math.max(room, 0));
if (!files.length) return;
setUploading(true);
const added = [];
for (const f of files) {
try {
const res = await uploadFile(draft.id, f);
added.push(res.fileName);
} catch (e) {
if (onNotify) onNotify(`${f.name} 上传失败,请重试`);
}
}
set({ webImages: [...images, ...added] });
setUploading(false);
}
async function handleRemove(idx) {
const f = images[idx];
// 和"产品主图"共用的那张不删文件,只从网站图片列表里移除
if (f && f !== draft.productImageFileName) {
try { await deleteFile(draft.id, f); } catch (e) {}
}
set({ webImages: images.filter((_, i) => i !== idx) });
}
function handleDrop(dropIdx) {
if (dragIdx === null || dragIdx === dropIdx) { setDragIdx(null); return; }
set({ webImages: reorderArray(images, dragIdx, dropIdx) });
setDragIdx(null);
}
function updateTier(idx, patch) {
setSale({ tiers: tiers.map((t, i) => (i === idx ? { ...t, ...patch } : t)) });
}
return (
<div className="web-section">
<div className="web-section-title"><Globe size={14} />网站发布</div>
<label style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 13, cursor: 'pointer' }}>
<input type="checkbox" checked={!!draft.publishToWeb} onChange={e => set({ publishToWeb: e.target.checked })} />
发布到网站(只公开规格、网站图片、视频、介绍和销售信息;正本价格和内部备注不会出现在网站上)
</label>
<div className="field">
<span className="field-label">网站主图(最多{WEB_MAX_IMAGES}张,第一张是封面,拖动可调整顺序)</span>
<div className="web-thumbs">
{images.map((f, idx) => (
<div
key={f + idx} className="web-thumb"
style={{ opacity: dragIdx === idx ? 0.5 : 1 }}
draggable
onDragStart={() => setDragIdx(idx)}
onDragOver={e => e.preventDefault()}
onDrop={() => handleDrop(idx)}
onDragEnd={() => setDragIdx(null)}
>
<img src={`/uploads/${draft.id}/${f}`} alt="" />
{idx === 0 && <span className="cover-tag">封面</span>}
<button type="button" title="移除" onClick={() => handleRemove(idx)}><X size={12} /></button>
</div>
))}
{images.length < WEB_MAX_IMAGES && (
<label className="web-thumb-add" style={{ cursor: uploading ? 'default' : 'pointer' }}>
<input type="file" accept="image/*" multiple style={{ display: 'none' }} disabled={uploading}
onChange={e => { handlePick(e.target.files); e.target.value = ''; }} />
<ImageIcon size={18} />{uploading ? '上传中…' : '添加图片'}
</label>
)}
</div>
{images.length === 0 && draft.productImageFileName && (
<div style={{ fontSize: 12, color: 'var(--ink-soft)', marginTop: 6 }}>
还没有网站主图时,网站会先用上面的"产品主图"。
<button type="button" className="btn btn-sm btn-ghost" onClick={() => set({ webImages: [draft.productImageFileName] })}>把产品主图设为第一张</button>
</div>
)}
</div>
<div className="field">
<span className="field-label">产品视频(YouTube链接,可选)</span>
<input className="input" value={draft.youtubeUrl || ''} onChange={e => set({ youtubeUrl: e.target.value })} placeholder="如:https://www.youtube.com/watch?v=xxxxxxxxxxx" />
{draft.youtubeUrl && (
<span style={{ fontSize: 12, color: ytId ? '#1d7a3e' : '#a5480b' }}>
{ytId ? `✓ 已识别视频ID:${ytId}` : '没能识别出视频ID,请粘贴完整的YouTube视频链接'}
</span>
)}
</div>
{WEB_DESC_LANGS.map(([code, label]) => (
<div className="field" key={code}>
<span className="field-label">网站产品介绍 · {label}</span>
<textarea
className="textarea" style={{ fontFamily: 'inherit', fontSize: 13 }} rows={3}
value={desc[code] || ''}
onChange={e => set({ webDescription: { ...desc, [code]: e.target.value } })}
/>
</div>
))}
<div className="grid-2">
<div className="field">
<span className="field-label">网站销售状态</span>
<select className="select" value={sale.mode || 'stock'} onChange={e => setSale({ mode: e.target.value })}>
<option value="stock">现货(起订20台,按20递增)</option>
<option value="group">拼单(起订100台,按100递增)</option>
</select>
</div>
{sale.mode !== 'group' ? (
<div className="field">
<span className="field-label">现货库存(台)</span>
<input className="input" type="number" min="0" value={sale.stock ?? ''} onChange={e => setSale({ stock: e.target.value })} placeholder="如:500" />
</div>
) : (
<div className="field">
<span className="field-label">拼单说明</span>
<div style={{ fontSize: 12.5, color: 'var(--ink-soft)', paddingTop: 6 }}>拼单进度、周期和定金确认在网站后台管理。</div>
</div>
)}
</div>
<div className="field">
<span className="field-label">阶梯价(美元/台,数量越大单价越低)</span>
{tiers.map((t, idx) => (
<div className="web-tier-row" key={idx}>
<input className="input" type="number" min="0" style={{ flex: 1 }} value={t.qty ?? ''} onChange={e => updateTier(idx, { qty: e.target.value })} placeholder={sale.mode === 'group' ? '起订数量,如 100' : '起订数量,如 20'} />
<span style={{ fontSize: 12.5, color: 'var(--ink-soft)' }}>台起</span>
<input className="input" type="number" min="0" step="0.01" style={{ flex: 1 }} value={t.price ?? ''} onChange={e => updateTier(idx, { price: e.target.value })} placeholder="单价 USD" />
<button type="button" className="btn btn-sm btn-ghost btn-danger" onClick={() => setSale({ tiers: tiers.filter((_, i) => i !== idx) })}><Trash2 size={12} /></button>
</div>
))}
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setSale({ tiers: [...tiers, { qty: '', price: '' }] })}>
<Plus size={12} style={{ verticalAlign: -2, marginRight: 4 }} />添加一档
</button>
</div>
</div>
);
}
// 产品详情页右上角的"更新到网站":调用 public-api.js 里的 /api/site/deploy,触发 Cloudflare 重新构建
function SiteDeployButton() {
const [state, setState] = useState('idle'); // idle | busy | ok | err
const [msg, setMsg] = useState('');
async function run() {
setState('busy'); setMsg('');
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}`);
setState('ok'); setMsg('已通知网站更新,约1分钟后生效');
} catch (e) {
setState('err'); setMsg(`更新失败:${e.message}`);
}
}
return (
<>
{msg && <span style={{ fontSize: 12, alignSelf: 'center', color: state === 'err' ? '#a5480b' : 'var(--ink-soft)' }}>{msg}</span>}
<button className="btn btn-sm" onClick={run} disabled={state === 'busy'}><Globe size={12} />{state === 'busy' ? '提交中…' : '更新到网站'}</button>
</>
);
}
function ProductForm({ draft, setDraft, onSave, onCancel, categories, onManage, onNotify, backPanelTemplates }) { function ProductForm({ draft, setDraft, onSave, onCancel, categories, onManage, onNotify, backPanelTemplates }) {
const set = (patch) => setDraft(d => ({ ...d, ...patch })); const set = (patch) => setDraft(d => ({ ...d, ...patch }));
const cat = (categories || []).find(c => c.id === draft.category); const cat = (categories || []).find(c => c.id === draft.category);
@ -6142,6 +6350,8 @@ function ProductForm({ draft, setDraft, onSave, onCancel, categories, onManage,
/> />
</div> </div>
<WebPublishSection draft={draft} set={set} onNotify={onNotify} />
<div className="field"><span className="field-label">正本价格(元)</span><input className="input" value={draft.costPrice} onChange={e => set({ costPrice: e.target.value })} placeholder="采购/成本价,人民币" /></div> <div className="field"><span className="field-label">正本价格(元)</span><input className="input" value={draft.costPrice} onChange={e => set({ costPrice: e.target.value })} placeholder="采购/成本价,人民币" /></div>
<div className="field"> <div className="field">
<span className="field-label">内部备注(仅自己可见,不会出现在任何导出的文档里)</span> <span className="field-label">内部备注(仅自己可见,不会出现在任何导出的文档里)</span>
@ -6165,8 +6375,9 @@ function ProductDetail({ product: p, onEdit, onDelete, categories, exportLang, o
<> <>
<div className="panel section-gap"> <div className="panel section-gap">
<div className="panel-header"> <div className="panel-header">
<div className="panel-title"><div className="led-dot blue" />{p.name || '(未命名)'}</div> <div className="panel-title"><div className="led-dot blue" />{p.name || '(未命名)'}{p.publishToWeb && <span className="web-badge"><Globe size={11} />已发布到网站</span>}</div>
<div style={{ display: 'flex', gap: 8 }}> <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', justifyContent: 'flex-end' }}>
{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>
<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>