From fa8d8e7b4b2c3c5e6429afef5b3cba03153aecbd Mon Sep 17 00:00:00 2001 From: cnliucheng Date: Thu, 4 Jun 2026 17:29:13 +0800 Subject: [PATCH] fix: seed existing local data to cloud on first launch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously cloud sync only triggered on writes. If the user already had training records before cloud was enabled, they'd never be pushed — leaving the database empty until the next training session. Now onLaunch handles both cases: - Local empty → restore from cloud (recovery after deletion) - Local has data → check cloud; if cloud empty, push existing data up Also extract _restoreFromCloud to reduce nesting. --- app.js | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 17a552a..a8ca155 100644 --- a/app.js +++ b/app.js @@ -24,31 +24,45 @@ App({ this.globalData.theme = themeMod.getCurrentTheme() - // If local records are empty, try pulling from cloud + // Sync with cloud: pull on empty local, push on empty cloud const records = wx.getStorageSync('training_records') if (!records || Object.keys(records).length === 0) { - cloud.pullAll().then((cloudData) => { - if (!cloudData) return - try { - if (cloudData.records && Object.keys(cloudData.records).length > 0) { - wx.setStorageSync('training_records', cloudData.records) - } - if (cloudData.settings) { - wx.setStorageSync('user_settings', cloudData.settings) - } - if (cloudData.streak) { - wx.setStorageSync('current_streak', cloudData.streak) - } - if (cloudData.themeId) { - themeMod.setTheme(cloudData.themeId) - } - } catch (e) { - console.error('Cloud restore failed:', e) + // Case 1: Fresh install — restore from cloud + cloud.pullAll().then(cloudData => this._restoreFromCloud(cloudData)) + } else { + // Case 2: Has local data — check if cloud needs seeding + cloud.pullAll().then(cloudData => { + if (!cloudData) { + // Cloud empty: push existing local data up + cloud.pushAll() + } else { + // Both have data — trust local (latest) and re-push + cloud.pushAll() } }) } }, + _restoreFromCloud(cloudData) { + if (!cloudData) return + try { + if (cloudData.records && Object.keys(cloudData.records).length > 0) { + wx.setStorageSync('training_records', cloudData.records) + } + if (cloudData.settings) { + wx.setStorageSync('user_settings', cloudData.settings) + } + if (cloudData.streak) { + wx.setStorageSync('current_streak', cloudData.streak) + } + if (cloudData.themeId) { + themeMod.setTheme(cloudData.themeId) + } + } catch (e) { + console.error('Cloud restore failed:', e) + } + }, + globalData: { planId: 'beginner', theme: null