1
This commit is contained in:
204
pages/courses/courses.js
Normal file
204
pages/courses/courses.js
Normal file
@@ -0,0 +1,204 @@
|
||||
// pages/courses/courses.js
|
||||
const { coursesData } = require('../../utils/data.js')
|
||||
const { showSuccess, showError } = require('../../utils/util.js')
|
||||
const learningTracker = require('../../utils/learningTracker.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
allCourses: [],
|
||||
displayCourses: [],
|
||||
searchKeyword: '',
|
||||
selectedCategory: 'all',
|
||||
categories: [
|
||||
{ id: 'all', name: '全部' },
|
||||
{ id: '必修', name: '必修' },
|
||||
{ id: '专业必修', name: '专业必修' },
|
||||
{ id: '选修', name: '选修' },
|
||||
{ id: '专业选修', name: '专业选修' }
|
||||
],
|
||||
showFilter: false,
|
||||
selectedDepartment: '全部', // 修改为'全部'而不是'all'
|
||||
departments: [
|
||||
'全部',
|
||||
'数学系',
|
||||
'物理系',
|
||||
'计算机学院',
|
||||
'外国语学院'
|
||||
]
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
try {
|
||||
this.loadCourses()
|
||||
} catch (error) {
|
||||
console.error('课程加载失败:', error)
|
||||
showError('课程数据加载失败')
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 开始跟踪学习时间
|
||||
learningTracker.onPageShow('course')
|
||||
|
||||
// 更新自定义TabBar选中状态
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||||
this.getTabBar().setData({
|
||||
selected: 1
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 停止跟踪学习时间
|
||||
learningTracker.onPageHide()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 记录学习时长
|
||||
learningTracker.onPageUnload()
|
||||
},
|
||||
|
||||
// 加载课程数据
|
||||
loadCourses() {
|
||||
const courses = coursesData.map(course => ({
|
||||
...course,
|
||||
isFavorite: this.checkFavorite(course.id)
|
||||
}))
|
||||
|
||||
this.setData({
|
||||
allCourses: courses,
|
||||
displayCourses: courses
|
||||
})
|
||||
},
|
||||
|
||||
// 检查是否已收藏
|
||||
checkFavorite(courseId) {
|
||||
const favorites = wx.getStorageSync('favoriteCourses') || []
|
||||
return favorites.includes(courseId)
|
||||
},
|
||||
|
||||
// 搜索课程(防抖处理)
|
||||
onSearchInput(e) {
|
||||
const keyword = e.detail.value
|
||||
this.setData({ searchKeyword: keyword })
|
||||
|
||||
// 清除之前的延迟搜索
|
||||
if (this.searchTimer) {
|
||||
clearTimeout(this.searchTimer)
|
||||
}
|
||||
|
||||
// 设置300ms防抖
|
||||
this.searchTimer = setTimeout(() => {
|
||||
this.filterCourses()
|
||||
}, 300)
|
||||
},
|
||||
|
||||
// 分类筛选
|
||||
onCategoryChange(e) {
|
||||
const category = e.currentTarget.dataset.category
|
||||
this.setData({ selectedCategory: category })
|
||||
this.filterCourses()
|
||||
},
|
||||
|
||||
// 院系筛选
|
||||
onDepartmentChange(e) {
|
||||
const index = e.detail.value
|
||||
this.setData({
|
||||
selectedDepartment: this.data.departments[index],
|
||||
showFilter: false
|
||||
})
|
||||
this.filterCourses()
|
||||
},
|
||||
|
||||
// 筛选课程
|
||||
filterCourses() {
|
||||
const { allCourses, searchKeyword, selectedCategory, selectedDepartment } = this.data
|
||||
|
||||
console.log('筛选条件:', {
|
||||
searchKeyword,
|
||||
selectedCategory,
|
||||
selectedDepartment,
|
||||
allCoursesCount: allCourses.length
|
||||
})
|
||||
|
||||
let filtered = allCourses.filter(course => {
|
||||
// 搜索关键词过滤(不区分大小写,去除空格)
|
||||
const keyword = (searchKeyword || '').trim().toLowerCase()
|
||||
const matchKeyword = !keyword ||
|
||||
(course.name && course.name.toLowerCase().includes(keyword)) ||
|
||||
(course.teacher && course.teacher.toLowerCase().includes(keyword)) ||
|
||||
(course.code && course.code.toLowerCase().includes(keyword))
|
||||
|
||||
// 分类过滤
|
||||
const matchCategory = selectedCategory === 'all' ||
|
||||
course.category === selectedCategory
|
||||
|
||||
// 院系过滤
|
||||
const matchDepartment = selectedDepartment === '全部' ||
|
||||
course.department === selectedDepartment
|
||||
|
||||
return matchKeyword && matchCategory && matchDepartment
|
||||
})
|
||||
|
||||
console.log('筛选结果:', filtered.length, '门课程')
|
||||
this.setData({ displayCourses: filtered })
|
||||
},
|
||||
|
||||
// 显示/隐藏筛选面板
|
||||
toggleFilter() {
|
||||
this.setData({ showFilter: !this.data.showFilter })
|
||||
},
|
||||
|
||||
// 收藏课程
|
||||
onFavorite(e) {
|
||||
const { id } = e.currentTarget.dataset
|
||||
const { allCourses } = this.data
|
||||
|
||||
// 更新课程收藏状态
|
||||
const updatedCourses = allCourses.map(course => {
|
||||
if (course.id === id) {
|
||||
course.isFavorite = !course.isFavorite
|
||||
|
||||
// 保存到本地存储
|
||||
let favorites = wx.getStorageSync('favoriteCourses') || []
|
||||
if (course.isFavorite) {
|
||||
favorites.push(id)
|
||||
showSuccess('收藏成功')
|
||||
} else {
|
||||
favorites = favorites.filter(fid => fid !== id)
|
||||
showSuccess('取消收藏')
|
||||
}
|
||||
wx.setStorageSync('favoriteCourses', favorites)
|
||||
}
|
||||
return course
|
||||
})
|
||||
|
||||
this.setData({ allCourses: updatedCourses })
|
||||
this.filterCourses()
|
||||
},
|
||||
|
||||
// 查看课程详情
|
||||
onCourseDetail(e) {
|
||||
const { id } = e.currentTarget.dataset
|
||||
wx.navigateTo({
|
||||
url: `/pages/course-detail/course-detail?id=${id}`
|
||||
})
|
||||
},
|
||||
|
||||
// 清空筛选
|
||||
onResetFilter() {
|
||||
this.setData({
|
||||
searchKeyword: '',
|
||||
selectedCategory: 'all',
|
||||
selectedDepartment: '全部',
|
||||
showFilter: false
|
||||
})
|
||||
this.filterCourses()
|
||||
},
|
||||
|
||||
// 清空搜索
|
||||
onClearSearch() {
|
||||
this.setData({ searchKeyword: '' })
|
||||
this.filterCourses()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user