study.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <view class="kecheng-study-page">
  3. <customNavbarVue title="课程" :show-back-btn="true" @back="goUpPage"></customNavbarVue>
  4. <!-- 播放器 -->
  5. <!-- <videoPlayVue v-if="showVideo" ref="videoRef" class="phone-video-box" @play-end="onPlayEnd" @play-play="onPlay"
  6. @play-pause="onPause" @play-timeupdate="onTimeupdate"></videoPlayVue> -->
  7. <video v-if="showVideo" :src="videoUrl" :poster="videoPoster" controls class="phone-video-box" @play="onVideoPlay"
  8. @pause="onVideoPause" @ended="onVideoEnd" @error="onVideoError" id="myVideo"></video>
  9. <!-- 视频背景图 -->
  10. <view v-else class="phone-video-box master-image" :style="{ backgroundImage: 'url(' + imgsArr.videoBj + ')' }">
  11. <!-- <view class="video-player-icon" @click="handlePlayFirst"></view> -->
  12. </view>
  13. <!-- 播放引导 -->
  14. <view class="yindao-shadow-image" v-if="!showVideo" :style="{ backgroundImage: 'url(' + imgsArr.pointIcon + ')' }"> </view>
  15. <!-- 中间区域 -->
  16. <view class="kc-name-box">
  17. <view>{{name}}</view>
  18. </view>
  19. <view class="kc-info-box">
  20. <view>时长:{{period}}</view>
  21. <view>{{userCount}}人学习</view>
  22. </view>
  23. <!-- 大纲 -->
  24. <view class="phone-tab-box">
  25. <uni-segmented-control :current="current" :values="items" style-type="text" :active-color="activeColor"
  26. @clickItem="onClickItem" />
  27. </view>
  28. <view class="kecheng-content-box">
  29. <!-- 目录 -->
  30. <kechengMuluVue v-if="current === 0" :chapterArr="list" @play="handlePlay" :isHasProgress="true"
  31. :activeKjId="curPlayData&&curPlayData.kjId"></kechengMuluVue>
  32. <!-- 介绍 -->
  33. <rich-text :nodes="intro || '暂无内容'" v-if="current === 1 && intro" class="kecheng-jieshao-box"></rich-text>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import cacheManager from '@/utils/cacheManager.js';
  39. import * as kechengApi from "@/api/kecheng.js";
  40. import customNavbarVue from "@/components/custom-navbar/custom-navbar.vue";
  41. import videoPlayVue from "@/components/videoPlay/videoPlay.vue";
  42. import kechengMuluVue from "@/components/kecheng-mulu/kecheng-mulu.vue";
  43. import {
  44. useUserCache
  45. } from "@/utils/userCache.js"
  46. import {
  47. formatSecondsToCnhms
  48. } from "@/utils/common.js"
  49. import {
  50. useKechengTools
  51. } from "./useKechengCache.js"
  52. const {
  53. getCurKjIndex,
  54. saveKechengData,
  55. getKechengDataFromHistory,
  56. saveKechengSectionPage,
  57. getKechengSectionPageFromHistory,
  58. mergeProgress,
  59. initCourseProgressAll,
  60. saveCourseProgress,
  61. updateSectionProgress,
  62. } = useKechengTools();
  63. export default {
  64. components: {
  65. videoPlayVue,
  66. kechengMuluVue,
  67. customNavbarVue
  68. },
  69. data() {
  70. return {
  71. showVideo: false,
  72. items: ['目录', '介绍'],
  73. colors: ['#007aff', '#4cd964', '#dd524d'],
  74. activeColor: '#3fd2a1',
  75. current: 0, // 激活的选项卡
  76. operId: '', // 课程
  77. name: '',
  78. period: 0, // 时长
  79. userCount: 0, // 学习人数
  80. list: [],
  81. intro: '',
  82. curPlayData: null,
  83. timer1: null,
  84. kcId: null,
  85. from: '',
  86. jzId: null,
  87. videoUrl: '', // 视频地址
  88. videoPoster: '', // 视频封面
  89. videoInfo: {}, // 视频信息
  90. loading: false, // 加载状态
  91. error: false, // 错误状态
  92. errorMessage: '', // 错误信息
  93. videoContext: null, // 视频上下文
  94. imgsArr: {}
  95. }
  96. },
  97. onLoad(options) {
  98. this.kcId = options.kcId;
  99. this.from = options.from;
  100. this.jzId = options.jzId;
  101. this.imgsArr.videoBj = cacheManager.get('projectImg').video_bj;
  102. this.imgsArr.pointIcon = cacheManager.get('projectImg').course_point_icon;
  103. this.init();
  104. },
  105. methods: {
  106. // 播放视频
  107. playVideo() {
  108. if (this.videoUrl) {
  109. this.videoContext.play()
  110. } else {
  111. uni.showToast({
  112. title: '暂无视频可播放',
  113. icon: 'none'
  114. })
  115. }
  116. },
  117. // 暂停视频
  118. pauseVideo() {
  119. this.videoContext.pause()
  120. },
  121. // 重新播放
  122. replayVideo() {
  123. this.videoContext.seek(0)
  124. this.videoContext.play()
  125. },
  126. // 视频开始播放
  127. onVideoPlay() {
  128. console.log('视频开始播放')
  129. },
  130. // 视频暂停
  131. onVideoPause() {
  132. console.log('视频暂停')
  133. },
  134. // 视频播放结束
  135. onVideoEnd() {
  136. console.log('视频播放结束')
  137. uni.showToast({
  138. title: '播放完成',
  139. icon: 'success'
  140. })
  141. },
  142. // 视频播放错误
  143. onVideoError(e) {
  144. console.error('视频播放错误:', e.detail.errMsg)
  145. this.error = true
  146. this.errorMessage = '视频播放出错,请检查视频地址或格式'
  147. },
  148. // 重新加载视频
  149. retryLoadVideo() {
  150. this.getVideoData()
  151. },
  152. handlePlayFirst() {
  153. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  154. // 设置默认展开项
  155. this.list[0].open = true;
  156. this.list[0].jieList[0].open = true;
  157. // 设置播放可见
  158. const kejian = this.list[0].jieList[0].kejianList[0];
  159. this.handlePlay(kejian)
  160. }
  161. },
  162. onPause() {
  163. clearInterval(this.timer1);
  164. this.timer1 = null;
  165. },
  166. onPlay() {
  167. clearInterval(this.timer1);
  168. this.timer1 = null;
  169. // this.timer = setInterval(() => {
  170. // updateSectionProgress(this.operId);
  171. // }, 1000 * 1 * 60) // 自动保存进度 1分钟已保存
  172. },
  173. sectionPlayerProgress(progress) {
  174. let sectionPage = getKechengSectionPageFromHistory(this.operId);
  175. let maxProcess = this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1
  176. .kjId == sectionPage.kjId).maxProcess;
  177. // 更新缓存进度
  178. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  179. sectionPage.kjId).curProcess = progress;
  180. if (progress < 100) {
  181. // 播放进度小于100
  182. if (progress < maxProcess) {
  183. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  184. sectionPage.kjId).maxProcess = maxProcess
  185. } else {
  186. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  187. sectionPage.kjId).maxProcess = progress
  188. }
  189. } else {
  190. // 播放进度大于100
  191. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  192. sectionPage.kjId).maxProcess = 100
  193. }
  194. },
  195. onTimeupdate(time) {
  196. const progress = parseInt(time / this.curPlayData.duration * 100);
  197. this.curPlayData.curProgress = parseInt(progress >= 100 ? '99' : progress);
  198. // 保存进度
  199. // saveCourseProgress(time, this.curPlayData.duration, this.operId)
  200. // 更新进度
  201. // this.sectionPlayerProgress(progress)
  202. },
  203. onPlayEnd() {
  204. clearInterval(this.timer1);
  205. this.timer1 = null;
  206. // saveCourseProgress(this.curPlayData.duration, this.curPlayData.duration, this.operId, 'end');
  207. // updateSectionProgress(this.operId, 'end', 'video', () => {
  208. // this.curPlayData.maxProcess = 99;
  209. // this.curPlayData.curProcess = 99;
  210. // });
  211. console.log('end')
  212. },
  213. goUpPage() {
  214. const pages = getCurrentPages();
  215. if (pages.length > 1) {
  216. uni.navigateBack()
  217. } else {
  218. /* if (this.from == 'kechengList') {
  219. uni.redirectTo({
  220. url: '/pages/client/Kecheng/list'
  221. })
  222. } else if (this.from == 'shouye') {
  223. uni.redirectTo({
  224. url: '/pages/client/ShouYe/shouye'
  225. })
  226. } else {
  227. uni.redirectTo({
  228. url: '/pages/client/ShouYe/shouye'
  229. })
  230. } */
  231. history.back();
  232. }
  233. },
  234. onClickItem(e) {
  235. if (this.current !== e.currentIndex) {
  236. this.current = e.currentIndex
  237. }
  238. },
  239. formatData(data) {
  240. data.forEach(zhang => {
  241. zhang.open = false;
  242. zhang.jieList.forEach(jie => {
  243. jie.open = false;
  244. })
  245. })
  246. return data;
  247. },
  248. handlePlay(data) {
  249. this.showVideo = true;
  250. if (this.curPlayData && this.curPlayData.url == data.url) {
  251. return;
  252. }
  253. console.log('data', data);
  254. this.curPlayData = data;
  255. kechengApi.getVideoId({
  256. videoId: data.url
  257. }).then(res => {
  258. this.videoUrl = res.data
  259. console.log('res', res);
  260. // const progress = data.curProcess || 0;
  261. // this.$refs.videoRef.init({
  262. // videoId: data.url,
  263. // playAuth: res.data,
  264. // seekTime: data.duration * progress / 100,
  265. // isPlay: false
  266. // })
  267. })
  268. },
  269. initFirstVideo() {
  270. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  271. // 设置默认展开项
  272. this.list[0].open = true;
  273. this.list[0].jieList[0].open = true;
  274. // 设置播放可见
  275. const kejian = this.list[0].jieList[0].kejianList[0];
  276. // this.handlePlay(kejian)
  277. }
  278. //saveKechengSectionPage(this.operId, sectionPage)
  279. },
  280. init() {
  281. kechengApi.getClientKechengStart({
  282. kcId: this.kcId,
  283. jzId: this.jzId
  284. }).then(res => {
  285. const {
  286. userCount,
  287. period,
  288. name,
  289. kejianUserVo,
  290. intro,
  291. operId,
  292. } = res.data;
  293. this.userCount = userCount;
  294. this.period = formatSecondsToCnhms(period, true);
  295. this.name = name;
  296. this.formatData(kejianUserVo.zhangList)
  297. this.list = kejianUserVo.zhangList;
  298. this.intro = intro;
  299. this.operId = operId;
  300. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  301. //let historyArrKecheng = getKechengDataFromHistory(this.operId)
  302. // let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  303. // // 判断是否有前台缓存
  304. // if (historyArrKecheng && sectionPageHistory) {
  305. // // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  306. // const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  307. // historyArrKecheng);
  308. // // 后台数据 同步前台缓存
  309. // saveKechengData(this.operId, arrKecheng)
  310. // } else {
  311. // // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  312. // saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  313. // }
  314. // 初始化页面 常规数据
  315. //initCourseProgressAll(this.operId)
  316. console.log('初始化播放首1123次')
  317. // 设置播放视频
  318. this.initFirstVideo();
  319. }).catch(err => {
  320. this.goUpPage();
  321. })
  322. }
  323. }
  324. }
  325. </script>
  326. <style>
  327. </style>