CommonVideo.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div>
  3. <!-- Video组件 -->
  4. <div id="common-video" class="h-100">
  5. <div :class="{ isShow: control }" class="h-100">
  6. <video
  7. ref="myVideo" :poster="poster" :src="src" :controls="controls" oncontextmenu="return false"
  8. controlslist="nodownload" class="video-box" @timeupdate="timeupdate"
  9. />
  10. <img
  11. src="/static/assets/client-base-v4/image/play.png" alt="播放" class="pointer operate-btn" :class="{ 'fade-out': videoState }"
  12. @click="operateVideo"
  13. />
  14. </div>
  15. </div>
  16. <Modal v-model:show="modal">
  17. <template #header>培训</template>
  18. <div>请点击确认继续培训?</div>
  19. </Modal>
  20. </div>
  21. </template>
  22. <script>
  23. import Common from '../common/Common.js';
  24. export default {
  25. name: 'CommonVideo',
  26. /**
  27. * @param poster 展示图
  28. * @param src 视频资源
  29. * @param controls 是否显示控件
  30. * @param control 控件控制
  31. * @param videoData 视频基础数据
  32. */
  33. props: {
  34. poster: {
  35. type: String,
  36. required: false,
  37. default: '',
  38. },
  39. src: {
  40. type: String,
  41. required: true,
  42. },
  43. controls: {
  44. type: Boolean,
  45. required: false,
  46. default: true,
  47. },
  48. control: {
  49. type: Boolean,
  50. required: false,
  51. default: false,
  52. },
  53. videoData: {
  54. type: Object,
  55. required: true,
  56. },
  57. },
  58. emits: ['videoStudyTime'],
  59. data() {
  60. return {
  61. videoState: false, // 视频播放状态
  62. // 学时
  63. studyTime: {
  64. currentTime: 0, // 当前已学时长
  65. duration: 0, // 总时长
  66. },
  67. timer: {}, // 定时器
  68. pauseTimer: {}, // 暂停定时器
  69. modal: false,
  70. };
  71. },
  72. watch: {
  73. // 监听操作
  74. videoData(val, oldVal) {
  75. const { currentTime, duration } = val;
  76. if (currentTime && duration && currentTime < duration) {
  77. this.hintOperate();
  78. }
  79. },
  80. },
  81. mounted() {
  82. // 监听视频播放
  83. this.$refs.myVideo.addEventListener('play', () => {
  84. console.log('video is playing');
  85. this.openTimer();
  86. });
  87. // 监听视频暂停
  88. this.$refs.myVideo.addEventListener('pause', () => {
  89. console.log('video is stop');
  90. this.closeTimer();
  91. });
  92. },
  93. methods: {
  94. // 开启定时器
  95. openTimer() {
  96. this.timer = setInterval(() => {
  97. this.$emit('videoStudyTime', this.studyTime);
  98. }, 5000);
  99. },
  100. // 关闭定时器
  101. closeTimer() {
  102. clearInterval(this.timer);
  103. this.$emit('videoStudyTime', this.studyTime);
  104. },
  105. // 开启暂停定时器
  106. openPauseTimer() {
  107. this.pauseTimer = setInterval(() => {
  108. this.hintOperate();
  109. }, 1000);
  110. },
  111. // 关闭暂停定时器
  112. closePauseTimer() {
  113. clearInterval(this.pauseTimer);
  114. },
  115. // 提示操作
  116. hintOperate() {
  117. this.modal = true;
  118. this.operateVideo();
  119. // this.$alert("请点击确认继续学习", "提示", {
  120. // confirmButtonText: "确定",
  121. // confirmButtonClass: "hint-btn",
  122. // showClose: false,
  123. // callback: action => {
  124. // this.$refs.myVideo.currentTime = this.videoData.currentTime;
  125. // this.operateVideo();
  126. // this.openPauseTimer();
  127. // }
  128. // });
  129. },
  130. // 获取当前播放位置
  131. timeupdate(e) {
  132. this.studyTime.currentTime = e.target.currentTime;
  133. this.studyTime.duration = e.target.duration ? e.target.duration : 0;
  134. },
  135. // 操作视频播放、暂停
  136. operateVideo() {
  137. if (!this.src) {
  138. // this.$message({ message: "暂无视频资源,请查看其他视频!" });
  139. return false;
  140. }
  141. if (this.$refs.myVideo.paused) {
  142. this.$refs.myVideo.play();
  143. this.videoState = true;
  144. } else {
  145. this.$refs.myVideo.pause();
  146. this.videoState = false;
  147. }
  148. },
  149. },
  150. };
  151. </script>
  152. <style>
  153. #common-video {
  154. position: relative;
  155. }
  156. .video-box {
  157. box-sizing: border-box;
  158. border: 0;
  159. display: block;
  160. width: 100%;
  161. height: 650px;
  162. outline: none !important;
  163. }
  164. video::-webkit-media-controls-timeline {
  165. display: none;
  166. }
  167. video::-webkit-media-controls-play-button {
  168. visibility: hidden;
  169. }
  170. .operate-btn {
  171. display: block;
  172. width: 60px;
  173. height: 60px;
  174. position: absolute;
  175. top: calc(50% - 30px);
  176. left: calc(50% - 30px);
  177. }
  178. .operate-btn:hover {
  179. opacity: 0.8;
  180. }
  181. .fade-out {
  182. opacity: 0;
  183. }
  184. </style>