study.vue 10 KB

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