1
This commit is contained in:
159
pages/countdown/countdown.js
Normal file
159
pages/countdown/countdown.js
Normal file
@@ -0,0 +1,159 @@
|
||||
// pages/countdown/countdown.js
|
||||
const { getCountdown, showSuccess, showError } = require('../../utils/util.js')
|
||||
const learningTracker = require('../../utils/learningTracker.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
countdowns: [],
|
||||
newEventName: '',
|
||||
newEventDate: ''
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadCountdowns()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 开始跟踪学习时间
|
||||
learningTracker.onPageShow('tools')
|
||||
|
||||
this.startTimer()
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 停止跟踪学习时间
|
||||
learningTracker.onPageHide()
|
||||
|
||||
this.stopTimer()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 记录学习时长
|
||||
learningTracker.onPageUnload()
|
||||
|
||||
this.stopTimer()
|
||||
},
|
||||
|
||||
// 加载倒计时
|
||||
loadCountdowns() {
|
||||
let countdowns = wx.getStorageSync('countdowns') || []
|
||||
|
||||
// 默认示例数据
|
||||
if (countdowns.length === 0) {
|
||||
countdowns = [
|
||||
{
|
||||
id: 1,
|
||||
name: '高等数学期末考试',
|
||||
date: '2025-12-20',
|
||||
color: '#FF6B6B'
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: '英语四级考试',
|
||||
date: '2025-12-15',
|
||||
color: '#4A90E2'
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: '课程设计答辩',
|
||||
date: '2025-12-25',
|
||||
color: '#50C878'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
this.setData({ countdowns })
|
||||
this.updateAllCountdowns()
|
||||
},
|
||||
|
||||
// 开始定时器
|
||||
startTimer() {
|
||||
this.timer = setInterval(() => {
|
||||
this.updateAllCountdowns()
|
||||
}, 1000)
|
||||
},
|
||||
|
||||
// 停止定时器
|
||||
stopTimer() {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer)
|
||||
this.timer = null
|
||||
}
|
||||
},
|
||||
|
||||
// 更新所有倒计时
|
||||
updateAllCountdowns() {
|
||||
const { countdowns } = this.data
|
||||
const updated = countdowns.map(item => ({
|
||||
...item,
|
||||
...getCountdown(item.date)
|
||||
}))
|
||||
this.setData({ countdowns: updated })
|
||||
},
|
||||
|
||||
// 事件名称输入
|
||||
onNameInput(e) {
|
||||
this.setData({ newEventName: e.detail.value })
|
||||
},
|
||||
|
||||
// 日期选择
|
||||
onDateChange(e) {
|
||||
this.setData({ newEventDate: e.detail.value })
|
||||
},
|
||||
|
||||
// 添加倒计时
|
||||
onAddCountdown() {
|
||||
const { newEventName, newEventDate, countdowns } = this.data
|
||||
|
||||
if (!newEventName.trim()) {
|
||||
showError('请输入事件名称')
|
||||
return
|
||||
}
|
||||
|
||||
if (!newEventDate) {
|
||||
showError('请选择日期')
|
||||
return
|
||||
}
|
||||
|
||||
const colors = ['#FF6B6B', '#4A90E2', '#50C878', '#F39C12', '#9B59B6']
|
||||
|
||||
const newCountdown = {
|
||||
id: Date.now(),
|
||||
name: newEventName.trim(),
|
||||
date: newEventDate,
|
||||
color: colors[Math.floor(Math.random() * colors.length)]
|
||||
}
|
||||
|
||||
countdowns.push(newCountdown)
|
||||
wx.setStorageSync('countdowns', countdowns)
|
||||
|
||||
this.setData({
|
||||
countdowns,
|
||||
newEventName: '',
|
||||
newEventDate: ''
|
||||
})
|
||||
|
||||
this.updateAllCountdowns()
|
||||
showSuccess('添加成功')
|
||||
},
|
||||
|
||||
// 删除倒计时
|
||||
onDelete(e) {
|
||||
const { id } = e.currentTarget.dataset
|
||||
|
||||
wx.showModal({
|
||||
title: '确认删除',
|
||||
content: '确定要删除这个倒计时吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
let { countdowns } = this.data
|
||||
countdowns = countdowns.filter(item => item.id !== id)
|
||||
wx.setStorageSync('countdowns', countdowns)
|
||||
|
||||
this.setData({ countdowns })
|
||||
showSuccess('删除成功')
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user