Files
wx_pbzc/utils/storage.js
T
lc a04c017e37 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'
2026-06-04 17:13:35 +08:00

123 lines
2.9 KiB
JavaScript

const { formatDate } = require('./util')
const cloud = require('./cloud')
const RECORDS_KEY = 'training_records'
const SETTINGS_KEY = 'user_settings'
const STREAK_KEY = 'current_streak'
const getRecords = () => wx.getStorageSync(RECORDS_KEY) || {}
const saveRecord = (record) => {
record.id = Date.now()
const records = getRecords()
const month = record.date.substring(0, 7)
if (!records[month]) records[month] = []
records[month].push(record)
records[month].sort((a, b) => b.date.localeCompare(a.date))
wx.setStorageSync(RECORDS_KEY, records)
cloud.pushAll()
}
const deleteRecord = (recordId) => {
const records = getRecords()
Object.keys(records).forEach((month) => {
records[month] = records[month].filter((r) => r.id !== recordId)
if (records[month].length === 0) delete records[month]
})
wx.setStorageSync(RECORDS_KEY, records)
cloud.pushAll()
}
const getRecordsByMonth = (month) => {
const records = getRecords()
return records[month] || []
}
const getTotalStats = () => {
const records = getRecords()
let totalDuration = 0
let totalSessions = 0
let maxDuration = 0
Object.keys(records).forEach((key) => {
records[key].forEach((r) => {
totalDuration += r.duration
totalSessions++
if (r.duration > maxDuration) maxDuration = r.duration
})
})
return { totalDuration, totalSessions, maxDuration }
}
const getSettings = () => wx.getStorageSync(SETTINGS_KEY) || {
planId: 'beginner',
dailyReminder: true,
reminderTime: '08:00',
voiceGuide: true,
vibrate: true
}
const saveSettings = (settings) => {
wx.setStorageSync(SETTINGS_KEY, settings)
cloud.pushAll()
}
const getStreak = () => wx.getStorageSync(STREAK_KEY) || { count: 0, lastDate: '' }
const updateStreak = (date) => {
const streak = getStreak()
const today = date || getToday()
const yesterday = getDateOffset(today, -1)
if (streak.lastDate === today) return streak
if (streak.lastDate === yesterday) {
streak.count += 1
} else {
streak.count = 1
}
streak.lastDate = today
wx.setStorageSync(STREAK_KEY, streak)
cloud.pushAll()
return streak
}
const getToday = () => formatDate(new Date())
const getDateOffset = (dateStr, offset) => {
const d = new Date(dateStr)
d.setDate(d.getDate() + offset)
return formatDate(d)
}
const getTodayRecord = () => {
const today = getToday()
const records = getRecords()
let totalDuration = 0
let hasRecord = false
Object.keys(records).forEach((month) => {
records[month].forEach((r) => {
if (r.date === today) {
totalDuration += r.duration
hasRecord = true
}
})
})
return hasRecord ? { date: today, duration: totalDuration } : null
}
module.exports = {
getRecords,
saveRecord,
deleteRecord,
getRecordsByMonth,
getTotalStats,
getSettings,
saveSettings,
getStreak,
updateStreak,
getToday,
formatDate,
getDateOffset,
getTodayRecord
}