study.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. const pages = getCurrentPages();
  133. if (pages.length>1) {
  134. uni.navigateBack()
  135. } else {
  136. /* if (this.from == 'kechengList') {
  137. uni.redirectTo({
  138. url: '/pages/client/Kecheng/list'
  139. })
  140. } else if (this.from == 'shouye') {
  141. uni.redirectTo({
  142. url: '/pages/client/ShouYe/shouye'
  143. })
  144. } else {
  145. uni.redirectTo({
  146. url: '/pages/client/ShouYe/shouye'
  147. })
  148. } */
  149. history.back();
  150. }
  151. },
  152. onClickItem(e) {
  153. if (this.current !== e.currentIndex) {
  154. this.current = e.currentIndex
  155. }
  156. },
  157. formatData(data) {
  158. data.forEach(zhang => {
  159. zhang.open = false;
  160. zhang.jieList.forEach(jie => {
  161. jie.open = false;
  162. })
  163. })
  164. return data;
  165. },
  166. handlePlay(data) {
  167. if (this.curPlayData && this.curPlayData.url == data.url) {
  168. return;
  169. }
  170. this.curPlayData = data;
  171. const {
  172. zhangIndex,
  173. jieIndex
  174. } = getCurKjIndex(data.kjId,this.operId)
  175. const sectionPage = {};
  176. sectionPage.iChapter = zhangIndex;
  177. sectionPage.iSection = jieIndex;
  178. sectionPage.kjId = data.kjId;
  179. sectionPage.operId = this.operId;
  180. saveKechengSectionPage(this.operId, sectionPage)
  181. kechengApi.getVideoAuth({
  182. videoId: data.url
  183. }).then(res => {
  184. this.$refs.videoRef.init({
  185. videoId: data.url,
  186. playAuth: res.data,
  187. seekTime: '',
  188. isPlay: true
  189. })
  190. })
  191. },
  192. initFirstVideo() {
  193. let sectionPage = getKechengSectionPageFromHistory(this.operId)
  194. if (sectionPage) {
  195. const {
  196. zhangIndex,
  197. jieIndex
  198. } = getCurKjIndex(sectionPage.kjId,this.operId)
  199. sectionPage.iChapter = zhangIndex;
  200. sectionPage.iSection = jieIndex;
  201. sectionPage.kjId = sectionPage.kjId;
  202. sectionPage.operId = this.operId;
  203. // 设置默认展开项
  204. this.list[zhangIndex].open = true;
  205. this.list[zhangIndex].jieList[jieIndex].open = true;
  206. const kejian = this.list[0].jieList && this.list[zhangIndex].jieList[jieIndex].kejianList.find(item => item.kjId ==sectionPage.kjId )
  207. this.handlePlay(kejian)
  208. } else {
  209. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  210. // 设置默认展开项
  211. this.list[0].open = true;
  212. this.list[0].jieList[0].open = true;
  213. // 设置播放可见
  214. const kejian = this.list[0].jieList[0].kejianList[0];
  215. this.handlePlay(kejian)
  216. // 更新缓存
  217. sectionPage = {
  218. kjId: kejian.kjId,
  219. iChapter: 0,
  220. iSection: 0,
  221. operId: this.operId
  222. }
  223. }
  224. }
  225. saveKechengSectionPage(this.operId, sectionPage)
  226. },
  227. init() {
  228. kechengApi.getClientKechengStart({
  229. kcId: this.kcId
  230. }).then(res => {
  231. const {
  232. userCount,
  233. period,
  234. name,
  235. kejianUserVo,
  236. intro,
  237. operId
  238. } = res.data;
  239. this.userCount = userCount;
  240. this.period = formatDuration(period);
  241. this.name = name;
  242. this.formatData(kejianUserVo.zhangList)
  243. this.list = kejianUserVo.zhangList;
  244. this.intro = intro;
  245. this.operId = operId;
  246. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  247. let historyArrKecheng = getKechengDataFromHistory(this.operId)
  248. let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  249. // 判断是否有前台缓存
  250. if (historyArrKecheng && sectionPageHistory) {
  251. // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  252. const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  253. historyArrKecheng);
  254. // 后台数据 同步前台缓存
  255. saveKechengData(this.operId, arrKecheng)
  256. } else {
  257. // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  258. saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  259. }
  260. // 初始化页面 常规数据
  261. initCourseProgressAll(this.operId)
  262. console.log('初始化播放首次')
  263. // 设置播放视频
  264. this.initFirstVideo();
  265. })
  266. }
  267. }
  268. }
  269. </script>
  270. <style>
  271. </style>