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