study.vue 8.5 KB

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