feat: cloud sync with WeChat CloudBase — data survives app deletion
Problem: All data stored in wx.StorageSync is permanently lost when
user deletes the mini-program or clears cache.
Solution: Sync data to WeChat CloudBase (wx.cloud.database).
- Graceful degradation: if cloud env isn't configured, all local
storage works exactly as before.
- On app launch: if local records are empty, pull from cloud.
- On writes: push to cloud (debounced 2s) after saveRecord,
deleteRecord, saveSettings, updateStreak, setTheme.
- Clear data in settings also clears cloud.
Files:
- utils/cloud.js: cloud sync wrapper (init, pullAll, pushAll, clearAll)
- app.js: cloud.init() + restore on fresh install
- utils/storage.js: cloud.pushAll() after every mutation
- utils/theme.js: cloud.pushAll() after setTheme
- pages/settings/settings.js: cloud.clearAll() when clearing data
- project.config.json: cloudfunctionRoot added
Setup required (one-time in WeChat DevTools):
1. Open 'Cloud Development' panel → enable CloudBase
2. Create environment → copy env ID
3. In app.js, change wx.cloud.init() → wx.cloud.init({ env: 'YOUR-ENV-ID' })
4. Create 'plank_data' collection in cloud database
5. Set collection permission to 'Only creator can read/write'
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { formatDate } = require('./util')
|
||||
const cloud = require('./cloud')
|
||||
|
||||
const RECORDS_KEY = 'training_records'
|
||||
const SETTINGS_KEY = 'user_settings'
|
||||
@@ -14,6 +15,7 @@ const saveRecord = (record) => {
|
||||
records[month].push(record)
|
||||
records[month].sort((a, b) => b.date.localeCompare(a.date))
|
||||
wx.setStorageSync(RECORDS_KEY, records)
|
||||
cloud.pushAll()
|
||||
}
|
||||
|
||||
const deleteRecord = (recordId) => {
|
||||
@@ -23,6 +25,7 @@ const deleteRecord = (recordId) => {
|
||||
if (records[month].length === 0) delete records[month]
|
||||
})
|
||||
wx.setStorageSync(RECORDS_KEY, records)
|
||||
cloud.pushAll()
|
||||
}
|
||||
|
||||
const getRecordsByMonth = (month) => {
|
||||
@@ -55,6 +58,7 @@ const getSettings = () => wx.getStorageSync(SETTINGS_KEY) || {
|
||||
|
||||
const saveSettings = (settings) => {
|
||||
wx.setStorageSync(SETTINGS_KEY, settings)
|
||||
cloud.pushAll()
|
||||
}
|
||||
|
||||
const getStreak = () => wx.getStorageSync(STREAK_KEY) || { count: 0, lastDate: '' }
|
||||
@@ -73,6 +77,7 @@ const updateStreak = (date) => {
|
||||
}
|
||||
streak.lastDate = today
|
||||
wx.setStorageSync(STREAK_KEY, streak)
|
||||
cloud.pushAll()
|
||||
return streak
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user