index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. <!-- :duration="state?.videoInfo?.duration" -->
  9. <video
  10. ref="myVideoRef"
  11. id="myVideoRef"
  12. :title="state?.videoInfo?.courseName || ''"
  13. :initial-time="state?.videoInfo?.initialTime"
  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. <customPopupDialog ref="popupDialogRef" title="试题弹出" dialogWidth="702" :isMaskClick="false" :showDialogClose="false" :showCancelBtn="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. </customPopupDialog>
  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/navBar.vue';
  66. import customPopupDialog from '@/components/customPopupDialog.vue';
  67. import { selectCourseDetail,addQuestionAnswerData,updateFollowCount,synTeachLearnData } from '@/reqApi/teacherStudy.js';
  68. import {
  69. onLoad,
  70. onUnload,
  71. onShow
  72. } from '@dcloudio/uni-app';
  73. import {
  74. reactive,
  75. ref,
  76. getCurrentInstance
  77. } from 'vue';
  78. const state = reactive({
  79. id: '', //视频id
  80. videoInfo:{},
  81. currentTime:'',//播放进度时分秒
  82. question:null,//试题弹出信息
  83. easyinputStyle:{
  84. color: '#303133',
  85. borderColor: '#E9E9E9'
  86. },
  87. questionsAnswer:'',
  88. lastValidTime:'',//上一次的播放进度
  89. });
  90. //提示框
  91. const popupDialogRef = ref(null);
  92. const myVideoRef = ref(null);
  93. const videoCtx = ref(null);//播放器
  94. onLoad((option) => {
  95. state.id = option.id;
  96. const instance = getCurrentInstance();
  97. videoCtx.value = uni.createVideoContext('myVideoRef', instance.proxy);
  98. GetCourseDetail();
  99. })
  100. onShow(() => {
  101. // #ifdef APP-PLUS
  102. const answer = uni.getStorageSync('videoDetailQuestionPopupSubmit');
  103. if(answer){
  104. state.questionsAnswer = answer?answer:'';
  105. uni.removeStorageSync('videoDetailQuestionPopupSubmit');
  106. DialogConfirm();
  107. }
  108. // #endif
  109. })
  110. //视频详情
  111. const GetCourseDetail = () => {
  112. selectCourseDetail(state.id).then(res => {
  113. if(res.code == 200){
  114. const resData = res.data;
  115. resData.initialTime = timeToSecond(resData.courseLearnTime ?? '');//视频初始播放位置
  116. state.lastValidTime = timeToSecond(resData.courseLearnTime ?? '');
  117. resData.duration = timeToSecond(resData.videoTime ?? '');//视频时长,单位为秒(s)
  118. if(resData?.questionsList?.length > 0){
  119. resData.questionsList.forEach(item=>{
  120. const showTimeDate = timeToSecond(item.showTime);
  121. item.isWriteAnswer = showTimeDate < resData.initialTime;//是否已作答 true 已作答
  122. })
  123. }
  124. state.videoInfo = resData;
  125. }
  126. })
  127. }
  128. //计算秒数
  129. const timeToSecond = (timeStr) => {
  130. if(timeStr == '') return 0
  131. const timeArr = timeStr.split(':');
  132. const [h, m, s] = timeArr.map(item => Number(item));
  133. return h * 3600 + m * 60 + s;
  134. }
  135. //播放进度变化时触发,event.detail = {currentTime, duration} 。触发频率 250ms 一次
  136. const videoTimeupdate = (event) => {
  137. const currentTime = event.detail.currentTime;
  138. if(currentTime > 0){
  139. // 1. 判断:如果当前时间小于记录值,说明在回退
  140. const diff = (Number(currentTime) - Number(state.lastValidTime)).toFixed(2);
  141. const dragStatus = state.videoInfo.dragStatus || 0;//0 不可拖拽、默认不可拖拽
  142. const learnStatus = state.videoInfo.learnStatus ?? 3;//学习状态
  143. const progress = state.videoInfo.progress;//进度
  144. if (diff > 0.35 && dragStatus == 0 && (learnStatus!=4 || (learnStatus!=1 && progress!='100%'))) {
  145. videoCtx.value.seek(state.lastValidTime);
  146. }
  147. state.currentTime = formatSecondsToHms(currentTime);
  148. const questionsList = state?.videoInfo?.questionsList || [];
  149. const question = questionsList.find(item=>item.showTime == state.currentTime);//视频显示的时间
  150. const isSelf = state?.videoInfo?.courseType==1// 0任务学习 1 自主学习 自主学习不弹试题
  151. if(question && !isSelf && !question.isWriteAnswer){//未作答
  152. videoCtx?.value?.pause();//暂停播放
  153. state.questionsAnswer = '';
  154. state.question = question;//试题弹框信息
  155. // #ifdef APP-PLUS
  156. //打开nvue弹窗页面,把question对象编码传过去
  157. uni.setStorageSync('videoDetailSendQuestion', JSON.stringify(question))
  158. setTimeout(()=>{
  159. uni.navigateTo({
  160. url:'/pages/teacherStudy/videoDetails/subNVue/questionPopup'
  161. })
  162. },0)
  163. // #endif
  164. // #ifndef APP-PLUS
  165. popupDialogRef.value.OpenDialog();
  166. // #endif
  167. }
  168. state.lastValidTime = currentTime;//上一次播放进度
  169. // 教师学习进度更新接口
  170. if(learnStatus!=4 || (learnStatus!=1 && progress!='100%')){
  171. state.videoInfo.progress = Math.min(currentTime / Number(state.videoInfo.duration) * 100, 100).toFixed(2) + '%';
  172. setSynTeachLearnData();
  173. }
  174. }
  175. }
  176. //提交试题
  177. const DialogConfirm = () => {
  178. if(state.questionsAnswer == ''){
  179. uni.showToast({
  180. title: '请输入答案!',
  181. icon: 'none'
  182. })
  183. return false
  184. }
  185. addQuestionAnswerData({
  186. teachCourseId: state.id,
  187. teachQuestionsId: state?.question?.id || '',
  188. teachQuestionsAnswer: state.questionsAnswer
  189. }).then(res=>{
  190. if(res.code == 200){
  191. state.question.isWriteAnswer = true;//状态修改成已作答
  192. popupDialogRef.value.CloseDialog();//关闭弹框
  193. videoCtx?.value?.play();//播放
  194. }else{
  195. uni.showToast({
  196. title: res.msg || '请求异常',
  197. icon: 'none'
  198. })
  199. }
  200. })
  201. }
  202. //教师学习进度更新接口
  203. const setSynTeachLearnData = () => {
  204. const videoTime = state.videoInfo.videoTime ?? '';//视频时长
  205. // 学习状态: 1-已结束(原已过期状态-课程结束了,但是用户没学完),2-待开始,3-未完成,4-已完成
  206. const learnStatus = state.videoInfo.learnStatus ?? 3;
  207. synTeachLearnData({
  208. teachCourseId:state.id,
  209. courseLearnTime:state.currentTime,
  210. learnStatus:state.currentTime==videoTime && learnStatus!=1 && learnStatus!=4?4:learnStatus
  211. }).then(res=>{
  212. if(res.code==200 && state.currentTime==videoTime && learnStatus!=1 && learnStatus!=4){
  213. state.videoInfo.learnStatus = 4;//学习状态
  214. state.videoInfo.progress = '100%';//进度
  215. }
  216. })
  217. }
  218. // 数字补零工具
  219. const addZero = (num) => {
  220. return num < 10 ? '0' + num : String(num);
  221. }
  222. //转换时分秒
  223. const formatSecondsToHms = (seconds) => {
  224. let total = Math.floor(seconds);
  225. const h = Math.floor(total / 3600);
  226. const m = Math.floor((total % 3600) / 60);
  227. const s = total % 60;
  228. const hh = addZero(h);
  229. const mm = addZero(m);
  230. const ss = addZero(s);
  231. return `${hh}:${mm}:${ss}`;
  232. }
  233. //收藏
  234. const ChangeFollowStatus = () => {
  235. updateFollowCount(state.id).then(res=>{
  236. if(res.code == 200){
  237. state.videoInfo.followStatus = state.videoInfo.followStatus===1?0:1;
  238. state.videoInfo.followCount = res.data;
  239. }
  240. })
  241. }
  242. //页面销毁时触发
  243. onUnload(()=>{
  244. videoCtx?.value?.pause();//暂停播放
  245. })
  246. //返回
  247. const GoBack = () => {
  248. uni.navigateBack({
  249. delta: 1
  250. });
  251. }
  252. </script>
  253. <style scoped lang="scss">
  254. .video_content {
  255. width: 100%;
  256. border-radius: 8rpx;
  257. margin-top: 24rpx;
  258. height: 398rpx;
  259. overflow: hidden;
  260. }
  261. .video_title{
  262. width: 100%;
  263. margin-top: 32rpx;
  264. font-weight: 500;
  265. font-size: 36rpx;
  266. color: #333333;
  267. display: -webkit-box;
  268. -webkit-line-clamp: 2;
  269. -webkit-box-orient: vertical;
  270. overflow: hidden;
  271. text-overflow: ellipsis;
  272. }
  273. .video_progress{
  274. display: flex;
  275. justify-content: space-between;
  276. width: 100%;
  277. margin-top: 16rpx;
  278. align-items: flex-start;
  279. .video_progress_box{
  280. display: inline-flex;
  281. flex-wrap: wrap;
  282. gap: 20rpx;
  283. .video_progress_text{
  284. font-weight: 400;
  285. font-size: 24rpx;
  286. color: #2E64FA;
  287. }
  288. }
  289. .task_collect {
  290. display: inline-flex;
  291. gap: 20rpx;
  292. .preview,
  293. .collect {
  294. display: inline-flex;
  295. align-items: center;
  296. }
  297. .img {
  298. width: 24rpx;
  299. height: 24rpx;
  300. margin-right: 12rpx;
  301. font-size: 24rpx !important;
  302. color: #999999 !important;
  303. &.filled{
  304. color: #2e64fa !important;
  305. }
  306. }
  307. .text {
  308. font-weight: 400;
  309. font-size: 24rpx;
  310. color: #999999;
  311. &.filled{
  312. color: #2e64fa;
  313. }
  314. }
  315. }
  316. }
  317. .video_class_name{
  318. display: flex;
  319. width: 100%;
  320. margin-top: 16rpx;
  321. gap: 16rpx 24rpx;
  322. flex-wrap: wrap;
  323. .class_name_item{
  324. font-weight: 400;
  325. font-size: 24rpx;
  326. color: #666666;
  327. }
  328. }
  329. .video_time{
  330. display: flex;
  331. width: 100%;
  332. margin-top: 16rpx;
  333. gap: 16rpx;
  334. flex-wrap: wrap;
  335. .time_item{
  336. font-weight: 400;
  337. font-size: 24rpx;
  338. color: #666666;
  339. }
  340. }
  341. .video_desc{
  342. display: flex;
  343. width: 100%;
  344. margin-top: 32rpx;
  345. flex-wrap: wrap;
  346. word-break: break-all;
  347. font-weight: 400;
  348. font-size: 24rpx;
  349. color: #999999;
  350. line-height: 44rpx;
  351. }
  352. .dialog_questions_input{
  353. display: flex;
  354. width: 100%;
  355. flex-direction: column;
  356. .questions_title{
  357. display: flex;
  358. width: 100%;
  359. font-weight: 400;
  360. font-size: 32rpx;
  361. color: #303133;
  362. text-align: left;
  363. word-break: break-all;
  364. }
  365. :deep(.uni-easyinput){
  366. margin-top: 20rpx;
  367. font-size: 28rpx;
  368. .is-input-border{
  369. border-radius: 8rpx;
  370. border-color: #E9E9E9 !important;
  371. }
  372. .uni-easyinput__content-textarea{
  373. height: 440rpx;
  374. min-height: 440rpx;
  375. font-size: 28rpx;
  376. margin:20rpx 24rpx 20rpx 14rpx;
  377. }
  378. .input-padding{
  379. padding-left: 10rpx;
  380. }
  381. }
  382. }
  383. </style>