study.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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="bifaibg-icon" @click="handlePlayFirst"></view>
  13. </view>
  14. <!-- 中间区域 -->
  15. <view class="kc-name-box">
  16. <icon @click="goUpPage"></icon>
  17. <view>{{zyName}} {{zyLevelName}}</view>
  18. </view>
  19. <view class="kc-info-box">
  20. <view>时长:{{period}}</view>
  21. <view>{{userCount}}人学习</view>
  22. </view>
  23. <!-- 大纲 -->
  24. <view class="phone-tab-box">
  25. <uni-segmented-control :current="current" :values="items" style-type="text" :active-color="activeColor"
  26. @clickItem="onClickItem" />
  27. </view>
  28. <view class="kecheng-content-box">
  29. <!-- 目录 -->
  30. <kechengMuluVue v-if="current === 0" :chapterArr="list" @play="handlePlay" :isHasProgress="true"
  31. :activeKjId="curPlayData&&curPlayData.kjId"></kechengMuluVue>
  32. <!-- 介绍 -->
  33. <rich-text :nodes="intro || '暂无内容'" v-if="current === 1 && intro" class="kecheng-jieshao-box"></rich-text>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import * as kechengApi from "@/api/kecheng.js";
  39. import videoPlayVue from "@/components/videoPlay/videoPlay.vue";
  40. import kechengMuluVue from "@/components/kecheng-mulu/kecheng-mulu.vue";
  41. import {
  42. useUserCache
  43. } from "@/utils/userCache.js"
  44. import {
  45. formatSecondsToCnhms
  46. } from "@/utils/common.js"
  47. import {
  48. useKechengTools
  49. } from "./useKechengCache.js"
  50. const {
  51. getCurKjIndex,
  52. saveKechengData,
  53. getKechengDataFromHistory,
  54. saveKechengSectionPage,
  55. getKechengSectionPageFromHistory,
  56. mergeProgress,
  57. initCourseProgressAll,
  58. saveCourseProgress,
  59. updateSectionProgress,
  60. } = useKechengTools();
  61. export default {
  62. components: {
  63. videoPlayVue,
  64. kechengMuluVue
  65. },
  66. data() {
  67. return {
  68. showVideo: false,
  69. items: ['目录', '介绍'],
  70. colors: ['#007aff', '#4cd964', '#dd524d'],
  71. activeColor: '#3fd2a1',
  72. current: 0, // 激活的选项卡
  73. operId: '', // 课程
  74. name: '',
  75. period: 0, // 时长
  76. userCount: 0, // 学习人数
  77. list: [],
  78. intro: '',
  79. curPlayData: null,
  80. timer1: null,
  81. kcId: null,
  82. from: '',
  83. zyName: '',
  84. zyLevelName: '',
  85. userKaozhengId: null,
  86. }
  87. },
  88. onLoad(options) {
  89. this.kcId = options.kcId;
  90. this.from = options.from;
  91. this.userKaozhengId = options.userKaozhengId;
  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. userKaozhengId:this.userKaozhengId
  278. }).then(res => {
  279. const {
  280. userCount,
  281. period,
  282. name,
  283. kejianUserVo,
  284. intro,
  285. operId,
  286. zyName,
  287. zyLevelName,
  288. } = res.data;
  289. this.userCount = userCount;
  290. this.period = formatSecondsToCnhms(period,true);
  291. this.name = name;
  292. this.zyName = zyName;
  293. this.zyLevelName = zyLevelName;
  294. this.formatData(kejianUserVo.zhangList)
  295. this.list = kejianUserVo.zhangList;
  296. this.intro = intro;
  297. this.operId = operId;
  298. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  299. let historyArrKecheng = getKechengDataFromHistory(this.operId)
  300. let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  301. // 判断是否有前台缓存
  302. if (historyArrKecheng && sectionPageHistory) {
  303. // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  304. const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  305. historyArrKecheng);
  306. // 后台数据 同步前台缓存
  307. saveKechengData(this.operId, arrKecheng)
  308. } else {
  309. // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  310. saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  311. }
  312. // 初始化页面 常规数据
  313. initCourseProgressAll(this.operId)
  314. console.log('初始化播放首次')
  315. // 设置播放视频
  316. this.initFirstVideo();
  317. }).catch(err => {
  318. this.goUpPage();
  319. })
  320. }
  321. }
  322. }
  323. </script>
  324. <style>
  325. </style>