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() {}
})