index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!--
  2. * @Description: 课程视频
  3. * @Author: 空白格
  4. * @Date: 2022-08-24 13:48:47
  5. * @LastEditors: 空白格
  6. * @LastEditTime: 2022-12-16 11:53:22
  7. * @FilePath: \veterans_client_web\src\components\CourseVideo\index.vue
  8. * @Copyright: Copyright (c) 2016~2022 by 空白格, All Rights Reserved.
  9. -->
  10. <template>
  11. <div class="course-video">
  12. <div class="course-video-header">
  13. <div class="course-video-header-left">
  14. <div class="subtitle" v-if="courseDetails.major">
  15. {{ courseDetails.major || "-" }}
  16. </div>
  17. <div class="title">{{ currentClasses.chapterName || "-" }}</div>
  18. </div>
  19. <div class="course-video-header-right">
  20. 本课程 共{{ courseDetails.amount || 0 }}课,已学完{{
  21. courseDetails.finishCount || 0
  22. }}课,共进度{{ courseDetails.finishPercent || 0 }}%
  23. </div>
  24. </div>
  25. <div class="course-video-box">
  26. <el-row>
  27. <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="16">
  28. <div class="course-video-box-left" v-if="isPay">
  29. <video
  30. ref="video"
  31. id="myVideo"
  32. @timeupdate="timeUpdate"
  33. :src="currentClasses.videoUrl"
  34. controls
  35. :initial-time="videoInfo.initial_time"
  36. object-fit="fill"
  37. play-btn-position="center"
  38. @ended="ended"
  39. @click="videoClick"
  40. @loadedmetadata="loadedmetadata"
  41. ></video>
  42. </div>
  43. </el-col>
  44. <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8">
  45. <div class="course-video-box-right">
  46. <div class="cvbr-top">
  47. <div class="cvbr-top-teacher">
  48. 主讲老师:{{ courseDetails.presenter || "-" }}
  49. </div>
  50. <div class="cvbr-top-des">
  51. {{ currentClasses.chapterInfo || "-" }}
  52. </div>
  53. </div>
  54. <div class="cvbr-bottom">
  55. <el-timeline class="cvbr-bottom-list">
  56. <el-timeline-item
  57. v-for="(course, index) in courseDetails.chapterList"
  58. :key="index"
  59. timestamp=""
  60. :color="course.id === currentClasses.id ? '#EF651F' : ''"
  61. >
  62. <div
  63. class="cvbr-bottom-list-item"
  64. :class="{ current: course.id === currentClasses.id }"
  65. @click="chooseClasses(course)"
  66. >
  67. 第{{ index + 1 }}课({{ course.finishPercent }}%)
  68. </div>
  69. </el-timeline-item>
  70. </el-timeline>
  71. </div>
  72. </div>
  73. </el-col>
  74. </el-row>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. import { submitClassesDuration } from "@/api/AdaptiveTraining";
  80. export default {
  81. name: "CourseVideoIndex",
  82. props: {
  83. courseDetails: {
  84. type: Object,
  85. default: () => {},
  86. },
  87. },
  88. data() {
  89. return {
  90. currentClasses: {},
  91. videoInfo: {
  92. initial_time: 0,
  93. duration: 0,
  94. playedTime: 0,
  95. video_real_time: 0,
  96. },
  97. isPay: false,
  98. };
  99. },
  100. watch: {
  101. "courseDetails.id"(val) {
  102. this.currentClasses = this.courseDetails.chapterList[0];
  103. this.videoInfo.playedTime =
  104. this.courseDetails.chapterList[0].playDuration;
  105. this.isPay = true;
  106. this.$nextTick(() => {
  107. let video = document.getElementById("myVideo");
  108. video.onpause = () => {
  109. this.submitVideoDuration();
  110. };
  111. });
  112. },
  113. },
  114. methods: {
  115. /**
  116. * 选择课程播放
  117. * @date 2022-08-24
  118. * @param {any} item
  119. * @returns {any}
  120. */
  121. chooseClasses(item) {
  122. this.isPay = false;
  123. this.submitVideoDuration(item, true);
  124. },
  125. /**
  126. * 视频播放实时触发
  127. * @date 2022-08-24
  128. * @param {any} e
  129. * @returns {any}
  130. */
  131. timeUpdate(e) {
  132. let detail = e.target;
  133. //播放的总时长
  134. let duration = detail.duration;
  135. //实时播放进度 秒数
  136. let jumpTime = parseInt(detail.currentTime);
  137. //当前视频进度
  138. if (jumpTime - this.videoInfo.playedTime > 3) {
  139. // 差别过大,调用seek方法跳转到实际观看时间
  140. this.$refs.video.currentTime = this.videoInfo.playedTime;
  141. this.$message.warning("未完整看完该视频,不能快进!");
  142. } else {
  143. this.videoInfo.video_real_time = parseInt(detail.currentTime);
  144. if (this.videoInfo.video_real_time > this.videoInfo.playedTime) {
  145. this.videoInfo.playedTime = this.videoInfo.video_real_time;
  146. }
  147. }
  148. },
  149. /**
  150. * 视频结束触发
  151. * @date 2022-08-24
  152. * @returns {any}
  153. */
  154. ended() {
  155. // this.submitVideoDuration();
  156. },
  157. /**
  158. * 视频点击触发
  159. * @date 2022-08-24
  160. * @param {any} e
  161. * @returns {any}
  162. */
  163. videoClick(e) {
  164. console.log(e);
  165. },
  166. /**
  167. * 视频加载完成触发
  168. * @date 2022-08-24
  169. * @param {any} details
  170. * @returns {any}
  171. */
  172. loadedmetadata(details) {
  173. this.videoInfo.duration = details.target.duration;
  174. this.videoInfo.initial_time = this.currentClasses.currentDuration;
  175. this.$nextTick(() => {
  176. this.$refs.video.currentTime = this.videoInfo.initial_time;
  177. });
  178. },
  179. /**
  180. * 提交视频播放进度
  181. * @date 2022-08-24
  182. * @returns {any}
  183. */
  184. submitVideoDuration(item, isNeed) {
  185. let playDuration = this.videoInfo.video_real_time,
  186. currentDuration = this.videoInfo.video_real_time;
  187. if (this.videoInfo.playDuration > this.videoInfo.video_real_time) {
  188. playDuration = this.videoInfo.playDuration;
  189. }
  190. // 防止视频未加载完成就提交
  191. if (Number(this.videoInfo.duration) > 0 && Number(playDuration) > 0) {
  192. submitClassesDuration({
  193. tabId: this.currentClasses.id,
  194. playDuration: playDuration,
  195. currentDuration: currentDuration,
  196. duration: this.videoInfo.duration,
  197. }).then((res) => {
  198. if (res.code === 200) {
  199. console.log("已记录播放时长" + JSON.stringify(res.data));
  200. if (isNeed) {
  201. this.$emit("changeClasses", item);
  202. this.currentClasses = item;
  203. this.isPay = true;
  204. this.videoInfo.playedTime = item.playDuration;
  205. this.videoInfo.initial_time = item.currentDuration;
  206. this.$nextTick(() => {
  207. let video = document.getElementById("myVideo");
  208. video.onpause = () => {
  209. this.submitVideoDuration();
  210. };
  211. });
  212. }
  213. }
  214. });
  215. }
  216. },
  217. },
  218. };
  219. </script>
  220. <style lang="scss" scoped>
  221. .course-video {
  222. padding: 10px 0 40px;
  223. &-header {
  224. display: flex;
  225. justify-content: space-between;
  226. align-items: center;
  227. color: #fff;
  228. padding: 10px 0;
  229. &-left {
  230. display: flex;
  231. align-items: center;
  232. .subtitle {
  233. padding: 7px 23px;
  234. background-color: #ef651f;
  235. font-size: 14px;
  236. border-radius: 20px;
  237. margin-right: 11px;
  238. }
  239. .title {
  240. font-size: 20px;
  241. line-height: 33px;
  242. }
  243. }
  244. &-right {
  245. font-size: 12px;
  246. line-height: 33px;
  247. }
  248. }
  249. &-box {
  250. width: 100%;
  251. &-left {
  252. width: 100%;
  253. video {
  254. width: 100%;
  255. background-color: #fff;
  256. aspect-ratio: 16 / 9;
  257. }
  258. }
  259. &-right {
  260. .cvbr-top {
  261. padding: 10px 25px;
  262. background: #4a5053;
  263. color: #fff;
  264. &-teacher {
  265. font-size: 20px;
  266. margin-bottom: 10px;
  267. }
  268. &-des {
  269. font-size: 14px;
  270. line-height: 25px;
  271. }
  272. }
  273. .cvbr-bottom {
  274. padding: 20px 30px;
  275. max-height: 40vh;
  276. overflow-y: auto;
  277. // &::-webkit-scrollbar {
  278. // width: 0;
  279. // }
  280. &-list {
  281. &-item {
  282. color: #fff;
  283. cursor: pointer;
  284. font-size: 14px;
  285. }
  286. .current {
  287. color: #ef651f;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. </style>