courseDetail.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import {courseTypes, courseTypeTexts, htmlTypes, logicStatus} from "../../model/enum";
  2. import Api from "../../model/api";
  3. import {getDataSet, getEventParam, showLoading, toast} from "../../utils/utils";
  4. import Route from "../../model/route";
  5. import {wxToPromise} from "../../utils/wx";
  6. Page({
  7. data: {
  8. id: '',
  9. type: '',
  10. item: {},
  11. obj: {},
  12. plan: {},
  13. fileList: [],
  14. videoList: [],
  15. videoIndex: 0,
  16. courseTypeTexts: courseTypeTexts,
  17. courseTypesEnum: courseTypes,
  18. videoItem: {},
  19. scanTime: 0,
  20. nodeTime: 0,
  21. lessStatus: logicStatus.NO,
  22. show: false,
  23. currentTime: 0,
  24. scanOkTime: 0,
  25. videoContext: null,
  26. tempFilePath: ''
  27. },
  28. async onLoad(options) {
  29. let id = options.id;
  30. let obj = JSON.parse(options.detail);
  31. let plan = JSON.parse(options.plan);
  32. let type = options.type;
  33. wx.setNavigationBarTitle({title: courseTypeTexts[type]});
  34. const videoContext = wx.createVideoContext('myVideo3');
  35. this.setData({
  36. id, plan,
  37. obj, type, videoContext
  38. }, async () => {
  39. showLoading();
  40. await this.getData();
  41. wx.hideLoading()
  42. this.data.videoContext.seek(this.data.nodeTime)
  43. })
  44. },
  45. async getData() {
  46. const res = await Api.getCourseDetail(this.data.id, this.data.obj.eduStuId);
  47. if (this.data.obj.isOnline) {
  48. res.data.isQuestion = res.data.suitangUpper;
  49. } else {
  50. res.data.isQuestion = res.data.suitangLower;
  51. }
  52. let fileList = [];
  53. let videoItem = {};
  54. if (res.data.enclosureUrl) {
  55. if (this.data.type == courseTypes.RECORD) {
  56. videoItem = JSON.parse(res.data.enclosureUrl)[0];
  57. if (videoItem.verifyInterval) {
  58. let scanTime = videoItem.verifyInterval;
  59. this.setData({scanTime})
  60. }
  61. const less = await Api.getRecordedLesson({
  62. stuId: this.data.obj.eduStuId,
  63. scheduleId: this.data.id
  64. })
  65. if (less.data) {
  66. let nodeTime = less.data.status == logicStatus.YES ? videoItem.duration : less.data.nodeTime || 0;
  67. let lessStatus = less.data.status;
  68. this.setData({nodeTime, lessStatus, videoItem})
  69. } else {
  70. this.setData({nodeTime: 0, lessStatus: logicStatus.NO, videoItem})
  71. }
  72. } else {
  73. fileList = JSON.parse(res.data.enclosureUrl);
  74. }
  75. }
  76. let videoList = [];
  77. if (this.data.type == courseTypes.PLAYBACK && res.data.videoPlaybackUrl) {
  78. videoList = res.data.videoPlaybackUrl.split(",");
  79. }
  80. this.compare(res.data, this.data.plan);
  81. this.setData({
  82. item: res.data, fileList, videoList
  83. })
  84. },
  85. compare(newData, oldData) {
  86. if (!oldData) {
  87. return;
  88. }
  89. if (newData.courseProcess != oldData.courseProcess ||
  90. newData.courseStatus != oldData.courseStatus ||
  91. newData.liveStatus != oldData.liveStatus
  92. ) {
  93. const eventChannel = this.getOpenerEventChannel()
  94. eventChannel.emit('refresh');
  95. }
  96. },
  97. async downloadFile(e) {
  98. let url = getDataSet(e, "url");
  99. let end = url.substr(url.lastIndexOf(".") + 1);
  100. showLoading('下载中...')
  101. let filePath = "";
  102. if (this.data.tempFilePath) {
  103. filePath = this.data.tempFilePath;
  104. } else {
  105. const res = await wxToPromise("downloadFile", {url});
  106. if (res.statusCode === 200) {
  107. filePath = res.tempFilePath
  108. this.setData({tempFilePath: filePath})
  109. } else {
  110. toast(`下载文档失败,请稍后重试${res.statusCode}:${res.errMsg}`)
  111. }
  112. }
  113. try {
  114. await wx.openDocument({
  115. filePath
  116. })
  117. } catch (e) {
  118. console.log(e)
  119. toast(`打开文档失败,请稍后重试${e.msg}`)
  120. }
  121. wx.hideLoading();
  122. },
  123. toTeacher(e) {
  124. let id = getDataSet(e, "id");
  125. Route.toTeacher(id);
  126. },
  127. clickImg(e) {
  128. let id = this.data.item.id;
  129. let eId = this.data.obj.eduStuId;
  130. Route.toNews(htmlTypes.SEAT, id, "座位图", eId);
  131. },
  132. async refresh(e) {
  133. await this.getData();
  134. const eventChannel = this.getOpenerEventChannel()
  135. eventChannel.emit('refresh');
  136. },
  137. changeVideo(e) {
  138. let index = getDataSet(e, "index");
  139. this.setData({
  140. videoIndex: index,
  141. })
  142. },
  143. // 以下是录播课的逻辑算法 待分离
  144. async onUnload() {
  145. await this.changeProgress();
  146. },
  147. async onHide() {
  148. await this.changeProgress();
  149. },
  150. async changeProgress() {
  151. if (this.data.type == courseTypes.RECORD) {
  152. const res = await Api.changeVideoProgress({
  153. stuId: this.data.obj.eduStuId,
  154. scheduleId: this.data.id,
  155. nodeTime: this.data.nodeTime
  156. })
  157. if (res.data == logicStatus.YES) {
  158. await this.getData(false);
  159. const eventChannel = this.getOpenerEventChannel()
  160. eventChannel.emit('refresh');
  161. }
  162. }
  163. },
  164. async timeUpdate(e) {
  165. let currentTime = parseInt(getEventParam(e, "currentTime"));
  166. console.log("timeUpdate", currentTime, this.data.scanTime)
  167. if (this.data.scanTime) {//需要处理验证人脸情况
  168. let flag = currentTime % this.data.scanTime;
  169. if (flag == 0 && currentTime != this.data.scanOkTime &&
  170. currentTime >= this.data.nodeTime && currentTime != this.data.videoItem.duration) {
  171. console.log("暂停,弹出人脸识别")
  172. this.data.videoContext.exitFullScreen();
  173. this.data.videoContext.pause();
  174. this.setData({
  175. show: true,
  176. currentTime
  177. });
  178. }
  179. }
  180. if (currentTime - this.data.nodeTime <= 1) {
  181. if (currentTime - this.data.nodeTime >= 0) {
  182. this.data.nodeTime = currentTime;
  183. if (this.data.nodeTime == this.data.videoItem.duration) {
  184. if (this.data.lessStatus == logicStatus.NO) {
  185. await wx.showModal({
  186. title: "恭喜你,视频已学完!",
  187. showCancel: false
  188. })
  189. this.data.lessStatus = logicStatus.YES;
  190. await this.changeProgress();
  191. }
  192. }
  193. }
  194. } else {
  195. this.data.videoContext.seek(this.data.nodeTime)
  196. }
  197. },
  198. scanOk(e) {
  199. this.data.scanOkTime = getEventParam(e, "currentTime");
  200. this.setData({
  201. show: false,
  202. });
  203. setTimeout(() => {
  204. this.data.videoContext.play();
  205. }, 500)
  206. },
  207. });