133 lines
3.3 KiB
JavaScript
133 lines
3.3 KiB
JavaScript
var RECORDS_KEY = 'training_records'
|
|
var SETTINGS_KEY = 'user_settings'
|
|
var STREAK_KEY = 'current_streak'
|
|
|
|
function getRecords() {
|
|
return wx.getStorageSync(RECORDS_KEY) || {}
|
|
}
|
|
|
|
function saveRecord(record) {
|
|
record.id = Date.now()
|
|
var records = getRecords()
|
|
var month = record.date.substring(0, 7)
|
|
if (!records[month]) records[month] = []
|
|
records[month].push(record)
|
|
records[month].sort(function (a, b) { return b.date.localeCompare(a.date) })
|
|
wx.setStorageSync(RECORDS_KEY, records)
|
|
}
|
|
|
|
function deleteRecord(recordId) {
|
|
var records = getRecords()
|
|
Object.keys(records).forEach(function (month) {
|
|
records[month] = records[month].filter(function (r) { return r.id !== recordId })
|
|
if (records[month].length === 0) delete records[month]
|
|
})
|
|
wx.setStorageSync(RECORDS_KEY, records)
|
|
}
|
|
|
|
function getRecordsByMonth(month) {
|
|
var records = getRecords()
|
|
return records[month] || []
|
|
}
|
|
|
|
function getTotalStats() {
|
|
var records = getRecords()
|
|
var totalDuration = 0
|
|
var totalSessions = 0
|
|
var maxDuration = 0
|
|
Object.keys(records).forEach(function (key) {
|
|
var list = records[key]
|
|
list.forEach(function (r) {
|
|
totalDuration += r.duration
|
|
totalSessions++
|
|
if (r.duration > maxDuration) maxDuration = r.duration
|
|
})
|
|
})
|
|
return { totalDuration: totalDuration, totalSessions: totalSessions, maxDuration: maxDuration }
|
|
}
|
|
|
|
function getSettings() {
|
|
return wx.getStorageSync(SETTINGS_KEY) || {
|
|
planId: 'beginner',
|
|
dailyReminder: true,
|
|
reminderTime: '08:00',
|
|
voiceGuide: true,
|
|
vibrate: true
|
|
}
|
|
}
|
|
|
|
function saveSettings(settings) {
|
|
wx.setStorageSync(SETTINGS_KEY, settings)
|
|
}
|
|
|
|
function getStreak() {
|
|
return wx.getStorageSync(STREAK_KEY) || { count: 0, lastDate: '' }
|
|
}
|
|
|
|
function updateStreak(date) {
|
|
var streak = getStreak()
|
|
var today = date || getToday()
|
|
var 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)
|
|
return streak
|
|
}
|
|
|
|
function getToday() {
|
|
return formatDate(new Date())
|
|
}
|
|
|
|
function formatDate(date) {
|
|
var y = date.getFullYear()
|
|
var m = String(date.getMonth() + 1)
|
|
if (m.length < 2) m = '0' + m
|
|
var d = String(date.getDate())
|
|
if (d.length < 2) d = '0' + d
|
|
return y + '-' + m + '-' + d
|
|
}
|
|
|
|
function getDateOffset(dateStr, offset) {
|
|
var d = new Date(dateStr)
|
|
d.setDate(d.getDate() + offset)
|
|
return formatDate(d)
|
|
}
|
|
|
|
function getTodayRecord() {
|
|
var today = getToday()
|
|
var month = today.substring(0, 7)
|
|
var records = getRecordsByMonth(month)
|
|
var totalDuration = 0
|
|
var hasRecord = false
|
|
for (var i = 0; i < records.length; i++) {
|
|
if (records[i].date === today) {
|
|
totalDuration += records[i].duration
|
|
hasRecord = true
|
|
}
|
|
}
|
|
return hasRecord ? { date: today, duration: totalDuration } : null
|
|
}
|
|
|
|
module.exports = {
|
|
getRecords: getRecords,
|
|
saveRecord: saveRecord,
|
|
deleteRecord: deleteRecord,
|
|
getRecordsByMonth: getRecordsByMonth,
|
|
getTotalStats: getTotalStats,
|
|
getSettings: getSettings,
|
|
saveSettings: saveSettings,
|
|
getStreak: getStreak,
|
|
updateStreak: updateStreak,
|
|
getToday: getToday,
|
|
formatDate: formatDate,
|
|
getDateOffset: getDateOffset,
|
|
getTodayRecord: getTodayRecord
|
|
}
|