study.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 ref="videoRef" class="phone-video-box" @play-end="onPlayEnd" @play-play="onPlay"
  9. @play-pause="onPause" @play-timeupdate="onTimeupdate"></videoPlayVue>
  10. <!-- 中间区域 -->
  11. <view class="kc-info-box">
  12. <view>时长:{{period}}分钟</view>
  13. <view>{{userCount}}人学习</view>
  14. </view>
  15. <!-- 大纲 -->
  16. <view class="phone-tab-box">
  17. <uni-segmented-control :current="current" :values="items" style-type="text" :active-color="activeColor"
  18. @clickItem="onClickItem" />
  19. </view>
  20. <view class="kecheng-content-box">
  21. <!-- 目录 -->
  22. <kechengMuluVue v-if="current === 0" :chapterArr="list" @play="handlePlay" :isHasProgress="true"
  23. :activeKjId="curPlayData&&curPlayData.kjId"></kechengMuluVue>
  24. <!-- 介绍 -->
  25. <rich-text :nodes="intro || '暂无内容'" v-if="current === 1 && intro" class="kecheng-jieshao-box"></rich-text>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import * as kechengApi from "@/api/kecheng.js";
  31. import videoPlayVue from "@/components/videoPlay/videoPlay.vue";
  32. import kechengMuluVue from "@/components/kecheng-mulu/kecheng-mulu.vue";
  33. import {
  34. useUserCache
  35. } from "@/utils/userCache.js"
  36. import {
  37. formatDuration
  38. } from "@/utils/common.js"
  39. import {
  40. useKechengTools
  41. } from "./useKechengCache.js"
  42. const {
  43. getCurKjIndex,
  44. saveKechengData,
  45. getKechengDataFromHistory,
  46. saveKechengSectionPage,
  47. getKechengSectionPageFromHistory,
  48. mergeProgress,
  49. initCourseProgressAll,
  50. saveCourseProgress,
  51. getCourseProgress,
  52. updateSectionProgress,
  53. saveInterfaceAbnormal
  54. } = useKechengTools();
  55. export default {
  56. components: {
  57. videoPlayVue,
  58. kechengMuluVue
  59. },
  60. data() {
  61. return {
  62. items: ['目录', '介绍'],
  63. colors: ['#007aff', '#4cd964', '#dd524d'],
  64. activeColor: '#3fd2a1',
  65. current: 0, // 激活的选项卡
  66. operId: '', // 课程
  67. name: '',
  68. period: 0, // 时长
  69. userCount: 0, // 学习人数
  70. list: [],
  71. intro: '',
  72. curPlayData: null,
  73. timer1: null,
  74. kcId: null,
  75. }
  76. },
  77. onLoad(options) {
  78. this.kcId = options.kcId;
  79. this.init();
  80. },
  81. methods: {
  82. onPause() {
  83. clearInterval(this.timer1);
  84. this.timer1 = null;
  85. },
  86. onPlay() {
  87. clearInterval(this.timer1);
  88. this.timer1 = null;
  89. this.timer = setInterval(() => {
  90. updateSectionProgress(this.operId);
  91. }, 1000 * 5 * 60)
  92. },
  93. sectionPlayerProgress(progress) {
  94. let sectionPage = getKechengSectionPageFromHistory(this.operId);
  95. console.log('sectionPage',sectionPage)
  96. let maxProcess = this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess;
  97. // 更新缓存进度
  98. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).curProcess = progress;
  99. if (progress < 100) {
  100. // 播放进度小于100
  101. if (progress < maxProcess) {
  102. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess = maxProcess
  103. } else {
  104. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess = progress
  105. }
  106. } else {
  107. // 播放进度大于100
  108. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess = 100
  109. }
  110. },
  111. onTimeupdate(time) {
  112. const progress = parseInt(time / this.curPlayData.duration * 100);
  113. this.curPlayData.curProgress = parseInt(progress >= 100 ? '99' : progress);
  114. // 保存进度
  115. saveCourseProgress(time, this.curPlayData.duration, this.operId)
  116. // 更新进度
  117. this.sectionPlayerProgress(progress)
  118. },
  119. onPlayEnd() {
  120. clearInterval(this.timer1);
  121. this.timer1 = null;
  122. saveCourseProgress(this.curPlayData.duration, this.curPlayData.duration, this.operId, 'end');
  123. updateSectionProgress(this.operId, 'end', 'video', () => {
  124. this.curPlayData.maxProcess = 99;
  125. this.curPlayData.curProcess = 99;
  126. });
  127. console.log('end')
  128. },
  129. goUpPage() {
  130. uni.redirectTo({
  131. url: '/pages/admin/Kecheng/list'
  132. })
  133. },
  134. onClickItem(e) {
  135. if (this.current !== e.currentIndex) {
  136. this.current = e.currentIndex
  137. }
  138. },
  139. formatData(data) {
  140. data.forEach(zhang => {
  141. zhang.open = false;
  142. zhang.jieList.forEach(jie => {
  143. jie.open = false;
  144. })
  145. })
  146. return data;
  147. },
  148. handlePlay(data) {
  149. if (this.curPlayData && this.curPlayData.url == data.url) {
  150. return;
  151. }
  152. this.curPlayData = data;
  153. const {
  154. zhangIndex,
  155. jieIndex
  156. } = getCurKjIndex(data.kjId,this.operId)
  157. const sectionPage = {};
  158. sectionPage.iChapter = zhangIndex;
  159. sectionPage.iSection = jieIndex;
  160. sectionPage.kjId = data.kjId;
  161. sectionPage.operId = this.operId;
  162. saveKechengSectionPage(this.operId, sectionPage)
  163. kechengApi.getVideoAuth({
  164. videoId: data.url
  165. }).then(res => {
  166. this.$refs.videoRef.init({
  167. videoId: data.url,
  168. playAuth: res.data,
  169. seekTime: '',
  170. isPlay: true
  171. })
  172. })
  173. },
  174. initFirstVideo() {
  175. let sectionPage = getKechengSectionPageFromHistory(this.operId)
  176. if (sectionPage) {
  177. const {
  178. zhangIndex,
  179. jieIndex
  180. } = getCurKjIndex(sectionPage.kjId,this.operId)
  181. sectionPage.iChapter = zhangIndex;
  182. sectionPage.iSection = jieIndex;
  183. sectionPage.kjId = sectionPage.kjId;
  184. sectionPage.operId = this.operId;
  185. // 设置默认展开项
  186. this.list[zhangIndex].open = true;
  187. this.list[zhangIndex].jieList[jieIndex].open = true;
  188. const kejian = this.list[0].jieList && this.list[zhangIndex].jieList[jieIndex].kejianList.find(item => item.kjId ==sectionPage.kjId )
  189. this.handlePlay(kejian)
  190. } else {
  191. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  192. // 设置默认展开项
  193. this.list[0].open = true;
  194. this.list[0].jieList[0].open = true;
  195. // 设置播放可见
  196. const kejian = this.list[0].jieList[0].kejianList[0];
  197. this.handlePlay(kejian)
  198. // 更新缓存
  199. sectionPage = {
  200. kjId: kejian.kjId,
  201. iChapter: 0,
  202. iSection: 0,
  203. operId: this.operId
  204. }
  205. }
  206. }
  207. saveKechengSectionPage(this.operId, sectionPage)
  208. },
  209. init() {
  210. kechengApi.getClientKechengStart({
  211. kcId: this.kcId
  212. }).then(res => {
  213. const {
  214. userCount,
  215. period,
  216. name,
  217. kejianUserVo,
  218. intro,
  219. operId
  220. } = res.data;
  221. this.userCount = userCount;
  222. this.period = formatDuration(period);
  223. this.name = name;
  224. this.formatData(kejianUserVo.zhangList)
  225. this.list = kejianUserVo.zhangList;
  226. this.intro = intro;
  227. this.operId = operId;
  228. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  229. let historyArrKecheng = getKechengDataFromHistory(this.operId)
  230. let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  231. // 判断是否有前台缓存
  232. if (historyArrKecheng && sectionPageHistory) {
  233. // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  234. const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  235. historyArrKecheng);
  236. // 后台数据 同步前台缓存
  237. saveKechengData(this.operId, arrKecheng)
  238. } else {
  239. // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  240. saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  241. }
  242. // 初始化页面 常规数据
  243. initCourseProgressAll(this.operId)
  244. console.log('初始化播放首次')
  245. // 设置播放视频
  246. this.initFirstVideo();
  247. })
  248. }
  249. }
  250. }
  251. </script>
  252. <style>
  253. </style>