huahuazenmehua/pages/course-detail/course-detail.js

61 lines
1.6 KiB
JavaScript

import { injectPage } from '@jdmini/api'
const { getCourseById } = require('../../utils/data.js')
const storage = require('../../utils/storage.js')
Page(injectPage()({
data: {
course: null,
progress: null,
isFavorite: false,
hasStarted: false
},
onLoad(options) {
const courseId = options.courseId
const course = getCourseById(courseId)
if (!course) {
wx.showToast({ title: '课程不存在', icon: 'none' })
setTimeout(() => wx.navigateBack(), 1500)
return
}
const progress = storage.getCourseProgress(courseId)
const isFav = storage.isFavorite(courseId)
this.setData({
course,
progress,
isFavorite: isFav,
hasStarted: progress.currentStep > 0 || progress.completed
})
},
onToggleFavorite() {
const { course } = this.data
const isFav = storage.toggleFavorite(course.id)
this.setData({ isFavorite: isFav })
wx.showToast({ title: isFav ? '已收藏' : '已取消收藏', icon: 'none' })
},
onStartLearning() {
const { course, progress } = this.data
const stepIndex = progress.completed ? 0 : (progress.currentStep || 0)
storage.addRecentHistory({
courseId: course.id,
courseTitle: course.title,
coverEmoji: course.coverEmoji,
coverColor: course.coverColor,
stepIndex
})
wx.navigateTo({
url: `/pages/study-step/study-step?courseId=${course.id}&stepIndex=${stepIndex}`
})
},
onShareAppMessage() {
const { course } = this.data
return {
title: `学画画:${course ? course.title : ''}`,
path: `/pages/course-detail/course-detail?courseId=${course ? course.id : ''}`
}
}
}))