1
This commit is contained in:
421
pages/schedule/schedule.js
Normal file
421
pages/schedule/schedule.js
Normal file
@@ -0,0 +1,421 @@
|
||||
// pages/schedule/schedule.js
|
||||
const learningTracker = require('../../utils/learningTracker.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
weekDays: ['周一', '周二', '周三', '周四', '周五'],
|
||||
timePeriods: ['1-2节', '3-4节', '5-6节', '7-8节', '9-10节'],
|
||||
currentWeek: 1, // 当前查看的周次
|
||||
totalWeeks: 20, // 总周数(一学期)
|
||||
schedule: {},
|
||||
scheduleData: [], // 用于视图渲染的二维数组
|
||||
showEditModal: false,
|
||||
editMode: 'add', // 'add' 或 'edit'
|
||||
editDay: '',
|
||||
editPeriod: '',
|
||||
editCourse: {
|
||||
name: '',
|
||||
location: ''
|
||||
}
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// 初始化当前周次(可以根据学期开始日期计算)
|
||||
this.initCurrentWeek()
|
||||
this.loadSchedule()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 开始跟踪学习时间
|
||||
learningTracker.onPageShow('tools')
|
||||
|
||||
// 更新自定义TabBar选中状态
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||||
this.getTabBar().setData({
|
||||
selected: 2
|
||||
})
|
||||
}
|
||||
// 刷新当前周的课表
|
||||
this.loadSchedule()
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 停止跟踪学习时间
|
||||
learningTracker.onPageHide()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 记录学习时长
|
||||
learningTracker.onPageUnload()
|
||||
},
|
||||
|
||||
// 初始化当前周次
|
||||
initCurrentWeek() {
|
||||
// 从缓存获取学期开始日期
|
||||
let semesterStartDate = wx.getStorageSync('semesterStartDate')
|
||||
|
||||
if (!semesterStartDate) {
|
||||
// 如果没有设置,默认为当前学期第1周
|
||||
// 实际应用中可以让用户设置学期开始日期
|
||||
this.setData({ currentWeek: 1 })
|
||||
return
|
||||
}
|
||||
|
||||
// 计算当前是第几周
|
||||
const startDate = new Date(semesterStartDate)
|
||||
const today = new Date()
|
||||
const diffTime = Math.abs(today - startDate)
|
||||
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
|
||||
const weekNumber = Math.ceil(diffDays / 7)
|
||||
|
||||
// 确保周数在有效范围内
|
||||
const currentWeek = Math.min(Math.max(weekNumber, 1), this.data.totalWeeks)
|
||||
this.setData({ currentWeek })
|
||||
},
|
||||
|
||||
// 加载课程表
|
||||
loadSchedule() {
|
||||
const { currentWeek } = this.data
|
||||
const allSchedules = wx.getStorageSync('allCourseSchedules') || {}
|
||||
const savedSchedule = allSchedules[`week_${currentWeek}`] || {}
|
||||
|
||||
// 示例数据(仅第1周有默认数据)
|
||||
let defaultSchedule = {}
|
||||
if (currentWeek === 1 && Object.keys(savedSchedule).length === 0) {
|
||||
defaultSchedule = {
|
||||
'周一-1-2节': { name: '高等数学A', location: '主楼A201' },
|
||||
'周一-3-4节': { name: '数据结构', location: '信息学馆301' },
|
||||
'周二-1-2节': { name: '大学英语', location: '外语楼205' },
|
||||
'周二-3-4节': { name: '大学物理', location: '主楼B305' },
|
||||
'周三-1-2节': { name: '高等数学A', location: '主楼A201' },
|
||||
'周三-5-6节': { name: 'Python程序设计', location: '实验室A' },
|
||||
'周四-1-2节': { name: '大学物理', location: '主楼B305' },
|
||||
'周四-5-6节': { name: '机器学习', location: '信息学馆505' },
|
||||
'周五-1-2节': { name: '数据结构', location: '信息学馆301' }
|
||||
}
|
||||
}
|
||||
|
||||
const schedule = Object.keys(savedSchedule).length > 0 ? savedSchedule : defaultSchedule
|
||||
|
||||
// 构建二维数组用于渲染
|
||||
const scheduleData = this.buildScheduleData(schedule)
|
||||
|
||||
this.setData({
|
||||
schedule,
|
||||
scheduleData
|
||||
})
|
||||
|
||||
// 保存默认数据(仅第1周)
|
||||
if (currentWeek === 1 && Object.keys(savedSchedule).length === 0 && Object.keys(defaultSchedule).length > 0) {
|
||||
const allSchedules = wx.getStorageSync('allCourseSchedules') || {}
|
||||
allSchedules[`week_${currentWeek}`] = defaultSchedule
|
||||
wx.setStorageSync('allCourseSchedules', allSchedules)
|
||||
}
|
||||
},
|
||||
|
||||
// 上一周
|
||||
onPrevWeek() {
|
||||
const { currentWeek } = this.data
|
||||
if (currentWeek > 1) {
|
||||
this.setData({ currentWeek: currentWeek - 1 })
|
||||
this.loadSchedule()
|
||||
wx.showToast({
|
||||
title: `第${currentWeek - 1}周`,
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
})
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: '已经是第一周了',
|
||||
icon: 'none',
|
||||
duration: 1500
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 下一周
|
||||
onNextWeek() {
|
||||
const { currentWeek, totalWeeks } = this.data
|
||||
if (currentWeek < totalWeeks) {
|
||||
this.setData({ currentWeek: currentWeek + 1 })
|
||||
this.loadSchedule()
|
||||
wx.showToast({
|
||||
title: `第${currentWeek + 1}周`,
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
})
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: '已经是最后一周了',
|
||||
icon: 'none',
|
||||
duration: 1500
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 回到本周
|
||||
onCurrentWeek() {
|
||||
this.initCurrentWeek()
|
||||
this.loadSchedule()
|
||||
wx.showToast({
|
||||
title: '已回到本周',
|
||||
icon: 'success',
|
||||
duration: 1000
|
||||
})
|
||||
},
|
||||
|
||||
// 构建课程表数据结构
|
||||
buildScheduleData(schedule) {
|
||||
const { weekDays, timePeriods } = this.data
|
||||
const scheduleData = []
|
||||
|
||||
timePeriods.forEach(period => {
|
||||
const row = {
|
||||
period: period,
|
||||
courses: []
|
||||
}
|
||||
|
||||
weekDays.forEach(day => {
|
||||
const key = `${day}-${period}`
|
||||
const course = schedule[key] || null
|
||||
row.courses.push({
|
||||
day: day,
|
||||
course: course,
|
||||
hasCourse: !!course
|
||||
})
|
||||
})
|
||||
|
||||
scheduleData.push(row)
|
||||
})
|
||||
|
||||
return scheduleData
|
||||
},
|
||||
|
||||
// 点击添加课程按钮
|
||||
onAddCourse() {
|
||||
wx.showModal({
|
||||
title: '添加课程',
|
||||
content: '请点击课程表中的空白格子来添加课程',
|
||||
showCancel: false,
|
||||
confirmText: '知道了',
|
||||
confirmColor: '#667eea'
|
||||
})
|
||||
},
|
||||
|
||||
// 点击课程格子
|
||||
onCourseClick(e) {
|
||||
const { day, period, course } = e.currentTarget.dataset
|
||||
|
||||
if (course) {
|
||||
// 有课程,显示课程信息
|
||||
wx.showModal({
|
||||
title: course.name,
|
||||
content: `上课时间:${day} ${period}\n上课地点:${course.location}`,
|
||||
showCancel: true,
|
||||
cancelText: '关闭',
|
||||
confirmText: '编辑',
|
||||
confirmColor: '#667eea',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
this.openEditModal('edit', day, period, course)
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// 无课程,添加课程
|
||||
this.openEditModal('add', day, period, null)
|
||||
}
|
||||
},
|
||||
|
||||
// 长按课程格子
|
||||
onCourseLongPress(e) {
|
||||
const { day, period, course } = e.currentTarget.dataset
|
||||
|
||||
if (course) {
|
||||
wx.showActionSheet({
|
||||
itemList: ['编辑课程', '删除课程'],
|
||||
success: (res) => {
|
||||
if (res.tapIndex === 0) {
|
||||
this.openEditModal('edit', day, period, course)
|
||||
} else if (res.tapIndex === 1) {
|
||||
this.deleteCourse(day, period)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 打开编辑弹窗
|
||||
openEditModal(mode, day, period, course) {
|
||||
this.setData({
|
||||
showEditModal: true,
|
||||
editMode: mode,
|
||||
editDay: day,
|
||||
editPeriod: period,
|
||||
editCourse: course ? { ...course } : { name: '', location: '' }
|
||||
})
|
||||
},
|
||||
|
||||
// 关闭编辑弹窗
|
||||
closeEditModal() {
|
||||
this.setData({
|
||||
showEditModal: false,
|
||||
editCourse: { name: '', location: '' }
|
||||
})
|
||||
},
|
||||
|
||||
// 课程名称输入
|
||||
onCourseNameInput(e) {
|
||||
this.setData({
|
||||
'editCourse.name': e.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
// 课程地点输入
|
||||
onCourseLocationInput(e) {
|
||||
this.setData({
|
||||
'editCourse.location': e.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
// 保存课程
|
||||
onSaveCourse() {
|
||||
const { editDay, editPeriod, editCourse, currentWeek } = this.data
|
||||
|
||||
if (!editCourse.name.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入课程名称',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (!editCourse.location.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入上课地点',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const key = `${editDay}-${editPeriod}`
|
||||
const schedule = this.data.schedule
|
||||
|
||||
schedule[key] = {
|
||||
name: editCourse.name.trim(),
|
||||
location: editCourse.location.trim()
|
||||
}
|
||||
|
||||
// 保存到对应周次的课表
|
||||
const allSchedules = wx.getStorageSync('allCourseSchedules') || {}
|
||||
allSchedules[`week_${currentWeek}`] = schedule
|
||||
wx.setStorageSync('allCourseSchedules', allSchedules)
|
||||
|
||||
// 更新视图
|
||||
const scheduleData = this.buildScheduleData(schedule)
|
||||
this.setData({
|
||||
schedule,
|
||||
scheduleData,
|
||||
showEditModal: false,
|
||||
editCourse: { name: '', location: '' }
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '保存成功',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
|
||||
// 删除课程
|
||||
onDeleteCourse() {
|
||||
const { editDay, editPeriod } = this.data
|
||||
|
||||
wx.showModal({
|
||||
title: '确认删除',
|
||||
content: '确定要删除这门课程吗?',
|
||||
confirmColor: '#FF5252',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
this.deleteCourse(editDay, editPeriod)
|
||||
this.closeEditModal()
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 删除课程逻辑
|
||||
deleteCourse(day, period) {
|
||||
const { currentWeek } = this.data
|
||||
const key = `${day}-${period}`
|
||||
const schedule = this.data.schedule
|
||||
|
||||
delete schedule[key]
|
||||
|
||||
// 保存到对应周次的课表
|
||||
const allSchedules = wx.getStorageSync('allCourseSchedules') || {}
|
||||
allSchedules[`week_${currentWeek}`] = schedule
|
||||
wx.setStorageSync('allCourseSchedules', allSchedules)
|
||||
|
||||
// 更新视图
|
||||
const scheduleData = this.buildScheduleData(schedule)
|
||||
this.setData({
|
||||
schedule,
|
||||
scheduleData
|
||||
})
|
||||
|
||||
|
||||
wx.showToast({
|
||||
title: '已删除',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
|
||||
// 复制当前周课表到其他周
|
||||
onCopySchedule() {
|
||||
const { currentWeek, schedule } = this.data
|
||||
|
||||
// 如果当前周没有课表,提示用户
|
||||
if (Object.keys(schedule).length === 0) {
|
||||
wx.showToast({
|
||||
title: '当前周没有课程',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
wx.showModal({
|
||||
title: '复制课表',
|
||||
content: `是否将第${currentWeek}周的课表复制到所有周?(不会覆盖已有课程的周)`,
|
||||
confirmText: '复制',
|
||||
cancelText: '取消',
|
||||
confirmColor: '#9B59B6',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
const allSchedules = wx.getStorageSync('allCourseSchedules') || {}
|
||||
let copiedCount = 0
|
||||
|
||||
// 复制到其他周(跳过已有课表的周)
|
||||
for (let week = 1; week <= this.data.totalWeeks; week++) {
|
||||
if (week !== currentWeek && !allSchedules[`week_${week}`]) {
|
||||
allSchedules[`week_${week}`] = { ...schedule }
|
||||
copiedCount++
|
||||
}
|
||||
}
|
||||
|
||||
wx.setStorageSync('allCourseSchedules', allSchedules)
|
||||
|
||||
wx.showToast({
|
||||
title: `已复制到${copiedCount}周`,
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 阻止事件冒泡
|
||||
doNothing() {}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user