// pages/VideoDetails/VideoDetails.js const api = require('../../utils/api'); const store = require('../../utils/store'); const format = require('../../utils/format'); const app = getApp(); Page({ data: { code: '', owned: false, loading: true, buyText: '', contactText: '', VideoTitle: '', VideoDetails: '', coverUrl: '', playdata: [], /** 列表上方的更新期数文案,整句来自后台配置 */ updatedText: '', VideoPlayerSrc: '', HiddenVideoControl: true, top: 0, }, onLoad(options) { const code = options.code; if (!code) { wx.switchTab({ url: '/pages/kecheng/kecheng' }); return; } this.setData({ code: code }); }, /** 首次进入和从购买页返回都会走这里:买完回来要立刻变成已拥有。 */ onShow() { app.ready().then(() => this.applyConfig()); if (this.data.code) this.load(this.data.code); }, /** 按钮文案取自后台配置,后台改完小程序下拉刷新即可生效。 */ applyConfig() { this.setData({ buyText: store.cfg('course.button.buy'), contactText: store.cfg('course.button.contact'), }); }, load(code) { api .getCourse(code) .then((course) => { this.setData({ loading: false, owned: course.owned, VideoTitle: course.title || '', VideoDetails: course.intro || '', coverUrl: course.coverUrl || '', }); if (course.owned) this.loadLessons(code); }) .catch((err) => { this.setData({ loading: false }); wx.showToast({ title: err.message || store.cfg('course.tip.loadFailed'), icon: 'none' }); setTimeout(() => wx.switchTab({ url: '/pages/kecheng/kecheng' }), 1500); }); }, loadLessons(code) { api .getLessons(code) .then((list) => { const rows = (list || []).map((item) => ({ chapterId: item.chapterId, playId: item.playId, chapterTitle: item.chapterTitle, lessonTitle: item.lessonTitle, duration: item.duration, publishedAt: format.toDateString(item.publishedAt), coverUrl: item.coverUrl, })); // 期数文案整句来自后台配置,{n} 在这里替换成实际条数 this.setData({ playdata: rows, updatedText: store.cfg('course.lesson.updated').replace('{n}', rows.length), }); }) .catch((err) => { this.setData({ playdata: [] }); console.warn('[VideoDetails] 课时加载失败', err); }); }, /** 点课时:向后端换一次性播放地址,视频本身不直接暴露。 */ onTapLesson(e) { const item = this.data.playdata[e.currentTarget.dataset.index]; if (!item) return; wx.showLoading({ title: '加载中', mask: true }); api .getPlayUrl(this.data.code, item.chapterId, item.playId) .then((res) => { wx.hideLoading(); this.setData({ VideoPlayerSrc: res.url, HiddenVideoControl: false, top: 420 }); }) .catch((err) => { wx.hideLoading(); wx.showToast({ title: err.message || store.cfg('course.tip.playFailed'), icon: 'none' }); }); }, /** 购买入口:详情页不做交易,统一跳商业化页面。 */ goBuy() { if (!this.data.code) return; // 已经拥有就不必再进购买页:原地提示一句 if (this.data.owned) { wx.showToast({ title: store.cfg('course.tip.owned'), icon: 'none' }); return; } wx.navigateTo({ url: '/pages/buy/buy?code=' + this.data.code }); }, onShareAppMessage() { return { title: store.cfg('share.title'), path: '/pages/index/index' }; }, });