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,
lessStatus: logicStatus.NO,
show: false,
currentTime: 0,
scanOkTime: 0,
videoContext: null,
tempFilePath: ''
},
async onLoad(options) {
let id = options.id;
let obj = JSON.parse(options.detail);
let plan = JSON.parse(options.plan);
let type = options.type;
wx.setNavigationBarTitle({title: courseTypeTexts[type]});
const videoContext = wx.createVideoContext('myVideo3');
this.setData({
id, plan,
obj, type, videoContext
}, 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})
}
const less = await Api.getRecordedLesson({
stuId: this.data.obj.eduStuId,
scheduleId: this.data.id
})
if (less.data) {
let nodeTime = less.data.status == logicStatus.YES ? videoItem.duration : less.data.nodeTime || 0;
let lessStatus = less.data.status;
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.type == courseTypes.RECORD) {
const res = await Api.changeVideoProgress({
stuId: this.data.obj.eduStuId,
scheduleId: this.data.id,
nodeTime: this.data.nodeTime
})
if (res.data == logicStatus.YES) {
await this.getData(false);
const eventChannel = this.getOpenerEventChannel()
eventChannel.emit('refresh');
}
}
},
async timeUpdate(e) {
let currentTime = parseInt(getEventParam(e, "currentTime"));
console.log("timeUpdate", currentTime, this.data.scanTime)
if (this.data.scanTime) {//需要处理验证人脸情况
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) {
if (currentTime - this.data.nodeTime >= 0) {
this.data.nodeTime = currentTime;
if (this.data.nodeTime == this.data.videoItem.duration) {
if (this.data.lessStatus == logicStatus.NO) {
await wx.showModal({
title: "恭喜你,视频已学完!",
showCancel: false
})
this.data.lessStatus = logicStatus.YES;
await this.changeProgress();
}
}
}
} else {
this.data.videoContext.seek(this.data.nodeTime)
}
},
scanOk(e) {
this.data.scanOkTime = getEventParam(e, "currentTime");
this.setData({
show: false,
});
setTimeout(() => {
this.data.videoContext.play();
}, 500)
},
});