// settings.js const storage = require('../../utils/storage') const LAST_BACKUP_KEY = 'lastBackupAt' function formatRelativeTime(ts) { if (!ts) return '从未备份' const diff = Date.now() - ts if (diff < 60 * 1000) return '刚刚' if (diff < 60 * 60 * 1000) return Math.floor(diff / 60000) + ' 分钟前' if (diff < 24 * 60 * 60 * 1000) return Math.floor(diff / 3600000) + ' 小时前' if (diff < 30 * 24 * 60 * 60 * 1000) return Math.floor(diff / 86400000) + ' 天前' const d = new Date(ts) return (d.getMonth() + 1) + '月' + d.getDate() + '日' } Page({ data: { personsCount: 0, anniversariesCount: 0, lastBackupText: '从未备份', version: 'v2.1.7' }, onLoad() { this.loadSummary() }, onShow() { this.loadSummary() }, loadSummary() { const persons = storage.getPersons() const anniversaries = storage.getAnniversaries() const lastBackupAt = wx.getStorageSync(LAST_BACKUP_KEY) || 0 this.setData({ personsCount: persons.length, anniversariesCount: anniversaries.length, lastBackupText: formatRelativeTime(lastBackupAt) }) }, onExportData() { const data = storage.exportData() const jsonStr = JSON.stringify(data, null, 2) wx.setClipboardData({ data: jsonStr, success: () => { wx.setStorageSync(LAST_BACKUP_KEY, Date.now()) this.loadSummary() wx.showToast({ title: '数据已复制到剪贴板', icon: 'success' }) } }) }, onImportData() { wx.showModal({ title: '导入数据', content: '将从剪贴板读取数据并导入。注意:导入会覆盖现有数据,请先备份!', confirmText: '确认导入', cancelText: '取消', success: (res) => { if (res.confirm) { wx.getClipboardData({ success: (res) => { try { const data = JSON.parse(res.data) const result = storage.importData(data) if (result.success) { wx.showToast({ title: '导入成功', icon: 'success' }) setTimeout(() => wx.reLaunch({ url: '/pages/index/index' }), 1500) } else { wx.showToast({ title: result.error || '导入失败,请检查数据格式', icon: 'none' }) } } catch (e) { wx.showToast({ title: '数据格式错误', icon: 'none' }) } } }) } } }) }, onClearData() { wx.showModal({ title: '确认清空', content: '本地和云端备份都会被清空,此操作不可恢复!', confirmText: '确认清空', confirmColor: '#C8412F', success: async (res) => { if (!res.confirm) return // 先清云端再清本地:顺序反了的话,本地清空后下次启动会从云端把数据整份拉回来 wx.showLoading({ title: '正在清空...', mask: true }) const cloud = await storage.clearCloudData() wx.hideLoading() if (!cloud.success) { // 云端没清干净就不动本地,否则会出现「清了又自己回来」的诡异现象 wx.showModal({ title: '清空失败', content: `云端数据未能清除(${cloud.error}),本地数据已保留。请检查网络后重试。`, showCancel: false }) return } if (storage.clearAllData()) { wx.showToast({ title: '已清空', icon: 'success' }) setTimeout(() => { wx.reLaunch({ url: '/pages/index/index' }) }, 1500) } else { wx.showToast({ title: '本地清空失败,请重试', icon: 'none' }) } } }) } })