study.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <view class="kecheng-study-page">
  3. <customNavbarVue title="课程" :show-back-btn="true" @back="goUpPage"></customNavbarVue>
  4. <!-- 播放器 -->
  5. <!-- <videoPlayVue v-if="showVideo" ref="videoRef" class="phone-video-box" @play-end="onPlayEnd" @play-play="onPlay"
  6. @play-pause="onPause" @play-timeupdate="onTimeupdate"></videoPlayVue> -->
  7. <view class="video-wrapper">
  8. <video v-if="showVideo" :src="videoUrl" :poster="videoPoster" controls class="phone-video-box"
  9. @play="onVideoPlay" @pause="onVideoPause" @ended="onVideoEnd" @error="onVideoError" id="myVideo"
  10. object-fit="contain" custom-cache @fullscreenchange="onVideoFullscreenChange"></video>
  11. <view v-else class="phone-video-box master-image" :style="{ backgroundImage: 'url(' + imgsArr.videoBj + ')' }">
  12. <!-- <view class="video-player-icon" @click="handlePlayFirst"></view> -->
  13. </view>
  14. <zmm-watermark :watermark="watermark" class="watermark-component" ></zmm-watermark>
  15. <view class="static-watermark">保密内容 · 请勿外传</view>
  16. <!-- 视频背景图 -->
  17. </view>
  18. <!-- 播放引导 -->
  19. <view class="yindao-shadow-image" v-if="!shaDowImgFlag"
  20. :style="{ backgroundImage: 'url(' + imgsArr.pointIcon + ')' }"> </view>
  21. <!-- 中间区域 -->
  22. <view class="kc-name-box">
  23. <view>{{name}}</view>
  24. </view>
  25. <view class="kc-info-box">
  26. <view>时长:{{period}}</view>
  27. <view>{{userCount}}人学习</view>
  28. </view>
  29. <!-- 大纲 -->
  30. <view class="phone-tab-box">
  31. <uni-segmented-control :current="current" :values="items" style-type="text" :active-color="activeColor"
  32. @clickItem="onClickItem" />
  33. </view>
  34. <view class="kecheng-content-box">
  35. <!-- 目录 -->
  36. <kechengMuluVue v-if="current === 0" :chapterArr="list" @play="handlePlay" :isHasProgress="true"
  37. :activeKjId="curPlayData&&curPlayData.kjId"></kechengMuluVue>
  38. <!-- 介绍 -->
  39. <rich-text :nodes="intro || '暂无内容'" v-if="current === 1 && intro" class="kecheng-jieshao-box"></rich-text>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import cacheManager from '@/utils/cacheManager.js';
  45. import * as kechengApi from "@/api/kecheng.js";
  46. import customNavbarVue from "@/components/custom-navbar/custom-navbar.vue";
  47. import videoPlayVue from "@/components/videoPlay/videoPlay.vue";
  48. import kechengMuluVue from "@/components/kecheng-mulu/kecheng-mulu.vue";
  49. import {
  50. useUserCache
  51. } from "@/utils/userCache.js"
  52. import {
  53. formatSecondsToCnhms
  54. } from "@/utils/common.js"
  55. import {
  56. useKechengTools
  57. } from "./useKechengCache.js"
  58. const {
  59. getCurKjIndex,
  60. saveKechengData,
  61. getKechengDataFromHistory,
  62. saveKechengSectionPage,
  63. getKechengSectionPageFromHistory,
  64. mergeProgress,
  65. initCourseProgressAll,
  66. saveCourseProgress,
  67. updateSectionProgress,
  68. } = useKechengTools();
  69. export default {
  70. components: {
  71. videoPlayVue,
  72. kechengMuluVue,
  73. customNavbarVue
  74. },
  75. data() {
  76. return {
  77. showVideo: false,
  78. shaDowImgFlag: false,
  79. items: ['目录', '介绍'],
  80. colors: ['#007aff', '#4cd964', '#dd524d'],
  81. activeColor: '#3fd2a1',
  82. current: 0, // 激活的选项卡
  83. operId: '', // 课程
  84. name: '',
  85. period: 0, // 时长
  86. userCount: 0, // 学习人数
  87. list: [],
  88. intro: '',
  89. curPlayData: null,
  90. timer1: null,
  91. kcId: null,
  92. from: '',
  93. jzId: null,
  94. watermark: '<h5>我是h5标签我是h5标签我是h5标签我是h5标签</h5><p style="color:#f00">我是p标签</p><br>/>',
  95. videoUrl: '', // 视频地址
  96. videoPoster: '', // 视频封面
  97. videoInfo: {}, // 视频信息
  98. loading: false, // 加载状态
  99. error: false, // 错误状态
  100. errorMessage: '', // 错误信息
  101. videoContext: null, // 视频上下文
  102. imgsArr: {}
  103. }
  104. },
  105. onLoad(options) {
  106. this.kcId = options.kcId;
  107. this.from = options.from;
  108. this.jzId = options.jzId;
  109. this.imgsArr.videoBj = cacheManager.get('projectImg').video_bj;
  110. this.imgsArr.pointIcon = cacheManager.get('projectImg').course_point_icon;
  111. this.init();
  112. },
  113. methods: {
  114. onVideoFullscreenChange(e) {
  115. console.log('全屏状态变化:', e.detail.fullScreen);
  116. // 改用uniapp专属选择器,获取视频容器(避免document兼容问题)
  117. const query = uni.createSelectorQuery().in(this);
  118. query.select('.video-wrapper')
  119. .fields({ node: true, dataset: true })
  120. .exec((res) => {
  121. if (res[0] && res[0].node) {
  122. const videoWrapper = res[0].node;
  123. if (e.detail.fullScreen) {
  124. // 全屏:添加wx-video-fullscreen类(匹配样式)
  125. videoWrapper.classList.add('wx-video-fullscreen');
  126. } else {
  127. // 退出全屏:移除类,恢复默认样式
  128. videoWrapper.classList.remove('wx-video-fullscreen');
  129. }
  130. }
  131. });
  132. },
  133. // 播放视频
  134. playVideo() {
  135. if (this.videoUrl) {
  136. this.videoContext.play()
  137. } else {
  138. uni.showToast({
  139. title: '暂无视频可播放',
  140. icon: 'none'
  141. })
  142. }
  143. },
  144. // 暂停视频
  145. pauseVideo() {
  146. this.videoContext.pause()
  147. },
  148. // 重新播放
  149. replayVideo() {
  150. this.videoContext.seek(0)
  151. this.videoContext.play()
  152. },
  153. // 视频开始播放
  154. onVideoPlay() {
  155. console.log('视频开始播放')
  156. },
  157. // 视频暂停
  158. onVideoPause() {
  159. console.log('视频暂停')
  160. },
  161. // 视频播放结束
  162. onVideoEnd() {
  163. console.log('视频播放结束')
  164. uni.showToast({
  165. title: '播放完成',
  166. icon: 'success'
  167. })
  168. },
  169. // 视频播放错误
  170. onVideoError(e) {
  171. console.error('视频播放错误:', e.detail.errMsg)
  172. this.error = true
  173. this.errorMessage = '视频播放出错,请检查视频地址或格式'
  174. },
  175. // 重新加载视频
  176. retryLoadVideo() {
  177. this.getVideoData()
  178. },
  179. handlePlayFirst() {
  180. console.log('this.list', this.list);
  181. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  182. // 设置默认展开项
  183. this.list[0].open = true;
  184. this.list[0].jieList[0].open = true;
  185. // 设置播放可见
  186. const kejian = this.list[0].jieList[0].kejianList[0];
  187. this.handlePlay(kejian)
  188. }
  189. },
  190. onPause() {
  191. clearInterval(this.timer1);
  192. this.timer1 = null;
  193. },
  194. onPlay() {
  195. clearInterval(this.timer1);
  196. this.timer1 = null;
  197. // this.timer = setInterval(() => {
  198. // updateSectionProgress(this.operId);
  199. // }, 1000 * 1 * 60) // 自动保存进度 1分钟已保存
  200. },
  201. sectionPlayerProgress(progress) {
  202. let sectionPage = getKechengSectionPageFromHistory(this.operId);
  203. let maxProcess = this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1
  204. .kjId == sectionPage.kjId).maxProcess;
  205. // 更新缓存进度
  206. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  207. sectionPage.kjId).curProcess = progress;
  208. if (progress < 100) {
  209. // 播放进度小于100
  210. if (progress < maxProcess) {
  211. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  212. sectionPage.kjId).maxProcess = maxProcess
  213. } else {
  214. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  215. sectionPage.kjId).maxProcess = progress
  216. }
  217. } else {
  218. // 播放进度大于100
  219. this.list[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList.find(it1 => it1.kjId ==
  220. sectionPage.kjId).maxProcess = 100
  221. }
  222. },
  223. onTimeupdate(time) {
  224. const progress = parseInt(time / this.curPlayData.duration * 100);
  225. this.curPlayData.curProgress = parseInt(progress >= 100 ? '99' : progress);
  226. // 保存进度
  227. // saveCourseProgress(time, this.curPlayData.duration, this.operId)
  228. // 更新进度
  229. // this.sectionPlayerProgress(progress)
  230. },
  231. onPlayEnd() {
  232. clearInterval(this.timer1);
  233. this.timer1 = null;
  234. // saveCourseProgress(this.curPlayData.duration, this.curPlayData.duration, this.operId, 'end');
  235. // updateSectionProgress(this.operId, 'end', 'video', () => {
  236. // this.curPlayData.maxProcess = 99;
  237. // this.curPlayData.curProcess = 99;
  238. // });
  239. console.log('end')
  240. },
  241. goUpPage() {
  242. const pages = getCurrentPages();
  243. if (pages.length > 1) {
  244. uni.navigateBack()
  245. } else {
  246. /* if (this.from == 'kechengList') {
  247. uni.redirectTo({
  248. url: '/pages/client/Kecheng/list'
  249. })
  250. } else if (this.from == 'shouye') {
  251. uni.redirectTo({
  252. url: '/pages/client/ShouYe/shouye'
  253. })
  254. } else {
  255. uni.redirectTo({
  256. url: '/pages/client/ShouYe/shouye'
  257. })
  258. } */
  259. history.back();
  260. }
  261. },
  262. onClickItem(e) {
  263. if (this.current !== e.currentIndex) {
  264. this.current = e.currentIndex
  265. }
  266. // if (this.current == 0) {
  267. // if (!this.showVideo) {
  268. // this.shaDowImgFlag = false
  269. // } else {
  270. // this.shaDowImgFlag = true
  271. // }
  272. // } else {
  273. // this.shaDowImgFlag = true
  274. // }
  275. this.shaDowImgFlag = this.current !== 0 || this.showVideo
  276. },
  277. formatData(data) {
  278. data.forEach(zhang => {
  279. zhang.open = false;
  280. zhang.jieList.forEach(jie => {
  281. jie.open = false;
  282. })
  283. })
  284. return data;
  285. },
  286. handlePlay(data) {
  287. this.showVideo = true;
  288. this.shaDowImgFlag = true;
  289. if (this.curPlayData && this.curPlayData.url == data.url) {
  290. return;
  291. }
  292. console.log('data', data);
  293. this.curPlayData = data;
  294. //this.videoUrl = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/2minute-demo.mp4'
  295. kechengApi.getVideoId({
  296. videoId: data.url
  297. }).then(res => {
  298. this.videoUrl = res.data
  299. console.log('res', res);
  300. // const progress = data.curProcess || 0;
  301. // this.$refs.videoRef.init({
  302. // videoId: data.url,
  303. // playAuth: res.data,
  304. // seekTime: data.duration * progress / 100,
  305. // isPlay: false
  306. // })
  307. })
  308. },
  309. initFirstVideo() {
  310. if (this.list && this.list[0].jieList && this.list[0].jieList[0].kejianList) {
  311. // 设置默认展开项
  312. this.list[0].open = true;
  313. this.list[0].jieList[0].open = true;
  314. // 设置播放可见
  315. const kejian = this.list[0].jieList[0].kejianList[0];
  316. // this.handlePlay(kejian)
  317. }
  318. //saveKechengSectionPage(this.operId, sectionPage)
  319. },
  320. init() {
  321. kechengApi.getClientKechengStart({
  322. kcId: this.kcId,
  323. jzId: this.jzId
  324. }).then(res => {
  325. const {
  326. userCount,
  327. period,
  328. name,
  329. kejianUserVo,
  330. intro,
  331. operId,
  332. } = res.data;
  333. this.userCount = userCount;
  334. this.period = formatSecondsToCnhms(period, true);
  335. this.name = name;
  336. this.formatData(kejianUserVo.zhangList)
  337. this.list = kejianUserVo.zhangList;
  338. this.intro = intro;
  339. this.operId = operId;
  340. // 获取课程缓存 && 课件缓存(课件缓存点击后产生)
  341. //let historyArrKecheng = getKechengDataFromHistory(this.operId)
  342. // let sectionPageHistory = getKechengSectionPageFromHistory(this.operId)
  343. // // 判断是否有前台缓存
  344. // if (historyArrKecheng && sectionPageHistory) {
  345. // // 有缓存---- 把start接口中,返回数据进度100%,更新到前台缓存
  346. // const arrKecheng = mergeProgress(kejianUserVo && kejianUserVo.zhangList,
  347. // historyArrKecheng);
  348. // // 后台数据 同步前台缓存
  349. // saveKechengData(this.operId, arrKecheng)
  350. // } else {
  351. // // 无缓存----把start接口中,返回的所有数据,更新到前台缓存
  352. // saveKechengData(this.operId, kejianUserVo && kejianUserVo.zhangList)
  353. // }
  354. // 初始化页面 常规数据
  355. //initCourseProgressAll(this.operId)
  356. console.log('初始化播放首1123次')
  357. // 设置播放视频
  358. this.initFirstVideo();
  359. }).catch(err => {
  360. this.goUpPage();
  361. })
  362. }
  363. }
  364. }
  365. </script>
  366. <style lang="scss" scoped>
  367. .video-wrapper {
  368. position: relative;
  369. width: 100%;
  370. height: 400rpx;
  371. background-color: #000;
  372. overflow: hidden; /* 防止水印溢出 */
  373. .phone-video-box {
  374. width: 100%;
  375. height: 100%;
  376. z-index: 1; /* 明确视频层级,低于水印 */
  377. }
  378. // 1. 确保水印组件样式完全生效(铺满、置顶、不遮挡操作)
  379. .watermark-component {
  380. position: absolute !important; /* 强制绝对定位,避免组件自身样式覆盖 */
  381. top: 0 !important;
  382. left: 0 !important;
  383. width: 100% !important;
  384. height: 100% !important;
  385. z-index: 999 !important; /* 高于视频z-index:1 */
  386. pointer-events: none !important;
  387. background: transparent !important; /* 避免组件背景遮挡视频 */
  388. }
  389. // 2. 优化静态水印:提高对比度,确保可见
  390. .static-watermark {
  391. position: absolute;
  392. top: 50%;
  393. left: 50%;
  394. transform: translate(-50%, -50%) rotate(-30deg);
  395. font-size: 32rpx;
  396. color: rgba(255, 255, 255, 0.3) !important; /* 提高不透明度,增强可见性 */
  397. font-weight: bold;
  398. white-space: nowrap;
  399. pointer-events: none;
  400. z-index: 999 !important;
  401. transition: all 0.3s ease;
  402. }
  403. }
  404. // 全屏样式优化:确保水印铺满屏幕
  405. .video-wrapper.wx-video-fullscreen {
  406. position: fixed !important;
  407. top: 0 !important;
  408. left: 0 !important;
  409. width: 100vw !important;
  410. height: 100vh !important;
  411. z-index: 9999 !important;
  412. background-color: #000 !important;
  413. .static-watermark {
  414. font-size: 80rpx !important;
  415. color: rgba(255, 255, 255, 0.4) !important; /* 全屏时进一步提高可见性 */
  416. }
  417. .watermark-component {
  418. width: 100vw !important;
  419. height: 100vh !important;
  420. }
  421. }
  422. </style>