refactor: ES6+ modernization + 2 bugfixes

- var→const/let, function→arrow/class across all 13 JS files
- Timer: prototype→class with ES6 getters
- plan days: IIFE→Array.from declarative generation
- storage/plan: unified formatDate via util.js
- Bugfix: getPlanDay now filters by planId (plan switch accuracy)
- Bugfix: getTodayRecord searches all months (cross-month boundary)
- WXML/WXSS unchanged; public API unchanged
This commit is contained in:
2026-06-04 16:29:25 +08:00
parent ff2700c067
commit 8c7f633541
13 changed files with 404 additions and 494 deletions
+65 -80
View File
@@ -1,73 +1,68 @@
var RECORDS_KEY = 'training_records'
var SETTINGS_KEY = 'user_settings'
var STREAK_KEY = 'current_streak'
const { formatDate } = require('./util')
function getRecords() {
return wx.getStorageSync(RECORDS_KEY) || {}
}
const RECORDS_KEY = 'training_records'
const SETTINGS_KEY = 'user_settings'
const STREAK_KEY = 'current_streak'
function saveRecord(record) {
const getRecords = () => wx.getStorageSync(RECORDS_KEY) || {}
const saveRecord = (record) => {
record.id = Date.now()
var records = getRecords()
var month = record.date.substring(0, 7)
const records = getRecords()
const 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) })
records[month].sort((a, b) => 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 })
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)
}
function getRecordsByMonth(month) {
var records = getRecords()
const getRecordsByMonth = (month) => {
const 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) {
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: totalDuration, totalSessions: totalSessions, maxDuration: maxDuration }
return { totalDuration, totalSessions, maxDuration }
}
function getSettings() {
return wx.getStorageSync(SETTINGS_KEY) || {
planId: 'beginner',
dailyReminder: true,
reminderTime: '08:00',
voiceGuide: true,
vibrate: true
}
const getSettings = () => wx.getStorageSync(SETTINGS_KEY) || {
planId: 'beginner',
dailyReminder: true,
reminderTime: '08:00',
voiceGuide: true,
vibrate: true
}
function saveSettings(settings) {
const saveSettings = (settings) => {
wx.setStorageSync(SETTINGS_KEY, settings)
}
function getStreak() {
return wx.getStorageSync(STREAK_KEY) || { count: 0, lastDate: '' }
}
const getStreak = () => wx.getStorageSync(STREAK_KEY) || { count: 0, lastDate: '' }
function updateStreak(date) {
var streak = getStreak()
var today = date || getToday()
var yesterday = getDateOffset(today, -1)
const updateStreak = (date) => {
const streak = getStreak()
const today = date || getToday()
const yesterday = getDateOffset(today, -1)
if (streak.lastDate === today) return streak
@@ -81,52 +76,42 @@ function updateStreak(date) {
return streak
}
function getToday() {
return formatDate(new Date())
}
const getToday = () => 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)
const getDateOffset = (dateStr, offset) => {
const 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
}
}
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: 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
getRecords,
saveRecord,
deleteRecord,
getRecordsByMonth,
getTotalStats,
getSettings,
saveSettings,
getStreak,
updateStreak,
getToday,
formatDate,
getDateOffset,
getTodayRecord
}