study.vue 8.7 KB

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