From b808a5c57e9dbd37b13a485320021f5bbf2b54c5 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 21 Oct 2026 11:33:40 +0800 Subject: [PATCH] Update App.jsx via upload script - 2026-10-21 11:33:35 --- App.jsx | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/App.jsx b/App.jsx index 84581c9..a1df8e1 100644 --- a/App.jsx +++ b/App.jsx @@ -1545,6 +1545,8 @@ function normalizeProductTaxonomy(products, categories) { /* -------------------------------- storage ------------------------------ */ async function loadAll() { + // 读取失败(服务器重启中、网络断了)和"还没有数据"必须区分:读取失败时绝不能把空列表当成真实数据再存回服务器 + const failed = []; let customers = []; let products = []; let quotes = []; @@ -1556,65 +1558,66 @@ async function loadAll() { try { const c = await storageGet(STORAGE_KEYS.customers); if (c && c.value) customers = JSON.parse(c.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const p = await storageGet(STORAGE_KEYS.products); if (p && p.value) products = JSON.parse(p.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const q = await storageGet(STORAGE_KEYS.quotes); if (q && q.value) quotes = JSON.parse(q.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const sp = await storageGet(STORAGE_KEYS.sellerProfiles); if (sp && sp.value) sellerProfiles = JSON.parse(sp.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const cf = await storageGet(STORAGE_KEYS.certifications); if (cf && cf.value) certifications = JSON.parse(cf.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const pc = await storageGet(STORAGE_KEYS.productCategories); if (pc && pc.value) productCategories = JSON.parse(pc.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const pl = await storageGet(STORAGE_KEYS.packingLists); if (pl && pl.value) packingLists = JSON.parse(pl.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } try { const cd = await storageGet(STORAGE_KEYS.catalogDocs); if (cd && cd.value) catalogDocs = JSON.parse(cd.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } let jmsLines = []; try { const jl = await storageGet(STORAGE_KEYS.jmsLines); if (jl && jl.value) jmsLines = JSON.parse(jl.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } let backPanelTemplates = []; try { const bpt = await storageGet(STORAGE_KEYS.backPanelTemplates); if (bpt && bpt.value) backPanelTemplates = JSON.parse(bpt.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } let suppliers = []; try { const sup = await storageGet(STORAGE_KEYS.suppliers); if (sup && sup.value) suppliers = JSON.parse(sup.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } let webDetailTemplates = []; try { const wdt = await storageGet(STORAGE_KEYS.webDetailTemplates); if (wdt && wdt.value) webDetailTemplates = JSON.parse(wdt.value); - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } let webAssets = {}; try { const wa = await storageGet(STORAGE_KEYS.webAssets); if (wa && wa.value) webAssets = JSON.parse(wa.value) || {}; - } catch (e) {} + } catch (e) { if (!e.notFound) failed.push(e.message); } let webNews = []; try { const wn = await storageGet(STORAGE_KEYS.webNews); 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 }; } @@ -2297,6 +2300,7 @@ export { LoginScreen, MainApp }; function MainApp({ username, onLogout }) { const [loaded, setLoaded] = useState(false); + const [loadError, setLoadError] = useState(''); const [customers, setCustomers] = useState([]); const [products, setProducts] = useState([]); const [view, setView] = useState('monitor'); @@ -2406,7 +2410,12 @@ function MainApp({ username, onLogout }) { useEffect(() => { (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); setCustomers(data.customers); setProducts(normalized.products); @@ -3527,6 +3536,17 @@ function MainApp({ username, onLogout }) { if (editingCertId === cert.id) setEditingCertId(null); } + if (!loaded && loadError) { + return ( +
+
+
数据读取失败
+
服务器可能正在重启(刚部署完常见)。为了保护数据,这次没有进入系统,也没有保存任何内容。请稍等几秒后重新加载。
{loadError}
+ +
+
+ ); + } if (!loaded) { return (