Files
wxserver/pages/settings/settings.js
T
yuming 192867a8d5
部署到群晖 / deploy (push) Successful in 41s
版本号显示 v2.1.5 → v2.1.6
带农历日期显示的版本,重新上传体验版。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-02 14:21:11 +08:00

117 lines
3.1 KiB
JavaScript

// 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.6'
},
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: (res) => {
if (res.confirm) {
const success = storage.clearAllData()
if (success) {
wx.showToast({
title: '已清空',
icon: 'success'
})
setTimeout(() => {
wx.reLaunch({
url: '/pages/index/index'
})
}, 1500)
}
}
}
})
}
})