study.vue 10 KB

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