study.vue 9.7 KB

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