videoDetails.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <view class="page_body">
  3. <!-- 头部 -->
  4. <nav-bar height="96" title="视频详情" :onlyTitle="true" :showHeaderborder="true" @GoBack="GoBack"></nav-bar>
  5. <view class="page_content">
  6. <view class="video_content">
  7. <!-- duration:指定视频时长,单位为秒(s) initial-time:指定视频初始播放位置,单位为秒(s) -->
  8. <video
  9. ref="myVideoRef"
  10. id="myVideoRef"
  11. :title="state?.videoInfo?.courseName || ''"
  12. :initial-time="state?.videoInfo?.initialTime"
  13. :duration="state?.videoInfo?.duration"
  14. :show-mute-btn="true"
  15. :poster="state?.videoInfo?.videoCoverUrl"
  16. :src="state?.videoInfo?.attachmentUrl"
  17. style="width: 100%; height: 100%; object-fit: contain;"
  18. @timeupdate="videoTimeupdate"
  19. >
  20. </video>
  21. </view>
  22. <view class="video_title">{{state?.videoInfo?.courseName}}</view>
  23. <view class="video_progress">
  24. <view class="video_progress_box">
  25. <text class="video_progress_text" v-if="state?.videoInfo?.courseType===0">必修任务:{{state.videoInfo.courseStatus===0?`剩余${state.videoInfo.remainingTime}`:'已结束'}}</text>
  26. <text class="video_progress_text">观看进度:{{state?.videoInfo?.progress}}</text>
  27. </view>
  28. <view class="task_collect">
  29. <view class="preview">
  30. <image class="img" src="@/static/image/icon/preview.png"></image>
  31. <text class="text">{{state?.videoInfo?.watchCount || 0}}</text>
  32. </view>
  33. <view class="collect" v-if="state?.videoInfo?.courseType===1" @click="ChangeFollowStatus()">
  34. <uni-icons v-if="state?.videoInfo?.followStatus==1" class="img filled" type="heart-filled"></uni-icons>
  35. <uni-icons v-else class="img" type="heart"></uni-icons>
  36. <text :class="['text',{'filled':state?.videoInfo?.followStatus==1}]">{{state?.videoInfo?.followCount || 0}}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="video_class_name">
  41. <text class="class_name_item">{{state?.videoInfo?.teachCourseTypeName}}</text>
  42. <text class="class_name_item">{{state?.videoInfo?.teachCourseTypeClassName}}</text>
  43. <text class="class_name_item">{{state?.videoInfo?.createdBy}}</text>
  44. </view>
  45. <view class="video_time">
  46. <text class="time_item">{{state?.videoInfo?.courseType===0 ?'开始':'上传'}}时间:{{state?.videoInfo?.courseStartDatetime}}</text>
  47. <text class="time_item" v-if="state?.videoInfo?.courseType===0">结束时间:{{state.videoInfo.courseEndDatetime}}</text>
  48. </view>
  49. <view class="video_desc">
  50. 课程简介:{{state?.videoInfo?.courseContent}}
  51. </view>
  52. </view>
  53. </view>
  54. <!-- 提示框 -->
  55. <custom-popup-dialog ref="popupDialogRef" title="试题弹出" dialogWidth="702" :isMaskClick="false" :showDialogClose="false" :showCloseBtn="false" @DialogConfirm="DialogConfirm">
  56. <view class="dialog_questions_input">
  57. <view class="questions_title">{{state?.question?.codeName}}:{{state?.question?.questionsName}}</view>
  58. <uni-easyinput type="textarea" v-model="state.questionsAnswer" :style="state.easyinputStyle" placeholder="请输入你的回答" placeholderStyle="font-weight: 400;font-size: 28rpx;color: #999999;"></uni-easyinput>
  59. </view>
  60. </custom-popup-dialog>
  61. </template>
  62. <script setup>
  63. import uniEasyinput from '@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue';
  64. import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  65. import navBar from '@/components/nav-bar.vue';
  66. import customPopupDialog from '@/components/custom-popup-dialog.vue';
  67. import { selectCourseDetail,addQuestionAnswerData,updateFollowCount,synTeachLearnData } from '@/reqApi/teacherStudy.js';
  68. import {
  69. onLoad,
  70. onUnload
  71. } from '@dcloudio/uni-app';
  72. import {
  73. reactive,
  74. ref,
  75. getCurrentInstance
  76. } from 'vue';
  77. const state = reactive({
  78. id: '', //视频id
  79. videoInfo:{},
  80. currentTime:'',//播放进度时分秒
  81. question:null,//试题弹出信息
  82. easyinputStyle:{
  83. color: '#303133',
  84. borderColor: '#E9E9E9'
  85. },
  86. questionsAnswer:'',
  87. lastValidTime:'',//上一次的播放进度
  88. });
  89. //提示框
  90. const popupDialogRef = ref(null);
  91. const myVideoRef = ref(null);
  92. const videoCtx = ref(null);//播放器
  93. onLoad((option) => {
  94. state.id = option.id;
  95. const instance = getCurrentInstance();
  96. videoCtx.value = uni.createVideoContext('myVideoRef', instance.proxy);
  97. GetCourseDetail();
  98. })
  99. //视频详情
  100. const GetCourseDetail = () => {
  101. selectCourseDetail(state.id).then(res => {
  102. if(res.code == 200){
  103. const resData = res.data;
  104. resData.initialTime = timeToSecond(resData.courseLearnTime ?? '');//视频初始播放位置
  105. state.lastValidTime = resData.initialTime;//上一次播放进度
  106. resData.duration = timeToSecond(resData.videoTime ?? '');//视频时长,单位为秒(s)
  107. if(resData?.questionsList?.length > 0){
  108. resData.questionsList.forEach(item=>{
  109. const showTimeDate = timeToSecond(item.showTime);
  110. item.isWriteAnswer = showTimeDate < resData.initialTime;//是否已作答 true 已作答
  111. })
  112. }
  113. state.videoInfo = resData;
  114. }
  115. })
  116. }
  117. //计算秒数
  118. const timeToSecond = (timeStr) => {
  119. if(timeStr == '') return 0
  120. const timeArr = timeStr.split(':');
  121. const [h, m, s] = timeArr.map(item => Number(item));
  122. return h * 3600 + m * 60 + s;
  123. }
  124. //播放进度变化时触发,event.detail = {currentTime, duration} 。触发频率 250ms 一次
  125. const videoTimeupdate = (event) => {
  126. const currentTime = event.detail.currentTime;
  127. // 1. 判断:如果当前时间小于记录值,说明在回退
  128. const diff = (Number(currentTime) - Number(state.lastValidTime)).toFixed(2);
  129. const dragStatus = state.videoInfo.dragStatus || 0;//0 不可拖拽、默认不可拖拽
  130. if (diff > 0.35 && dragStatus == 0) {
  131. videoCtx.value.seek(state.lastValidTime);
  132. }
  133. state.currentTime = formatSecondsToHms(currentTime);
  134. const questionsList = state?.videoInfo?.questionsList || [];
  135. const question = questionsList.find(item=>item.showTime == state.currentTime);//视频显示的时间
  136. if(question && !question.isWriteAnswer){//未作答
  137. videoCtx?.value?.pause();//暂停播放
  138. state.questionsAnswer = '';
  139. state.question = question;//试题弹框信息
  140. popupDialogRef.value.OpenDialog();
  141. }
  142. state.lastValidTime = currentTime;//上一次播放进度
  143. // 教师学习进度更新接口
  144. setSynTeachLearnData();
  145. }
  146. //提交试题
  147. const DialogConfirm = () => {
  148. if(state.questionsAnswer == ''){
  149. uni.showToast({
  150. title: '请输入答案!',
  151. icon: 'none'
  152. })
  153. return false
  154. }
  155. addQuestionAnswerData({
  156. teachCourseId: state.id,
  157. teachQuestionsId: state?.question?.id || '',
  158. teachQuestionsAnswer: state.questionsAnswer
  159. }).then(res=>{
  160. if(res.code == 200){
  161. state.question.isWriteAnswer = true;//状态修改成已作答
  162. popupDialogRef.value.CloseDialog();//关闭弹框
  163. videoCtx?.value?.play();//播放
  164. }else{
  165. uni.showToast({
  166. title: res.msg || '请求异常',
  167. icon: 'none'
  168. })
  169. }
  170. })
  171. }
  172. //教师学习进度更新接口
  173. const setSynTeachLearnData = () => {
  174. const videoTime = state.videoInfo.videoTime ?? '';//视频时长
  175. // 学习状态: 1-已结束(原已过期状态-课程结束了,但是用户没学完),2-待开始,3-未完成,4-已完成
  176. const learnStatus = state.videoInfo.learnStatus ?? 3;//视频时长
  177. synTeachLearnData({
  178. teachCourseId:state.id,
  179. courseLearnTime:state.currentTime,
  180. learnStatus:state.currentTime==videoTime && learnStatus!=1 && learnStatus!=4?4:learnStatus
  181. })
  182. }
  183. // 数字补零工具
  184. const addZero = (num) => {
  185. return num < 10 ? '0' + num : String(num);
  186. }
  187. //转换时分秒
  188. const formatSecondsToHms = (seconds) => {
  189. let total = Math.floor(seconds);
  190. const h = Math.floor(total / 3600);
  191. const m = Math.floor((total % 3600) / 60);
  192. const s = total % 60;
  193. const hh = addZero(h);
  194. const mm = addZero(m);
  195. const ss = addZero(s);
  196. return `${hh}:${mm}:${ss}`;
  197. }
  198. //收藏
  199. const ChangeFollowStatus = () => {
  200. updateFollowCount(state.id).then(res=>{
  201. if(res.code == 200){
  202. state.videoInfo.followStatus = state.videoInfo.followStatus===1?0:1;
  203. state.videoInfo.followCount = res.data;
  204. }
  205. })
  206. }
  207. //页面销毁时触发
  208. onUnload(()=>{
  209. videoCtx?.value?.pause();//暂停播放
  210. })
  211. //返回
  212. const GoBack = () => {
  213. uni.navigateBack({
  214. delta: 1
  215. });
  216. }
  217. </script>
  218. <style scoped lang="scss">
  219. .video_content {
  220. width: 100%;
  221. border-radius: 8rpx;
  222. margin-top: 24rpx;
  223. height: 398rpx;
  224. overflow: hidden;
  225. }
  226. .video_title{
  227. width: 100%;
  228. margin-top: 32rpx;
  229. font-weight: 500;
  230. font-size: 36rpx;
  231. color: #333333;
  232. display: -webkit-box;
  233. -webkit-line-clamp: 2;
  234. -webkit-box-orient: vertical;
  235. overflow: hidden;
  236. text-overflow: ellipsis;
  237. }
  238. .video_progress{
  239. display: flex;
  240. justify-content: space-between;
  241. width: 100%;
  242. margin-top: 16rpx;
  243. align-items: flex-start;
  244. .video_progress_box{
  245. display: inline-flex;
  246. flex-wrap: wrap;
  247. gap: 20rpx;
  248. .video_progress_text{
  249. font-weight: 400;
  250. font-size: 24rpx;
  251. color: #2E64FA;
  252. }
  253. }
  254. .task_collect {
  255. display: inline-flex;
  256. gap: 20rpx;
  257. .preview,
  258. .collect {
  259. display: inline-flex;
  260. align-items: center;
  261. }
  262. .img {
  263. width: 24rpx;
  264. height: 24rpx;
  265. margin-right: 12rpx;
  266. font-size: 24rpx !important;
  267. color: #999999 !important;
  268. &.filled{
  269. color: #2e64fa !important;
  270. }
  271. }
  272. .text {
  273. font-weight: 400;
  274. font-size: 24rpx;
  275. color: #999999;
  276. &.filled{
  277. color: #2e64fa;
  278. }
  279. }
  280. }
  281. }
  282. .video_class_name{
  283. display: flex;
  284. width: 100%;
  285. margin-top: 16rpx;
  286. gap: 16rpx 24rpx;
  287. flex-wrap: wrap;
  288. .class_name_item{
  289. font-weight: 400;
  290. font-size: 24rpx;
  291. color: #666666;
  292. }
  293. }
  294. .video_time{
  295. display: flex;
  296. width: 100%;
  297. margin-top: 16rpx;
  298. gap: 16rpx;
  299. flex-wrap: wrap;
  300. .time_item{
  301. font-weight: 400;
  302. font-size: 24rpx;
  303. color: #666666;
  304. }
  305. }
  306. .video_desc{
  307. display: flex;
  308. width: 100%;
  309. margin-top: 32rpx;
  310. flex-wrap: wrap;
  311. word-break: break-all;
  312. font-weight: 400;
  313. font-size: 24rpx;
  314. color: #999999;
  315. line-height: 44rpx;
  316. }
  317. .dialog_questions_input{
  318. display: flex;
  319. width: 100%;
  320. flex-direction: column;
  321. .questions_title{
  322. display: flex;
  323. width: 100%;
  324. font-weight: 400;
  325. font-size: 32rpx;
  326. color: #303133;
  327. text-align: left;
  328. word-break: break-all;
  329. }
  330. :deep(.uni-easyinput){
  331. margin-top: 20rpx;
  332. font-size: 28rpx;
  333. .is-input-border{
  334. border-radius: 8rpx;
  335. border-color: #E9E9E9 !important;
  336. }
  337. .uni-easyinput__content-textarea{
  338. height: 440rpx;
  339. min-height: 440rpx;
  340. font-size: 28rpx;
  341. margin:20rpx 24rpx 20rpx 14rpx;
  342. }
  343. .input-padding{
  344. padding-left: 10rpx;
  345. }
  346. }
  347. }
  348. </style>