Update App.jsx via upload script - 2026-10-21 11:33:35

This commit is contained in:
mike 2026-10-21 11:33:40 +08:00
parent e3ca100b57
commit b808a5c57e
1 changed files with 35 additions and 15 deletions

50
App.jsx
View File

@ -1545,6 +1545,8 @@ function normalizeProductTaxonomy(products, categories) {
/* -------------------------------- storage ------------------------------ */ /* -------------------------------- storage ------------------------------ */
async function loadAll() { async function loadAll() {
// 读取失败(服务器重启中、网络断了)和"还没有数据"必须区分:读取失败时绝不能把空列表当成真实数据再存回服务器
const failed = [];
let customers = []; let customers = [];
let products = []; let products = [];
let quotes = []; let quotes = [];
@ -1556,65 +1558,66 @@ async function loadAll() {
try { try {
const c = await storageGet(STORAGE_KEYS.customers); const c = await storageGet(STORAGE_KEYS.customers);
if (c && c.value) customers = JSON.parse(c.value); if (c && c.value) customers = JSON.parse(c.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const p = await storageGet(STORAGE_KEYS.products); const p = await storageGet(STORAGE_KEYS.products);
if (p && p.value) products = JSON.parse(p.value); if (p && p.value) products = JSON.parse(p.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const q = await storageGet(STORAGE_KEYS.quotes); const q = await storageGet(STORAGE_KEYS.quotes);
if (q && q.value) quotes = JSON.parse(q.value); if (q && q.value) quotes = JSON.parse(q.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const sp = await storageGet(STORAGE_KEYS.sellerProfiles); const sp = await storageGet(STORAGE_KEYS.sellerProfiles);
if (sp && sp.value) sellerProfiles = JSON.parse(sp.value); if (sp && sp.value) sellerProfiles = JSON.parse(sp.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const cf = await storageGet(STORAGE_KEYS.certifications); const cf = await storageGet(STORAGE_KEYS.certifications);
if (cf && cf.value) certifications = JSON.parse(cf.value); if (cf && cf.value) certifications = JSON.parse(cf.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const pc = await storageGet(STORAGE_KEYS.productCategories); const pc = await storageGet(STORAGE_KEYS.productCategories);
if (pc && pc.value) productCategories = JSON.parse(pc.value); if (pc && pc.value) productCategories = JSON.parse(pc.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const pl = await storageGet(STORAGE_KEYS.packingLists); const pl = await storageGet(STORAGE_KEYS.packingLists);
if (pl && pl.value) packingLists = JSON.parse(pl.value); if (pl && pl.value) packingLists = JSON.parse(pl.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
try { try {
const cd = await storageGet(STORAGE_KEYS.catalogDocs); const cd = await storageGet(STORAGE_KEYS.catalogDocs);
if (cd && cd.value) catalogDocs = JSON.parse(cd.value); if (cd && cd.value) catalogDocs = JSON.parse(cd.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
let jmsLines = []; let jmsLines = [];
try { try {
const jl = await storageGet(STORAGE_KEYS.jmsLines); const jl = await storageGet(STORAGE_KEYS.jmsLines);
if (jl && jl.value) jmsLines = JSON.parse(jl.value); if (jl && jl.value) jmsLines = JSON.parse(jl.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
let backPanelTemplates = []; let backPanelTemplates = [];
try { try {
const bpt = await storageGet(STORAGE_KEYS.backPanelTemplates); const bpt = await storageGet(STORAGE_KEYS.backPanelTemplates);
if (bpt && bpt.value) backPanelTemplates = JSON.parse(bpt.value); if (bpt && bpt.value) backPanelTemplates = JSON.parse(bpt.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
let suppliers = []; let suppliers = [];
try { try {
const sup = await storageGet(STORAGE_KEYS.suppliers); const sup = await storageGet(STORAGE_KEYS.suppliers);
if (sup && sup.value) suppliers = JSON.parse(sup.value); if (sup && sup.value) suppliers = JSON.parse(sup.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
let webDetailTemplates = []; let webDetailTemplates = [];
try { try {
const wdt = await storageGet(STORAGE_KEYS.webDetailTemplates); const wdt = await storageGet(STORAGE_KEYS.webDetailTemplates);
if (wdt && wdt.value) webDetailTemplates = JSON.parse(wdt.value); if (wdt && wdt.value) webDetailTemplates = JSON.parse(wdt.value);
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
let webAssets = {}; let webAssets = {};
try { try {
const wa = await storageGet(STORAGE_KEYS.webAssets); const wa = await storageGet(STORAGE_KEYS.webAssets);
if (wa && wa.value) webAssets = JSON.parse(wa.value) || {}; if (wa && wa.value) webAssets = JSON.parse(wa.value) || {};
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
let webNews = []; let webNews = [];
try { try {
const wn = await storageGet(STORAGE_KEYS.webNews); const wn = await storageGet(STORAGE_KEYS.webNews);
if (wn && wn.value) webNews = JSON.parse(wn.value) || []; if (wn && wn.value) webNews = JSON.parse(wn.value) || [];
} catch (e) {} } catch (e) { if (!e.notFound) failed.push(e.message); }
if (failed.length) { const err = new Error(failed[0]); err.loadFailed = true; throw err; }
return { customers, products, quotes, sellerProfiles, certifications, productCategories, packingLists, catalogDocs, jmsLines, backPanelTemplates, suppliers, webDetailTemplates, webAssets, webNews }; return { customers, products, quotes, sellerProfiles, certifications, productCategories, packingLists, catalogDocs, jmsLines, backPanelTemplates, suppliers, webDetailTemplates, webAssets, webNews };
} }
@ -2297,6 +2300,7 @@ export { LoginScreen, MainApp };
function MainApp({ username, onLogout }) { function MainApp({ username, onLogout }) {
const [loaded, setLoaded] = useState(false); const [loaded, setLoaded] = useState(false);
const [loadError, setLoadError] = useState('');
const [customers, setCustomers] = useState([]); const [customers, setCustomers] = useState([]);
const [products, setProducts] = useState([]); const [products, setProducts] = useState([]);
const [view, setView] = useState('monitor'); const [view, setView] = useState('monitor');
@ -2406,7 +2410,12 @@ function MainApp({ username, onLogout }) {
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const data = await loadAll(); let data;
// 读取失败:停在错误页,不进入系统(loaded 保持 false,所以不会触发任何自动保存)
for (let i = 0; i < 3 && !data; i++) {
try { data = await loadAll(); }
catch (e) { if (i < 2) await new Promise(r => setTimeout(r, 2000)); else { setLoadError(e.message || '读取失败'); return; } }
}
const normalized = normalizeProductTaxonomy(data.products, data.productCategories); const normalized = normalizeProductTaxonomy(data.products, data.productCategories);
setCustomers(data.customers); setCustomers(data.customers);
setProducts(normalized.products); setProducts(normalized.products);
@ -3527,6 +3536,17 @@ function MainApp({ username, onLogout }) {
if (editingCertId === cert.id) setEditingCertId(null); if (editingCertId === cert.id) setEditingCertId(null);
} }
if (!loaded && loadError) {
return (
<div className="crm-root" style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: 320, padding: 20 }}>
<div style={{ maxWidth: 420, textAlign: 'center', fontSize: 14, lineHeight: 1.7 }}>
<div style={{ fontWeight: 700, marginBottom: 6 }}>数据读取失败</div>
<div style={{ color: 'var(--ink-soft)', marginBottom: 12 }}>服务器可能正在重启(刚部署完常见)。为了保护数据,这次没有进入系统,也没有保存任何内容。请稍等几秒后重新加载。<br /><span style={{ fontSize: 12 }}>{loadError}</span></div>
<button className="btn btn-primary" onClick={() => window.location.reload()}>重新加载</button>
</div>
</div>
);
}
if (!loaded) { if (!loaded) {
return ( return (
<div className="crm-root" style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: 320 }}> <div className="crm-root" style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: 320 }}>