study.vue 8.8 KB

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