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
+72 -87
View File
@@ -1,92 +1,77 @@
function Timer(options) {
options = options || {}
this.onTick = options.onTick || function () {}
this.onComplete = options.onComplete || function () {}
class Timer {
constructor(options = {}) {
this.onTick = options.onTick || (() => {})
this.onComplete = options.onComplete || (() => {})
this._duration = 0
this._remaining = 0
this._elapsed = 0
this._startTime = 0
this._pausedTime = 0
this._intervalId = null
this._running = false
this._paused = false
this._completed = false
}
Timer.prototype.start = function (duration) {
this._duration = duration
this._remaining = duration
this._elapsed = 0
this._completed = false
this._running = true
this._paused = false
this._startTime = Date.now()
this._tick()
var self = this
this._intervalId = setInterval(function () { self._tick() }, 1000)
}
Timer.prototype.pause = function () {
if (!this._running || this._paused) return
this._paused = true
this._pausedTime = Date.now()
clearInterval(this._intervalId)
this._intervalId = null
}
Timer.prototype.resume = function () {
if (!this._running || !this._paused) return
this._startTime += Date.now() - this._pausedTime
this._paused = false
this._tick()
var self = this
this._intervalId = setInterval(function () { self._tick() }, 1000)
}
Timer.prototype.stop = function () {
this._running = false
this._paused = false
clearInterval(this._intervalId)
this._intervalId = null
return this._elapsed
}
Timer.prototype._tick = function () {
if (!this._running || this._paused) return
this._elapsed = Math.floor((Date.now() - this._startTime) / 1000)
this._remaining = Math.max(0, this._duration - this._elapsed)
this.onTick({
remaining: this._remaining,
elapsed: this._elapsed,
duration: this._duration
})
if (this._remaining <= 0 && !this._completed) {
this._completed = true
this.onComplete({ elapsed: this._elapsed, duration: this._duration })
this._duration = 0
this._remaining = 0
this._elapsed = 0
this._startTime = 0
this._pausedTime = 0
this._intervalId = null
this._running = false
this._paused = false
this._completed = false
}
start(duration) {
this._duration = duration
this._remaining = duration
this._elapsed = 0
this._completed = false
this._running = true
this._paused = false
this._startTime = Date.now()
this._tick()
this._intervalId = setInterval(() => { this._tick() }, 1000)
}
pause() {
if (!this._running || this._paused) return
this._paused = true
this._pausedTime = Date.now()
clearInterval(this._intervalId)
this._intervalId = null
}
resume() {
if (!this._running || !this._paused) return
this._startTime += Date.now() - this._pausedTime
this._paused = false
this._tick()
this._intervalId = setInterval(() => { this._tick() }, 1000)
}
stop() {
this._running = false
this._paused = false
clearInterval(this._intervalId)
this._intervalId = null
return this._elapsed
}
_tick() {
if (!this._running || this._paused) return
this._elapsed = Math.floor((Date.now() - this._startTime) / 1000)
this._remaining = Math.max(0, this._duration - this._elapsed)
this.onTick({
remaining: this._remaining,
elapsed: this._elapsed,
duration: this._duration
})
if (this._remaining <= 0 && !this._completed) {
this._completed = true
this.onComplete({ elapsed: this._elapsed, duration: this._duration })
}
}
get isRunning() { return this._running && !this._paused }
get isPaused() { return this._paused }
get isCompleted() { return this._completed }
get remaining() { return this._remaining }
get elapsed() { return this._elapsed }
}
Object.defineProperty(Timer.prototype, 'isRunning', {
get: function () { return this._running && !this._paused }
})
Object.defineProperty(Timer.prototype, 'isPaused', {
get: function () { return this._paused }
})
Object.defineProperty(Timer.prototype, 'isCompleted', {
get: function () { return this._completed }
})
Object.defineProperty(Timer.prototype, 'remaining', {
get: function () { return this._remaining }
})
Object.defineProperty(Timer.prototype, 'elapsed', {
get: function () { return this._elapsed }
})
module.exports = Timer