study.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. from: ''
  76. }
  77. },
  78. onLoad(options) {
  79. this.kcId = options.kcId;
  80. this.from = options.from;
  81. this.init();
  82. },
  83. methods: {
  84. onPause() {
  85. clearInterval(this.timer1);
  86. this.timer1 = null;
  87. },
  88. onPlay() {
  89. clearInterval(this.timer1);
  90. this.timer1 = null;
  91. this.timer = setInterval(() => {
  92. updateSectionProgress(this.operId);
  93. }, 1000 * 5 * 60)
  94. },
  95. sectionPlayerProgress(progress) {
  96. let sectionPage = getKechengSectionPageFromHistory(this.operId);
  97. console.log('sectionPage',sectionPage)
  98. let maxProcess = this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess;
  99. // 更新缓存进度
  100. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).curProcess = progress;
  101. if (progress < 100) {
  102. // 播放进度小于100
  103. if (progress < maxProcess) {
  104. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess = maxProcess
  105. } else {
  106. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess = progress
  107. }
  108. } else {
  109. // 播放进度大于100
  110. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1=>it1.kjId == sectionPage.kjId).maxProcess = 100
  111. }
  112. },
  113. onTimeupdate(time) {
  114. const progress = parseInt(time / this.curPlayData.duration * 100);
  115. this.curPlayData.curProgress = parseInt(progress >= 100 ? '99' : progress);
  116. // 保存进度
  117. saveCourseProgress(time, this.curPlayData.duration, this.operId)
  118. // 更新进度
  119. this.sectionPlayerProgress(progress)
  120. },
  121. onPlayEnd() {
  122. clearInterval(this.timer1);
  123. this.timer1 = null;
  124. saveCourseProgress(this.curPlayData.duration, this.curPlayData.duration, this.operId, 'end');
  125. updateSectionProgress(this.operId, 'end', 'video', () => {
  126. this.curPlayData.maxProcess = 99;
  127. this.curPlayData.curProcess = 99;
  128. });
  129. console.log('end')
  130. },
  131. goUpPage() {
  132. if (this.from == 'kechengList') {
  133. uni.redirectTo({
  134. url: '/pages/client/Kecheng/list'
  135. })
  136. } else if (this.from == 'shouye') {
  137. uni.redirectTo({
  138. url: '/pages/client/ShouYe/shouye'
  139. })
  140. } else {
  141. uni.redirectTo({
  142. url: '/pages/client/ShouYe/shouye'
  143. })
  144. }
  145. },
  146. onClickItem(e) {
  147. if (this.current !== e.currentIndex) {
  148. this.current = e.currentIndex
  149. }
  150. },
  151. formatData(data) {
  152. data.forEach(zhang => {
  153. zhang.open = false;
  154. zhang.jieList.forEach(jie => {
  155. jie.open = false;
  156. })
  157. })
  158. return data;
  159. },
  160. handlePlay(data) {
  161. if (this.curPlayData && this.curPlayData.url == data.url) {
  162. return;
  163. }
  164. this.curPlayData = data;
  165. const {
  166. zhangIndex,
  167. jieIndex
  168. } = getCurKjIndex(data.kjId,this.operId)
  169. const sectionPage = {};
  170. sectionPage.iChapter = zhangIndex;
  171. sectionPage.iSection = jieIndex;
  172. sectionPage.kjId = data.kjId;
  173. sectionPage.operId = this.operId;
  174. saveKechengSectionPage(this.operId, sectionPage)
  175. kechengApi.getVideoAuth({
  176. videoId: data.url
  177. }).then(res => {
  178. this.$refs.videoRef.init({
  179. videoId: data.url,
  180. playAuth: res.data,
  181. seekTime: '',
  182. isPlay: true
  183. })
  184. })
  185. },
  186. initFirstVideo() {
  187. let sectionPage = getKechengSectionPageFromHistory(this.operId)
  188. if (sectionPage) {
  189. const {
  190. zhangIndex,
  191. jieIndex
  192. } = getCurKjIndex(sectionPage.kjId,this.operId)
  193. sectionPage.iChapter = zhangIndex;
  194. sectionPage.iSection = jieIndex;
  195. sectionPage.kjId = sectionPage.kjId;
  196. sectionPage.operId = this.operId;
  197. // 设置默认展开项
  198. this.list[zhangIndex].open = true;
  199. this.list[zhangIndex].jieList[jieIndex].open = true;
  200. const kejian = this.list[0].jieList && this.list[zhangIndex].jieList[jieIndex].kejianList.find(item => item.kjId ==sectionPage.kjId )
  201. this.handlePlay(kejian)
  202. } else {
  203. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  204. // 设置默认展开项
  205. this.list[0].open = true;
  206. this.list[0].jieList[0].open = true;
  207. // 设置播放可见
  208. const kejian = this.list[0].jieList[0].kejianList[0];
  209. this.handlePlay(kejian)
  210. // 更新缓存
  211. sectionPage = {
  212. kjId: kejian.kjId,
  213. iChapter: 0,
  214. iSection: 0,
  215. operId: this.operId
  216. }
  217. }
  218. }
  219. saveKechengSectionPage(this.operId, sectionPage)
  220. },
  221. init() {
  222. kechengApi.getClientKechengStart({
  223. kcId: this.kcId
  224. }).then(res => {
  225. const {
  226. userCount,
  227. period,
  228. name,
  229. kejianUserVo,
  230. intro,
  231. operId
  232. } = res.data;
  233. this.userCount = userCount;
  234. this.period = formatDuration(period);
  235. this.name = name;
  236. this.formatData(kejianUserVo.zhangList)
  237. this.list = kejianUserVo.zhangList;
  238. this.intro = intro;
  239. this.operId = operId;
  240. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  241. let historyArrKecheng = getKechengDataFromHistory(this.operId)
  242. let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  243. // 判断是否有前台缓存
  244. if (historyArrKecheng && sectionPageHistory) {
  245. // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  246. const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  247. historyArrKecheng);
  248. // 后台数据 同步前台缓存
  249. saveKechengData(this.operId, arrKecheng)
  250. } else {
  251. // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  252. saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  253. }
  254. // 初始化页面 常规数据
  255. initCourseProgressAll(this.operId)
  256. console.log('初始化播放首次')
  257. // 设置播放视频
  258. this.initFirstVideo();
  259. })
  260. }
  261. }
  262. }
  263. </script>
  264. <style>
  265. </style>