1
This commit is contained in:
82
pages/course-detail/course-detail.js
Normal file
82
pages/course-detail/course-detail.js
Normal file
@@ -0,0 +1,82 @@
|
||||
// pages/course-detail/course-detail.js
|
||||
const { coursesData } = require('../../utils/data.js')
|
||||
const { showSuccess, showError } = require('../../utils/util.js')
|
||||
const learningTracker = require('../../utils/learningTracker.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
course: null,
|
||||
isFavorite: false,
|
||||
activeTab: 0,
|
||||
tabs: ['课程信息', '教学大纲', '课程评价']
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
const { id } = options
|
||||
this.loadCourseDetail(id)
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 开始跟踪学习时间
|
||||
learningTracker.onPageShow('course')
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 停止跟踪学习时间
|
||||
learningTracker.onPageHide()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 记录学习时长
|
||||
learningTracker.onPageUnload()
|
||||
},
|
||||
|
||||
// 加载课程详情
|
||||
loadCourseDetail(id) {
|
||||
const course = coursesData.find(c => c.id === parseInt(id))
|
||||
if (course) {
|
||||
const favorites = wx.getStorageSync('favoriteCourses') || []
|
||||
this.setData({
|
||||
course,
|
||||
isFavorite: favorites.includes(course.id)
|
||||
})
|
||||
} else {
|
||||
showError('课程不存在')
|
||||
setTimeout(() => {
|
||||
wx.navigateBack()
|
||||
}, 1500)
|
||||
}
|
||||
},
|
||||
|
||||
// 切换标签
|
||||
onTabChange(e) {
|
||||
const { index } = e.currentTarget.dataset
|
||||
this.setData({ activeTab: index })
|
||||
},
|
||||
|
||||
// 收藏/取消收藏
|
||||
onToggleFavorite() {
|
||||
const { course, isFavorite } = this.data
|
||||
let favorites = wx.getStorageSync('favoriteCourses') || []
|
||||
|
||||
if (isFavorite) {
|
||||
favorites = favorites.filter(id => id !== course.id)
|
||||
showSuccess('取消收藏')
|
||||
} else {
|
||||
favorites.push(course.id)
|
||||
showSuccess('收藏成功')
|
||||
}
|
||||
|
||||
wx.setStorageSync('favoriteCourses', favorites)
|
||||
this.setData({ isFavorite: !isFavorite })
|
||||
},
|
||||
|
||||
// 分享课程
|
||||
onShareAppMessage() {
|
||||
const { course } = this.data
|
||||
return {
|
||||
title: `推荐课程:${course.name}`,
|
||||
path: `/pages/course-detail/course-detail?id=${course.id}`
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user