study.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. console.log('this.list',this.list);
  154. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  155. // 设置默认展开项
  156. this.list[0].open = true;
  157. this.list[0].jieList[0].open = true;
  158. // 设置播放可见
  159. const kejian = this.list[0].jieList[0].kejianList[0];
  160. this.handlePlay(kejian)
  161. }
  162. },
  163. onPause() {
  164. clearInterval(this.timer1);
  165. this.timer1 = null;
  166. },
  167. onPlay() {
  168. clearInterval(this.timer1);
  169. this.timer1 = null;
  170. // this.timer = setInterval(() => {
  171. // updateSectionProgress(this.operId);
  172. // }, 1000 * 1 * 60) // 自动保存进度 1分钟已保存
  173. },
  174. sectionPlayerProgress(progress) {
  175. let sectionPage = getKechengSectionPageFromHistory(this.operId);
  176. let maxProcess = this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1
  177. .kjId == sectionPage.kjId).maxProcess;
  178. // 更新缓存进度
  179. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  180. sectionPage.kjId).curProcess = progress;
  181. if (progress < 100) {
  182. // 播放进度小于100
  183. if (progress < maxProcess) {
  184. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  185. sectionPage.kjId).maxProcess = maxProcess
  186. } else {
  187. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  188. sectionPage.kjId).maxProcess = progress
  189. }
  190. } else {
  191. // 播放进度大于100
  192. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  193. sectionPage.kjId).maxProcess = 100
  194. }
  195. },
  196. onTimeupdate(time) {
  197. const progress = parseInt(time / this.curPlayData.duration * 100);
  198. this.curPlayData.curProgress = parseInt(progress >= 100 ? '99' : progress);
  199. // 保存进度
  200. // saveCourseProgress(time, this.curPlayData.duration, this.operId)
  201. // 更新进度
  202. // this.sectionPlayerProgress(progress)
  203. },
  204. onPlayEnd() {
  205. clearInterval(this.timer1);
  206. this.timer1 = null;
  207. // saveCourseProgress(this.curPlayData.duration, this.curPlayData.duration, this.operId, 'end');
  208. // updateSectionProgress(this.operId, 'end', 'video', () => {
  209. // this.curPlayData.maxProcess = 99;
  210. // this.curPlayData.curProcess = 99;
  211. // });
  212. console.log('end')
  213. },
  214. goUpPage() {
  215. const pages = getCurrentPages();
  216. if (pages.length > 1) {
  217. uni.navigateBack()
  218. } else {
  219. /* if (this.from == 'kechengList') {
  220. uni.redirectTo({
  221. url: '/pages/client/Kecheng/list'
  222. })
  223. } else if (this.from == 'shouye') {
  224. uni.redirectTo({
  225. url: '/pages/client/ShouYe/shouye'
  226. })
  227. } else {
  228. uni.redirectTo({
  229. url: '/pages/client/ShouYe/shouye'
  230. })
  231. } */
  232. history.back();
  233. }
  234. },
  235. onClickItem(e) {
  236. if (this.current !== e.currentIndex) {
  237. this.current = e.currentIndex
  238. }
  239. },
  240. formatData(data) {
  241. data.forEach(zhang => {
  242. zhang.open = false;
  243. zhang.jieList.forEach(jie => {
  244. jie.open = false;
  245. })
  246. })
  247. return data;
  248. },
  249. handlePlay(data) {
  250. this.showVideo = true;
  251. if (this.curPlayData && this.curPlayData.url == data.url) {
  252. return;
  253. }
  254. console.log('data', data);
  255. this.curPlayData = data;
  256. kechengApi.getVideoId({
  257. videoId: data.url
  258. }).then(res => {
  259. this.videoUrl = res.data
  260. console.log('res', res);
  261. // const progress = data.curProcess || 0;
  262. // this.$refs.videoRef.init({
  263. // videoId: data.url,
  264. // playAuth: res.data,
  265. // seekTime: data.duration * progress / 100,
  266. // isPlay: false
  267. // })
  268. })
  269. },
  270. initFirstVideo() {
  271. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  272. // 设置默认展开项
  273. this.list[0].open = true;
  274. this.list[0].jieList[0].open = true;
  275. // 设置播放可见
  276. const kejian = this.list[0].jieList[0].kejianList[0];
  277. // this.handlePlay(kejian)
  278. }
  279. //saveKechengSectionPage(this.operId, sectionPage)
  280. },
  281. init() {
  282. kechengApi.getClientKechengStart({
  283. kcId: this.kcId,
  284. jzId: this.jzId
  285. }).then(res => {
  286. const {
  287. userCount,
  288. period,
  289. name,
  290. kejianUserVo,
  291. intro,
  292. operId,
  293. } = res.data;
  294. this.userCount = userCount;
  295. this.period = formatSecondsToCnhms(period, true);
  296. this.name = name;
  297. this.formatData(kejianUserVo.zhangList)
  298. this.list = kejianUserVo.zhangList;
  299. this.intro = intro;
  300. this.operId = operId;
  301. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  302. //let historyArrKecheng = getKechengDataFromHistory(this.operId)
  303. // let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  304. // // 判断是否有前台缓存
  305. // if (historyArrKecheng && sectionPageHistory) {
  306. // // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  307. // const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  308. // historyArrKecheng);
  309. // // 后台数据 同步前台缓存
  310. // saveKechengData(this.operId, arrKecheng)
  311. // } else {
  312. // // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  313. // saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  314. // }
  315. // 初始化页面 常规数据
  316. //initCourseProgressAll(this.operId)
  317. console.log('初始化播放首1123次')
  318. // 设置播放视频
  319. this.initFirstVideo();
  320. }).catch(err => {
  321. this.goUpPage();
  322. })
  323. }
  324. }
  325. }
  326. </script>
  327. <style>
  328. </style>