videoDetails.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. <!-- 提示框 -->
  54. <custom-popup-dialog ref="popupDialogRef" title="试题弹出" dialogWidth="702" :isMaskClick="false" :showDialogClose="false" :showCloseBtn="false" @DialogConfirm="DialogConfirm">
  55. <view class="dialog_questions_input">
  56. <view class="questions_title">{{state?.question?.codeName}}:{{state?.question?.questionsName}}</view>
  57. <uni-easyinput type="textarea" v-model="state.questionsAnswer" :style="state.easyinputStyle" placeholder="请输入你的回答" placeholderStyle="font-weight: 400;font-size: 28rpx;color: #999999;"></uni-easyinput>
  58. </view>
  59. </custom-popup-dialog>
  60. </view>
  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 { teacherSelectCourseDetail,addQuestionAnswerData,updateFollowCountTeacher,synTeachLearnData } from '@/reqApi/studentStudy.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. teacherSelectCourseDetail(state.id).then(res => {
  102. if(res.code == 200){
  103. const resData = res.data;
  104. resData.initialTime = timeToSecond(resData.courseLearnTime ?? '');//视频初始播放位置
  105. state.lastValidTime = timeToSecond(resData.courseLearnTime ?? '');
  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. if(currentTime > 0){
  128. // 1. 判断:如果当前时间小于记录值,说明在回退
  129. const diff = (Number(currentTime) - Number(state.lastValidTime)).toFixed(2);
  130. const dragStatus = state.videoInfo.dragStatus || 0;//0 不可拖拽、默认不可拖拽
  131. const learnStatus = state.videoInfo.learnStatus ?? 3;//学习状态
  132. const progress = state.videoInfo.progress;//进度
  133. if (diff > 0.35 && dragStatus == 0 && (learnStatus!=4 || (learnStatus!=1 && progress!='100%'))) {
  134. videoCtx.value.seek(state.lastValidTime);
  135. }
  136. state.currentTime = formatSecondsToHms(currentTime);
  137. const questionsList = state?.videoInfo?.questionsList || [];
  138. const question = questionsList.find(item=>item.showTime == state.currentTime);//视频显示的时间
  139. if(question && !question.isWriteAnswer){//未作答
  140. videoCtx?.value?.pause();//暂停播放
  141. state.questionsAnswer = '';
  142. state.question = question;//试题弹框信息
  143. popupDialogRef.value.OpenDialog();
  144. }
  145. state.lastValidTime = currentTime;//上一次播放进度
  146. // 教师学习进度更新接口
  147. if(learnStatus!=4 || (learnStatus!=1 && progress!='100%')){
  148. state.videoInfo.progress = Math.min(currentTime / Number(state.videoInfo.duration) * 100, 100).toFixed(2) + '%';
  149. setSynTeachLearnData();
  150. }
  151. }
  152. }
  153. //提交试题
  154. const DialogConfirm = () => {
  155. if(state.questionsAnswer == ''){
  156. uni.showToast({
  157. title: '请输入答案!',
  158. icon: 'none'
  159. })
  160. return false
  161. }
  162. addQuestionAnswerData({
  163. teachCourseId: state.id,
  164. teachQuestionsId: state?.question?.id || '',
  165. teachQuestionsAnswer: state.questionsAnswer
  166. }).then(res=>{
  167. if(res.code == 200){
  168. state.question.isWriteAnswer = true;//状态修改成已作答
  169. popupDialogRef.value.CloseDialog();//关闭弹框
  170. videoCtx?.value?.play();//播放
  171. }else{
  172. uni.showToast({
  173. title: res.msg || '请求异常',
  174. icon: 'none'
  175. })
  176. }
  177. })
  178. }
  179. //教师学习进度更新接口
  180. const setSynTeachLearnData = () => {
  181. const videoTime = state.videoInfo.videoTime ?? '';//视频时长
  182. // 学习状态: 1-已结束(原已过期状态-课程结束了,但是用户没学完),2-待开始,3-未完成,4-已完成
  183. const learnStatus = state.videoInfo.learnStatus ?? 3;
  184. synTeachLearnData({
  185. teachCourseId:state.id,
  186. courseLearnTime:state.currentTime,
  187. learnStatus:state.currentTime==videoTime && learnStatus!=1 && learnStatus!=4?4:learnStatus
  188. }).then(res=>{
  189. if(res.code==200 && state.currentTime==videoTime && learnStatus!=1 && learnStatus!=4){
  190. state.videoInfo.learnStatus = 4;//学习状态
  191. state.videoInfo.progress = '100%';//进度
  192. }
  193. })
  194. }
  195. // 数字补零工具
  196. const addZero = (num) => {
  197. return num < 10 ? '0' + num : String(num);
  198. }
  199. //转换时分秒
  200. const formatSecondsToHms = (seconds) => {
  201. let total = Math.floor(seconds);
  202. const h = Math.floor(total / 3600);
  203. const m = Math.floor((total % 3600) / 60);
  204. const s = total % 60;
  205. const hh = addZero(h);
  206. const mm = addZero(m);
  207. const ss = addZero(s);
  208. return `${hh}:${mm}:${ss}`;
  209. }
  210. //收藏
  211. const ChangeFollowStatus = () => {
  212. updateFollowCountTeacher(state.id).then(res=>{
  213. if(res.code == 200){
  214. state.videoInfo.followStatus = state.videoInfo.followStatus===1?0:1;
  215. state.videoInfo.followCount = res.data;
  216. }
  217. })
  218. }
  219. //页面销毁时触发
  220. onUnload(()=>{
  221. videoCtx?.value?.pause();//暂停播放
  222. })
  223. //返回
  224. const GoBack = () => {
  225. uni.navigateBack({
  226. delta: 1
  227. });
  228. }
  229. </script>
  230. <style scoped lang="scss">
  231. .video_content {
  232. width: 100%;
  233. border-radius: 8rpx;
  234. margin-top: 24rpx;
  235. height: 398rpx;
  236. overflow: hidden;
  237. }
  238. .video_title{
  239. width: 100%;
  240. margin-top: 32rpx;
  241. font-weight: 500;
  242. font-size: 36rpx;
  243. color: #333333;
  244. display: -webkit-box;
  245. -webkit-line-clamp: 2;
  246. -webkit-box-orient: vertical;
  247. overflow: hidden;
  248. text-overflow: ellipsis;
  249. }
  250. .video_progress{
  251. display: flex;
  252. justify-content: space-between;
  253. width: 100%;
  254. margin-top: 16rpx;
  255. align-items: flex-start;
  256. .video_progress_box{
  257. display: inline-flex;
  258. flex-wrap: wrap;
  259. gap: 20rpx;
  260. .video_progress_text{
  261. font-weight: 400;
  262. font-size: 24rpx;
  263. color: #2E64FA;
  264. }
  265. }
  266. .task_collect {
  267. display: inline-flex;
  268. gap: 20rpx;
  269. .preview,
  270. .collect {
  271. display: inline-flex;
  272. align-items: center;
  273. }
  274. .img {
  275. width: 24rpx;
  276. height: 24rpx;
  277. margin-right: 12rpx;
  278. font-size: 24rpx !important;
  279. color: #999999 !important;
  280. &.filled{
  281. color: #2e64fa !important;
  282. }
  283. }
  284. .text {
  285. font-weight: 400;
  286. font-size: 24rpx;
  287. color: #999999;
  288. &.filled{
  289. color: #2e64fa;
  290. }
  291. }
  292. }
  293. }
  294. .video_class_name{
  295. display: flex;
  296. width: 100%;
  297. margin-top: 16rpx;
  298. gap: 16rpx 24rpx;
  299. flex-wrap: wrap;
  300. .class_name_item{
  301. font-weight: 400;
  302. font-size: 24rpx;
  303. color: #666666;
  304. }
  305. }
  306. .video_time{
  307. display: flex;
  308. width: 100%;
  309. margin-top: 16rpx;
  310. gap: 16rpx;
  311. flex-wrap: wrap;
  312. .time_item{
  313. font-weight: 400;
  314. font-size: 24rpx;
  315. color: #666666;
  316. }
  317. }
  318. .video_desc{
  319. display: flex;
  320. width: 100%;
  321. margin-top: 32rpx;
  322. flex-wrap: wrap;
  323. word-break: break-all;
  324. font-weight: 400;
  325. font-size: 24rpx;
  326. color: #999999;
  327. line-height: 44rpx;
  328. }
  329. .dialog_questions_input{
  330. display: flex;
  331. width: 100%;
  332. flex-direction: column;
  333. .questions_title{
  334. display: flex;
  335. width: 100%;
  336. font-weight: 400;
  337. font-size: 32rpx;
  338. color: #303133;
  339. text-align: left;
  340. word-break: break-all;
  341. }
  342. :deep(.uni-easyinput){
  343. margin-top: 20rpx;
  344. font-size: 28rpx;
  345. .is-input-border{
  346. border-radius: 8rpx;
  347. border-color: #E9E9E9 !important;
  348. }
  349. .uni-easyinput__content-textarea{
  350. height: 440rpx;
  351. min-height: 440rpx;
  352. font-size: 28rpx;
  353. margin:20rpx 24rpx 20rpx 14rpx;
  354. }
  355. .input-padding{
  356. padding-left: 10rpx;
  357. }
  358. }
  359. }
  360. </style>