study.vue 10 KB

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