123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- import {courseTypes, courseTypeTexts, htmlTypes, logicStatus} from "../../model/enum";
- import Api from "../../model/api";
- import {getDataSet, getEventParam, showLoading, toast} from "../../utils/utils";
- import Route from "../../model/route";
- import {wxToPromise} from "../../utils/wx";
- Page({
- data: {
- id: '',
- type: '',
- item: {},
- obj: {},
- plan: {},
- fileList: [],
- videoList: [],
- videoIndex: 0,
- courseTypeTexts: courseTypeTexts,
- courseTypesEnum: courseTypes,
- videoItem: {},
- scanTime: 0,
- //已被记录的视频播放时长
- nodeTime: 0,
- //课程状态 logicStatus.NO: 未学完 logicStatus.YES: 已学完
- lessStatus: logicStatus.NO,
- show: false,
- //视频实时播放时长(拖拽进度条,currentTime为拖拽位置的时长)
- currentTime: 0,
- scanOkTime: 0,
- videoContext: null,
- tempFilePath: '',
- //10s 本地缓存一次视频时长
- localRecordInterval: 10,
- //是否是从往期培训来的
- isPre: logicStatus.NO,
- },
- async onLoad(options) {
- let {id, detail, plan, type, isPre} = options;
- let obj = JSON.parse(detail);
- plan = JSON.parse(plan);
- wx.setNavigationBarTitle({title: courseTypeTexts[type]});
- const videoContext = wx.createVideoContext('myVideo3');
- this.setData({
- id, plan,
- obj, type,
- videoContext,
- isPre: Number(isPre)
- }, async () => {
- showLoading();
- await this.getData();
- wx.hideLoading()
- this.data.videoContext.seek(this.data.nodeTime)
- })
- },
- async getData() {
- const res = await Api.getCourseDetail(this.data.id, this.data.obj.eduStuId);
- if (this.data.obj.isOnline) {
- res.data.isQuestion = res.data.suitangUpper;
- } else {
- res.data.isQuestion = res.data.suitangLower;
- }
- let fileList = [];
- let videoItem = {};
- if (res.data.enclosureUrl) {
- if (this.data.type == courseTypes.RECORD) {
- videoItem = JSON.parse(res.data.enclosureUrl)[0];
- if (videoItem.verifyInterval) {
- let scanTime = videoItem.verifyInterval;
- this.setData({scanTime})
- }
- if (this.data.isPre == logicStatus.YES) {
- // 如果是往期培训来的
- this.setData({nodeTime: 0, lessStatus: logicStatus.NO, videoItem});
- return;
- }
- const less = await Api.getRecordedLesson({
- stuId: this.data.obj.eduStuId,
- scheduleId: this.data.id
- })
- if (less.data) {
- let lessStatus = less.data.status;
- let hasFulfilled = lessStatus == logicStatus.YES;
- let localRecordTime = this.getLessonRecordByLocal();
- // localRecordTime = hasFulfilled && localRecordTime >= videoItem.duration ? 0 : localRecordTime;
- let nodeTime = hasFulfilled ? 0 : Math.max(localRecordTime, less.data.nodeTime || 0);
- this.setData({nodeTime, lessStatus, videoItem});
- if (!hasFulfilled && localRecordTime > less.data.nodeTime) {
- //如果未完成课程学习 并且 本地记录的时长 > 数据库记录的时长(异常退出) 更新数据库记录
- this.changeProgress();
- }
- hasFulfilled && this.removeLessonRecordByLocal();
- // let lessStatus = less.data.status;
- // let nodeTime = lessStatus == logicStatus.YES ? 0 : less.data.nodeTime || 0;
- // this.setData({nodeTime, lessStatus, videoItem})
- } else {
- this.setData({nodeTime: 0, lessStatus: logicStatus.NO, videoItem})
- }
- } else {
- fileList = JSON.parse(res.data.enclosureUrl);
- }
- }
- let videoList = [];
- if (this.data.type == courseTypes.PLAYBACK && res.data.videoPlaybackUrl) {
- videoList = res.data.videoPlaybackUrl.split(",");
- }
- this.compare(res.data, this.data.plan);
- this.setData({
- item: res.data, fileList, videoList
- })
- },
- compare(newData, oldData) {
- if (!oldData) {
- return;
- }
- if (newData.courseProcess != oldData.courseProcess ||
- newData.courseStatus != oldData.courseStatus ||
- newData.liveStatus != oldData.liveStatus
- ) {
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.emit('refresh');
- }
- },
- async downloadFile(e) {
- let url = getDataSet(e, "url");
- let end = url.substr(url.lastIndexOf(".") + 1);
- showLoading('下载中...')
- let filePath = "";
- if (this.data.tempFilePath) {
- filePath = this.data.tempFilePath;
- } else {
- const res = await wxToPromise("downloadFile", {url});
- if (res.statusCode === 200) {
- filePath = res.tempFilePath
- this.setData({tempFilePath: filePath})
- } else {
- toast(`下载文档失败,请稍后重试${res.statusCode}:${res.errMsg}`)
- }
- }
- try {
- await wx.openDocument({
- filePath
- })
- } catch (e) {
- console.log(e)
- toast(`打开文档失败,请稍后重试${e.msg}`)
- }
- wx.hideLoading();
- },
- toTeacher(e) {
- let id = getDataSet(e, "id");
- Route.toTeacher(id);
- },
- clickImg(e) {
- let id = this.data.item.id;
- let eId = this.data.obj.eduStuId;
- Route.toNews(htmlTypes.SEAT, id, "座位图", eId);
- },
- async refresh(e) {
- await this.getData();
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.emit('refresh');
- },
- changeVideo(e) {
- let index = getDataSet(e, "index");
- this.setData({
- videoIndex: index,
- })
- },
- // 以下是录播课的逻辑算法 待分离
- async onUnload() {
- await this.changeProgress();
- },
- async onHide() {
- await this.changeProgress();
- },
- async changeProgress() {
- if (this.data.isPre == logicStatus.YES) {
- return;
- }
- if (this.data.type == courseTypes.RECORD) {
- const res = await Api.changeVideoProgress({
- stuId: this.data.obj.eduStuId,
- scheduleId: this.data.id,
- nodeTime: this.data.nodeTime
- });
- // 学习时长记录到本地缓存 start
- // 如果未完成视频学习 将学习记录缓存到本地
- this.data.lessStatus == logicStatus.NO && this.recordLessonByLocal(this.data.currentTime);
- // 学习时长记录到本地缓存 end
- if (res.data == logicStatus.YES) {
- await this.getData(false);
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.emit('refresh');
- }
- }
- },
- async timeUpdate(e) {
- if (this.data.isPre == logicStatus.YES) {
- //往期培训不记录播放时长
- return;
- }
- let currentTime = parseInt(getEventParam(e, "currentTime"));
- let lessStatus = this.data.lessStatus;
- let unFulfilledFlag = lessStatus == logicStatus.NO;
- console.log("timeUpdate", currentTime, this.data.scanTime)
- if (this.data.scanTime && unFulfilledFlag) {//需要处理验证人脸情况
- let flag = currentTime % this.data.scanTime;
- if (flag == 0 && currentTime != this.data.scanOkTime &&
- currentTime >= this.data.nodeTime && currentTime != this.data.videoItem.duration) {
- console.log("暂停,弹出人脸识别")
- this.data.videoContext.exitFullScreen();
- this.data.videoContext.pause();
- this.setData({
- show: true,
- currentTime
- });
- }
- }
- if (currentTime - this.data.nodeTime <= 1) {
- // 播放时长和被记录的时长的时间差<=1秒
- if (currentTime - this.data.nodeTime >= 0) {
- this.data.nodeTime = currentTime;
- if (this.data.nodeTime == this.data.videoItem.duration) {
- if (unFulfilledFlag) {
- this.setData({
- lessStatus: logicStatus.YES
- });
- await wx.showModal({
- title: "恭喜你,视频已学完!",
- showCancel: false
- });
- await this.changeProgress();
- }
- }
- }
- // 学习时长记录到本地缓存 start
- if (unFulfilledFlag && currentTime % this.data.localRecordInterval === 0) {
- //未学完课程时 才将学习时长记录到本地
- this.recordLessonByLocal(currentTime);
- }
- this.setData({
- currentTime
- });
- // 学习时长记录到本地缓存 end
- } else {
- // 不可以快进的情况(课程未学完)
- if (unFulfilledFlag) {
- wx.showToast({
- title: '视频未学完,不可以快进!',
- icon: 'none'
- });
- this.data.videoContext.seek(this.data.nodeTime);
- }
- }
- },
- recordLessonByLocal(time) {
- console.log('缓存视频学习时长', time);
- wx.setStorageSync(`stu${this.data.obj.eduStuId}-course${this.data.id}`, time);
- },
- getLessonRecordByLocal() {
- return wx.getStorageSync(`stu${this.data.obj.eduStuId}-course${this.data.id}`) || 0;
- },
- removeLessonRecordByLocal() {
- //如果课程已学完,移除本地学习时长的记录
- wx.removeStorageSync(`stu${this.data.obj.eduStuId}-course${this.data.id}`);
- },
- scanOk(e) {
- this.data.scanOkTime = getEventParam(e, "currentTime");
- this.setData({
- show: false,
- });
- setTimeout(() => {
- this.data.videoContext.play();
- }, 500)
- },
- });
|