study.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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"
  8. @play="onVideoPlay" @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="!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. videoUrl: '', // 视频地址
  90. videoPoster: '', // 视频封面
  91. videoInfo: {}, // 视频信息
  92. loading: false, // 加载状态
  93. error: false, // 错误状态
  94. errorMessage: '', // 错误信息
  95. videoContext: null, // 视频上下文
  96. imgsArr: {}
  97. }
  98. },
  99. onLoad(options) {
  100. this.kcId = options.kcId;
  101. this.from = options.from;
  102. this.jzId = options.jzId;
  103. this.imgsArr.videoBj = cacheManager.get('projectImg').video_bj;
  104. this.imgsArr.pointIcon = cacheManager.get('projectImg').course_point_icon;
  105. this.init();
  106. },
  107. methods: {
  108. // 播放视频
  109. playVideo() {
  110. if (this.videoUrl) {
  111. this.videoContext.play()
  112. } else {
  113. uni.showToast({
  114. title: '暂无视频可播放',
  115. icon: 'none'
  116. })
  117. }
  118. },
  119. // 暂停视频
  120. pauseVideo() {
  121. this.videoContext.pause()
  122. },
  123. // 重新播放
  124. replayVideo() {
  125. this.videoContext.seek(0)
  126. this.videoContext.play()
  127. },
  128. // 视频开始播放
  129. onVideoPlay() {
  130. console.log('视频开始播放')
  131. },
  132. // 视频暂停
  133. onVideoPause() {
  134. console.log('视频暂停')
  135. },
  136. // 视频播放结束
  137. onVideoEnd() {
  138. console.log('视频播放结束')
  139. uni.showToast({
  140. title: '播放完成',
  141. icon: 'success'
  142. })
  143. },
  144. // 视频播放错误
  145. onVideoError(e) {
  146. console.error('视频播放错误:', e.detail.errMsg)
  147. this.error = true
  148. this.errorMessage = '视频播放出错,请检查视频地址或格式'
  149. },
  150. // 重新加载视频
  151. retryLoadVideo() {
  152. this.getVideoData()
  153. },
  154. handlePlayFirst() {
  155. console.log('this.list', this.list);
  156. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  157. // 设置默认展开项
  158. this.list[0].open = true;
  159. this.list[0].jieList[0].open = true;
  160. // 设置播放可见
  161. const kejian = this.list[0].jieList[0].kejianList[0];
  162. this.handlePlay(kejian)
  163. }
  164. },
  165. onPause() {
  166. clearInterval(this.timer1);
  167. this.timer1 = null;
  168. },
  169. onPlay() {
  170. clearInterval(this.timer1);
  171. this.timer1 = null;
  172. // this.timer = setInterval(() => {
  173. // updateSectionProgress(this.operId);
  174. // }, 1000 * 1 * 60) // 自动保存进度 1分钟已保存
  175. },
  176. sectionPlayerProgress(progress) {
  177. let sectionPage = getKechengSectionPageFromHistory(this.operId);
  178. let maxProcess = this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1
  179. .kjId == sectionPage.kjId).maxProcess;
  180. // 更新缓存进度
  181. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  182. sectionPage.kjId).curProcess = progress;
  183. if (progress < 100) {
  184. // 播放进度小于100
  185. if (progress < maxProcess) {
  186. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  187. sectionPage.kjId).maxProcess = maxProcess
  188. } else {
  189. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  190. sectionPage.kjId).maxProcess = progress
  191. }
  192. } else {
  193. // 播放进度大于100
  194. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  195. sectionPage.kjId).maxProcess = 100
  196. }
  197. },
  198. onTimeupdate(time) {
  199. const progress = parseInt(time / this.curPlayData.duration * 100);
  200. this.curPlayData.curProgress = parseInt(progress >= 100 ? '99' : progress);
  201. // 保存进度
  202. // saveCourseProgress(time, this.curPlayData.duration, this.operId)
  203. // 更新进度
  204. // this.sectionPlayerProgress(progress)
  205. },
  206. onPlayEnd() {
  207. clearInterval(this.timer1);
  208. this.timer1 = null;
  209. // saveCourseProgress(this.curPlayData.duration, this.curPlayData.duration, this.operId, 'end');
  210. // updateSectionProgress(this.operId, 'end', 'video', () => {
  211. // this.curPlayData.maxProcess = 99;
  212. // this.curPlayData.curProcess = 99;
  213. // });
  214. console.log('end')
  215. },
  216. goUpPage() {
  217. const pages = getCurrentPages();
  218. if (pages.length > 1) {
  219. uni.navigateBack()
  220. } else {
  221. /* if (this.from == 'kechengList') {
  222. uni.redirectTo({
  223. url: '/pages/client/Kecheng/list'
  224. })
  225. } else if (this.from == 'shouye') {
  226. uni.redirectTo({
  227. url: '/pages/client/ShouYe/shouye'
  228. })
  229. } else {
  230. uni.redirectTo({
  231. url: '/pages/client/ShouYe/shouye'
  232. })
  233. } */
  234. history.back();
  235. }
  236. },
  237. onClickItem(e) {
  238. if (this.current !== e.currentIndex) {
  239. this.current = e.currentIndex
  240. }
  241. // if (this.current == 0) {
  242. // if (!this.showVideo) {
  243. // this.shaDowImgFlag = false
  244. // } else {
  245. // this.shaDowImgFlag = true
  246. // }
  247. // } else {
  248. // this.shaDowImgFlag = true
  249. // }
  250. this.shaDowImgFlag = this.current !== 0 || this.showVideo
  251. },
  252. formatData(data) {
  253. data.forEach(zhang => {
  254. zhang.open = false;
  255. zhang.jieList.forEach(jie => {
  256. jie.open = false;
  257. })
  258. })
  259. return data;
  260. },
  261. handlePlay(data) {
  262. this.showVideo = true;
  263. this.shaDowImgFlag = true;
  264. if (this.curPlayData && this.curPlayData.url == data.url) {
  265. return;
  266. }
  267. console.log('data', data);
  268. this.curPlayData = data;
  269. //this.videoUrl = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/2minute-demo.mp4'
  270. kechengApi.getVideoId({
  271. videoId: data.url
  272. }).then(res => {
  273. this.videoUrl = res.data
  274. console.log('res', res);
  275. // const progress = data.curProcess || 0;
  276. // this.$refs.videoRef.init({
  277. // videoId: data.url,
  278. // playAuth: res.data,
  279. // seekTime: data.duration * progress / 100,
  280. // isPlay: false
  281. // })
  282. })
  283. },
  284. initFirstVideo() {
  285. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  286. // 设置默认展开项
  287. this.list[0].open = true;
  288. this.list[0].jieList[0].open = true;
  289. // 设置播放可见
  290. const kejian = this.list[0].jieList[0].kejianList[0];
  291. // this.handlePlay(kejian)
  292. }
  293. //saveKechengSectionPage(this.operId, sectionPage)
  294. },
  295. init() {
  296. kechengApi.getClientKechengStart({
  297. kcId: this.kcId,
  298. jzId: this.jzId
  299. }).then(res => {
  300. const {
  301. userCount,
  302. period,
  303. name,
  304. kejianUserVo,
  305. intro,
  306. operId,
  307. } = res.data;
  308. this.userCount = userCount;
  309. this.period = formatSecondsToCnhms(period, true);
  310. this.name = name;
  311. this.formatData(kejianUserVo.zhangList)
  312. this.list = kejianUserVo.zhangList;
  313. this.intro = intro;
  314. this.operId = operId;
  315. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  316. //let historyArrKecheng = getKechengDataFromHistory(this.operId)
  317. // let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  318. // // 判断是否有前台缓存
  319. // if (historyArrKecheng && sectionPageHistory) {
  320. // // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  321. // const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  322. // historyArrKecheng);
  323. // // 后台数据 同步前台缓存
  324. // saveKechengData(this.operId, arrKecheng)
  325. // } else {
  326. // // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  327. // saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  328. // }
  329. // 初始化页面 常规数据
  330. //initCourseProgressAll(this.operId)
  331. console.log('初始化播放首1123次')
  332. // 设置播放视频
  333. this.initFirstVideo();
  334. }).catch(err => {
  335. this.goUpPage();
  336. })
  337. }
  338. }
  339. }
  340. </script>
  341. <style>
  342. </style>