From dfe169d7d4fa69eb0947d382daf9dc8b7b2e374f Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 4 Nov 2026 09:41:40 +0800 Subject: [PATCH] Update App.jsx via upload script - 2026-11-04 09:41:31 --- App.jsx | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/App.jsx b/App.jsx index 379927d..e37144c 100644 --- a/App.jsx +++ b/App.jsx @@ -10,7 +10,7 @@ import { import { MicUploadPage } from './mic-upload/MicUploadPage'; import { ImageToolPage } from './mic-upload/ImageToolPage'; import { - storageGet, storageSet, uploadFile, deleteFile, + storageGet, storageSet, hasUnsavedChanges, uploadFile, deleteFile, authCheck, authLogin, authLogout, authChangePassword, getAiConfig, saveAiConfig, deleteAiConfig, testAiConfig, parseCatalogFiles, translateTerms, @@ -2474,6 +2474,14 @@ function MainApp({ username, onLogout }) { // 保存出错:版本冲突(别的页面改过)或网络 / 服务器错误,都要让人看见,不能悄悄丢修改 const [saveIssue, setSaveIssue] = useState(null); // { conflict, message } const reportSaveError = (e) => setSaveIssue(prev => (prev && prev.conflict) ? prev : { conflict: !!(e && e.conflict), message: (e && e.message) || '未知错误', key: e && e.key }); + // 之前保存失败的那份数据这次存上了:去掉提示条(冲突提示要刷新页面才能解决,不自动去掉) + const clearSaveIssue = (key) => setSaveIssue(prev => (prev && !prev.conflict && prev.key === key ? null : prev)); + // 还有没存上的修改(正在保存,或者保存失败了)时,关闭 / 刷新页面先弹浏览器的确认框,免得修改悄悄丢掉 + useEffect(() => { + const onLeave = (e) => { if (hasUnsavedChanges()) { e.preventDefault(); e.returnValue = ''; return ''; } }; + window.addEventListener('beforeunload', onLeave); + return () => window.removeEventListener('beforeunload', onLeave); + }, []); const [customers, setCustomers] = useState([]); const [products, setProducts] = useState([]); const [view, setView] = useState('monitor'); @@ -2635,22 +2643,22 @@ function MainApp({ username, onLogout }) { setLoaded(true); })(); }, []); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.customers, JSON.stringify(customers)).catch(reportSaveError); }, [customers, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.products, JSON.stringify(products)).catch(reportSaveError); }, [products, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.quotes, JSON.stringify(quotes)).catch(reportSaveError); }, [quotes, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.sellerProfiles, JSON.stringify(sellerProfiles)).catch(reportSaveError); }, [sellerProfiles, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.certifications, JSON.stringify(certifications)).catch(reportSaveError); }, [certifications, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.productCategories, JSON.stringify(productCategories)).catch(reportSaveError); }, [productCategories, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.packingLists, JSON.stringify(packingLists)).catch(reportSaveError); }, [packingLists, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.catalogDocs, JSON.stringify(catalogDocs)).catch(reportSaveError); }, [catalogDocs, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.jmsLines, JSON.stringify(jmsLines)).catch(reportSaveError); }, [jmsLines, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.backPanelTemplates, JSON.stringify(backPanelTemplates)).catch(reportSaveError); }, [backPanelTemplates, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.suppliers, JSON.stringify(suppliers)).catch(reportSaveError); }, [suppliers, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webDetailTemplates, JSON.stringify(webDetailTemplates)).catch(reportSaveError); }, [webDetailTemplates, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webPackSpecs, JSON.stringify(webPackSpecs)).catch(reportSaveError); }, [webPackSpecs, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webAssets, JSON.stringify(webAssets)).catch(reportSaveError); }, [webAssets, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webNews, JSON.stringify(webNews)).catch(reportSaveError); }, [webNews, loaded]); - useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.orders, JSON.stringify(orders)).catch(reportSaveError); }, [orders, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.customers, JSON.stringify(customers)).then(() => clearSaveIssue(STORAGE_KEYS.customers)).catch(reportSaveError); }, [customers, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.products, JSON.stringify(products)).then(() => clearSaveIssue(STORAGE_KEYS.products)).catch(reportSaveError); }, [products, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.quotes, JSON.stringify(quotes)).then(() => clearSaveIssue(STORAGE_KEYS.quotes)).catch(reportSaveError); }, [quotes, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.sellerProfiles, JSON.stringify(sellerProfiles)).then(() => clearSaveIssue(STORAGE_KEYS.sellerProfiles)).catch(reportSaveError); }, [sellerProfiles, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.certifications, JSON.stringify(certifications)).then(() => clearSaveIssue(STORAGE_KEYS.certifications)).catch(reportSaveError); }, [certifications, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.productCategories, JSON.stringify(productCategories)).then(() => clearSaveIssue(STORAGE_KEYS.productCategories)).catch(reportSaveError); }, [productCategories, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.packingLists, JSON.stringify(packingLists)).then(() => clearSaveIssue(STORAGE_KEYS.packingLists)).catch(reportSaveError); }, [packingLists, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.catalogDocs, JSON.stringify(catalogDocs)).then(() => clearSaveIssue(STORAGE_KEYS.catalogDocs)).catch(reportSaveError); }, [catalogDocs, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.jmsLines, JSON.stringify(jmsLines)).then(() => clearSaveIssue(STORAGE_KEYS.jmsLines)).catch(reportSaveError); }, [jmsLines, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.backPanelTemplates, JSON.stringify(backPanelTemplates)).then(() => clearSaveIssue(STORAGE_KEYS.backPanelTemplates)).catch(reportSaveError); }, [backPanelTemplates, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.suppliers, JSON.stringify(suppliers)).then(() => clearSaveIssue(STORAGE_KEYS.suppliers)).catch(reportSaveError); }, [suppliers, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webDetailTemplates, JSON.stringify(webDetailTemplates)).then(() => clearSaveIssue(STORAGE_KEYS.webDetailTemplates)).catch(reportSaveError); }, [webDetailTemplates, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webPackSpecs, JSON.stringify(webPackSpecs)).then(() => clearSaveIssue(STORAGE_KEYS.webPackSpecs)).catch(reportSaveError); }, [webPackSpecs, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webAssets, JSON.stringify(webAssets)).then(() => clearSaveIssue(STORAGE_KEYS.webAssets)).catch(reportSaveError); }, [webAssets, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.webNews, JSON.stringify(webNews)).then(() => clearSaveIssue(STORAGE_KEYS.webNews)).catch(reportSaveError); }, [webNews, loaded]); + useEffect(() => { if (loaded) storageSet(STORAGE_KEYS.orders, JSON.stringify(orders)).then(() => clearSaveIssue(STORAGE_KEYS.orders)).catch(reportSaveError); }, [orders, loaded]); // 侧栏“订单管理”上的数字:到期未处理的订单提醒(每分钟刷新一次) const [ordTick, setOrdTick] = useState(0); useEffect(() => { const t = setInterval(() => setOrdTick(x => x + 1), 60000); return () => clearInterval(t); }, []); @@ -3766,7 +3774,7 @@ function MainApp({ username, onLogout }) {
{saveIssue.conflict ? `这份数据(${SAVE_KEY_LABEL[saveIssue.key] || saveIssue.key || '数据'})已经在别的页面或设备上改过。为了不互相覆盖,这个页面之后的修改没有保存,请刷新页面后再改。` - : `保存失败:${saveIssue.message}。刚才的修改可能没存上,请检查网络后刷新页面确认。`} + : `${SAVE_KEY_LABEL[saveIssue.key] || saveIssue.key || '数据'}保存失败:${saveIssue.message}。修改还在这个页面里,没有存到服务器——先别刷新或关闭页面,问题解决后再随便改一下就会重新保存。`} {!saveIssue.conflict && }