study.vue 11 KB

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