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
+27 -32
View File
@@ -1,7 +1,7 @@
var storage = require('../../utils/storage')
var planMod = require('../../utils/plan')
var util = require('../../utils/util')
var themeMod = require('../../utils/theme')
const storage = require('../../utils/storage')
const planMod = require('../../utils/plan')
const util = require('../../utils/util')
const themeMod = require('../../utils/theme')
Page({
data: {
@@ -22,38 +22,33 @@ Page({
customInput: ''
},
onLoad: function () {
onLoad() {
this.refresh()
},
onShow: function () {
onShow() {
themeMod.applyThemeToPage(this)
try {
var tb = this.getTabBar()
const tb = this.getTabBar()
if (tb) tb.setData({ selected: 0 })
} catch (e) {}
this.refresh()
},
onPullDownRefresh: function () {
onPullDownRefresh() {
this.refresh()
wx.stopPullDownRefresh()
},
refresh: function () {
var settings = storage.getSettings()
var streak = storage.getStreak()
var todayRecord = storage.getTodayRecord()
refresh() {
const settings = storage.getSettings()
const streak = storage.getStreak()
const todayRecord = storage.getTodayRecord()
var records = []
var allRecords = storage.getRecords()
Object.keys(allRecords).forEach(function (key) {
records = records.concat(allRecords[key])
})
var plan = planMod.getPlan(settings.planId)
var planDay = planMod.getPlanDay(settings.planId, records)
var target = planMod.getTodayTarget(settings.planId, Math.min(planDay, plan.totalDays))
const allRecords = Object.values(storage.getRecords()).flat()
const plan = planMod.getPlan(settings.planId)
const planDay = planMod.getPlanDay(settings.planId, allRecords)
const target = planMod.getTodayTarget(settings.planId, Math.min(planDay, plan.totalDays))
this.setData({
todayTarget: target,
@@ -68,37 +63,37 @@ Page({
})
},
onStartTrain: function () {
onStartTrain() {
wx.navigateTo({ url: '/pages/timer/timer' })
},
onFreeTrain: function () {
onFreeTrain() {
this.setData({ showPicker: true, customInput: '', customDuration: 60 })
},
onClosePicker: function () {
onClosePicker() {
this.setData({ showPicker: false })
},
onSelectPreset: function (e) {
var val = e.currentTarget.dataset.value
onSelectPreset(e) {
const val = e.currentTarget.dataset.value
this.setData({ customDuration: val, customInput: '' })
},
onCustomInput: function (e) {
var val = parseInt(e.detail.value) || 0
onCustomInput(e) {
const val = parseInt(e.detail.value) || 0
this.setData({ customInput: e.detail.value, customDuration: val })
},
onConfirmFree: function () {
var dur = this.data.customDuration
onConfirmFree() {
const dur = this.data.customDuration
if (!dur || dur <= 0) {
wx.showToast({ title: '请输入有效时长', icon: 'none' })
return
}
this.setData({ showPicker: false })
wx.navigateTo({ url: '/pages/timer/timer?free=' + dur })
wx.navigateTo({ url: `/pages/timer/timer?free=${dur}` })
},
noop: function () {}
noop() {}
})
+41 -57
View File
@@ -1,6 +1,6 @@
var storage = require('../../utils/storage')
var util = require('../../utils/util')
var themeMod = require('../../utils/theme')
const storage = require('../../utils/storage')
const util = require('../../utils/util')
const themeMod = require('../../utils/theme')
Page({
data: {
@@ -19,36 +19,31 @@ Page({
dayDetail: {}
},
onLoad: function () {
onLoad() {
this.refresh()
},
onShow: function () {
onShow() {
themeMod.applyThemeToPage(this)
try {
var tb = this.getTabBar()
const tb = this.getTabBar()
if (tb) tb.setData({ selected: 1 })
} catch (e) {}
this.refresh()
},
onPullDownRefresh: function () {
onPullDownRefresh() {
this.refresh()
wx.stopPullDownRefresh()
},
refresh: function () {
var stats = storage.getTotalStats()
var monthKey = this.data.currentYear + '-' +
(this.data.currentMonth < 10 ? '0' : '') + this.data.currentMonth
var records = storage.getRecordsByMonth(monthKey)
refresh() {
const stats = storage.getTotalStats()
const monthKey = `${this.data.currentYear}-${String(this.data.currentMonth).padStart(2, '0')}`
const records = storage.getRecordsByMonth(monthKey)
var allRecords = []
var all = storage.getRecords()
Object.keys(all).forEach(function (key) {
allRecords = allRecords.concat(all[key])
})
allRecords.sort(function (a, b) { return b.date.localeCompare(a.date) })
const allRecords = Object.values(storage.getRecords()).flat()
allRecords.sort((a, b) => b.date.localeCompare(a.date))
this.setData({
totalDuration: stats.totalDuration,
@@ -61,48 +56,38 @@ Page({
})
},
onPrevMonth: function () {
var year = this.data.currentYear
var month = this.data.currentMonth
if (month === 1) {
year--
month = 12
onPrevMonth() {
let { currentYear, currentMonth } = this.data
if (currentMonth === 1) {
currentYear--
currentMonth = 12
} else {
month--
currentMonth--
}
this.setData({ currentYear: year, currentMonth: month })
this.setData({ currentYear, currentMonth })
this.refresh()
},
onNextMonth: function () {
var year = this.data.currentYear
var month = this.data.currentMonth
if (month === 12) {
year++
month = 1
onNextMonth() {
let { currentYear, currentMonth } = this.data
if (currentMonth === 12) {
currentYear++
currentMonth = 1
} else {
month++
currentMonth++
}
this.setData({ currentYear: year, currentMonth: month })
this.setData({ currentYear, currentMonth })
this.refresh()
},
onDayTap: function (e) {
var detail = e.detail
var dateStr = detail.year + '-' +
(detail.month < 10 ? '0' : '') + detail.month + '-' +
(detail.day < 10 ? '0' : '') + detail.day
onDayTap(e) {
const { year, month, day } = e.detail
const dateStr = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`
var allRecords = []
var all = storage.getRecords()
Object.keys(all).forEach(function (key) {
allRecords = allRecords.concat(all[key])
})
var sessions = allRecords.filter(function (r) { return r.date === dateStr })
var totalDur = 0
sessions.forEach(function (r) { totalDur += r.duration })
var calories = Math.round(totalDur * 0.068)
const allRecords = Object.values(storage.getRecords()).flat()
const sessions = allRecords.filter(r => r.date === dateStr)
const totalDur = sessions.reduce((sum, r) => sum + r.duration, 0)
const calories = Math.round(totalDur * 0.068)
this.setData({
showDayDetail: true,
@@ -111,27 +96,26 @@ Page({
sessions: sessions.length,
duration: totalDur,
durationText: util.formatDuration(totalDur),
calories: calories
calories
}
})
},
onCloseDayDetail: function () {
onCloseDayDetail() {
this.setData({ showDayDetail: false })
},
noop: function () {},
noop() {},
onDeleteRecord: function (e) {
var id = e.currentTarget.dataset.id
var self = this
onDeleteRecord(e) {
const id = e.currentTarget.dataset.id
wx.showModal({
title: '删除记录',
content: '确定要删除这条训练记录吗?',
success: function (res) {
success: (res) => {
if (res.confirm) {
storage.deleteRecord(id)
self.refresh()
this.refresh()
wx.showToast({ title: '已删除', icon: 'none', duration: 1200 })
}
}
+25 -27
View File
@@ -1,5 +1,5 @@
var storage = require('../../utils/storage')
var themeMod = require('../../utils/theme')
const storage = require('../../utils/storage')
const themeMod = require('../../utils/theme')
Page({
data: {
@@ -19,9 +19,9 @@ Page({
vibrate: true
},
onLoad: function () {
var s = storage.getSettings()
var theme = themeMod.getCurrentTheme()
onLoad() {
const s = storage.getSettings()
const theme = themeMod.getCurrentTheme()
themeMod.applyThemeToPage(this)
this.setData({
currentPlanId: s.planId,
@@ -33,70 +33,68 @@ Page({
})
},
onShow: function () {
onShow() {
try {
var tb = this.getTabBar()
const tb = this.getTabBar()
if (tb) tb.setData({ selected: 2 })
} catch (e) {}
themeMod.applyThemeToPage(this)
},
onSelectTheme: function (e) {
var id = e.currentTarget.dataset.id
var theme = themeMod.setTheme(id)
onSelectTheme(e) {
const id = e.currentTarget.dataset.id
const theme = themeMod.setTheme(id)
this.setData({ currentThemeId: id })
try {
var tb = this.getTabBar()
if (tb) {
tb.setData({ themeStyle: themeMod.getThemeStyle(theme) })
}
const tb = this.getTabBar()
if (tb) tb.setData({ themeStyle: themeMod.getThemeStyle(theme) })
} catch (e) {}
themeMod.applyThemeToPage(this)
},
onSelectPlan: function (e) {
var planId = e.currentTarget.dataset.id
var s = storage.getSettings()
onSelectPlan(e) {
const planId = e.currentTarget.dataset.id
const s = storage.getSettings()
s.planId = planId
storage.saveSettings(s)
this.setData({ currentPlanId: planId })
wx.showToast({ title: '计划已切换', icon: 'success', duration: 1200 })
},
onToggleReminder: function (e) {
var s = storage.getSettings()
onToggleReminder(e) {
const s = storage.getSettings()
s.dailyReminder = e.detail.value
storage.saveSettings(s)
this.setData({ dailyReminder: e.detail.value })
},
onReminderTimeChange: function (e) {
var s = storage.getSettings()
onReminderTimeChange(e) {
const s = storage.getSettings()
s.reminderTime = e.detail.value
storage.saveSettings(s)
this.setData({ reminderTime: e.detail.value })
},
onToggleVoice: function (e) {
var s = storage.getSettings()
onToggleVoice(e) {
const s = storage.getSettings()
s.voiceGuide = e.detail.value
storage.saveSettings(s)
this.setData({ voiceGuide: e.detail.value })
},
onToggleVibrate: function (e) {
var s = storage.getSettings()
onToggleVibrate(e) {
const s = storage.getSettings()
s.vibrate = e.detail.value
storage.saveSettings(s)
this.setData({ vibrate: e.detail.value })
},
onCopyWechat: function () {
onCopyWechat() {
wx.setClipboardData({
data: '刘承',
success: function () {
success: () => {
wx.showToast({ title: '已复制', icon: 'success', duration: 1500 })
}
})
+29 -36
View File
@@ -1,7 +1,7 @@
var Timer = require('../../utils/timer')
var storage = require('../../utils/storage')
var planMod = require('../../utils/plan')
var themeMod = require('../../utils/theme')
const Timer = require('../../utils/timer')
const storage = require('../../utils/storage')
const planMod = require('../../utils/plan')
const themeMod = require('../../utils/theme')
Page({
data: {
@@ -19,26 +19,21 @@ Page({
isFreeMode: false
},
onLoad: function (options) {
onLoad(options) {
themeMod.applyThemeToPage(this)
var target
var planDay = 1
var isFreeMode = false
let target
let planDay = 1
let isFreeMode = false
if (options && options.free) {
target = parseInt(options.free) || 60
isFreeMode = true
} else {
var settings = storage.getSettings()
var records = []
var allRecords = storage.getRecords()
Object.keys(allRecords).forEach(function (key) {
records = records.concat(allRecords[key])
})
var plan = planMod.getPlan(settings.planId)
planDay = planMod.getPlanDay(settings.planId, records)
const settings = storage.getSettings()
const allRecords = Object.values(storage.getRecords()).flat()
const plan = planMod.getPlan(settings.planId)
planDay = planMod.getPlanDay(settings.planId, allRecords)
target = planMod.getTodayTarget(settings.planId, Math.min(planDay, plan.totalDays))
}
@@ -47,19 +42,18 @@ Page({
remaining: target,
todayTarget: target,
planDay: isFreeMode ? 0 : Math.min(planDay, 99),
isFreeMode: isFreeMode
isFreeMode
})
var self = this
this._timer = new Timer({
onTick: function (tick) {
self.setData({
onTick: (tick) => {
this.setData({
remaining: tick.remaining > 0 ? tick.remaining : 0,
overtime: tick.remaining <= 0 ? tick.elapsed - self.data.duration : 0
overtime: tick.remaining <= 0 ? tick.elapsed - this.data.duration : 0
})
},
onComplete: function () {
self.setData({ status: 'completed', isCompleted: true })
onComplete: () => {
this.setData({ status: 'completed', isCompleted: true })
if (storage.getSettings().vibrate) {
try { wx.vibrateLong() } catch (e) {}
}
@@ -68,11 +62,11 @@ Page({
})
},
onUnload: function () {
onUnload() {
if (this._timer) this._timer.stop()
},
onStart: function () {
onStart() {
if (this.data.isPaused) {
this._timer.resume()
this.setData({ isRunning: true, isPaused: false, status: 'running' })
@@ -83,26 +77,25 @@ Page({
this.setData({ isRunning: true, status: 'running' })
},
onPause: function () {
onPause() {
if (!this.data.isRunning || this.data.isPaused) return
this._timer.pause()
this.setData({ isPaused: true, status: 'paused' })
},
onStop: function () {
var self = this
onStop() {
wx.showModal({
title: '结束训练',
content: '确定要结束本次训练吗?',
success: function (res) {
if (res.confirm) self.finishTraining()
success: (res) => {
if (res.confirm) this.finishTraining()
}
})
},
finishTraining: function () {
var elapsed = this._timer.stop()
var today = storage.getToday()
finishTraining() {
const elapsed = this._timer.stop()
const today = storage.getToday()
storage.saveRecord({
date: today,
duration: elapsed,
@@ -111,7 +104,7 @@ Page({
})
storage.updateStreak(today)
wx.showToast({ title: '已记录 ' + elapsed + '秒', icon: 'none', duration: 1500 })
setTimeout(function () { wx.navigateBack() }, 1500)
wx.showToast({ title: `已记录 ${elapsed}`, icon: 'none', duration: 1500 })
setTimeout(() => { wx.navigateBack() }, 1500)
}
})