index.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. import {
  2. Button,
  3. closeToast,
  4. Icon,
  5. Popup,
  6. showToast,
  7. Slider,
  8. Swipe,
  9. SwipeItem,
  10. Tab,
  11. Tabs
  12. } from 'vant'
  13. import {
  14. defineComponent,
  15. onMounted,
  16. reactive,
  17. nextTick,
  18. onUnmounted,
  19. ref,
  20. watch,
  21. Transition,
  22. computed
  23. } from 'vue'
  24. import iconBack from './image/back.svg'
  25. import styles from './index.module.less'
  26. import 'plyr/dist/plyr.css'
  27. import request from '@/helpers/request'
  28. import { state } from '@/state'
  29. import { useRoute } from 'vue-router'
  30. import { listenerMessage, postMessage, promisefiyPostMessage } from '@/helpers/native-message'
  31. import MusicScore from './component/musicScore'
  32. import iconMenu from './image/icon-menu.svg'
  33. import iconDian from './image/icon-dian.svg'
  34. import iconPoint from './image/icon-point.svg'
  35. import iconLoop from './image/icon-loop.svg'
  36. import iconLoopActive from './image/icon-loop-active.svg'
  37. import iconplay from './image/icon-play.svg'
  38. import iconpause from './image/icon-pause.svg'
  39. import iconUp from './image/icon-up.svg'
  40. import iconDown from './image/icon-down.svg'
  41. import iconVideobg from './image/icon-videobg.png'
  42. import Points from './component/points'
  43. import { browser, getSecondRPM } from '@/helpers/utils'
  44. import { useRect } from '@vant/use'
  45. export default defineComponent({
  46. name: 'CoursewarePlay',
  47. setup() {
  48. const handleInit = (type = 0) => {
  49. // 横屏
  50. postMessage({
  51. api: 'setRequestedOrientation',
  52. content: {
  53. orientation: type
  54. }
  55. })
  56. // 头,包括返回箭头
  57. postMessage({
  58. api: 'setTitleBarVisibility',
  59. content: {
  60. status: type
  61. }
  62. })
  63. // 安卓的状态栏
  64. postMessage({
  65. api: 'setStatusBarVisibility',
  66. content: {
  67. isVisibility: type
  68. }
  69. })
  70. }
  71. handleInit()
  72. onUnmounted(() => {
  73. handleInit(1)
  74. window.removeEventListener('message', iframeHandle)
  75. })
  76. const route = useRoute()
  77. const headeRef = ref()
  78. const data = reactive({
  79. detail: null,
  80. knowledgePointList: [] as any,
  81. itemList: [] as any,
  82. showHead: true,
  83. isCourse: false
  84. })
  85. const activeData = reactive({
  86. nowTime: 0,
  87. model: true, // 遮罩
  88. videoBtns: true, // 视频
  89. currentTime: 0,
  90. duration: 0,
  91. timer: null as any,
  92. item: null as any
  93. })
  94. // 获取缓存路径
  95. const getCacheFilePath = async (material: any) => {
  96. const res = await promisefiyPostMessage({
  97. api: 'getCourseFilePath',
  98. content: {
  99. url: material.content,
  100. localPath: '',
  101. materialId: material.id,
  102. updateTime: material.updateTime,
  103. type: material.type // SONG VIDEO IMAGE
  104. }
  105. })
  106. console.log('缓存路径返回', res)
  107. return res
  108. }
  109. // 获取当前课程是否签退
  110. const getCourseSchedule = async () => {
  111. if (!route.query.courseId) return
  112. try {
  113. const res = await request.get(
  114. `${state.platformApi}/courseSchedule/detail/${route.query.courseId}`
  115. )
  116. if (res?.data) {
  117. data.isCourse = res.data.status === 'COMPLETE' ? false : true
  118. }
  119. } catch (e) {
  120. console.log(e)
  121. }
  122. }
  123. const getItemList = async () => {
  124. const list: any = []
  125. const browserInfo = browser()
  126. for (let i = 0; i < data.knowledgePointList.length; i++) {
  127. const item = data.knowledgePointList[i]
  128. const itemLength = item.materialList.length - 1
  129. for (let j = 0; j < item.materialList.length; j++) {
  130. const material = item.materialList[j]
  131. //请求本地缓存
  132. if (browserInfo.isApp && ['VIDEO', 'IMG'].includes(material.type)) {
  133. const localData = await getCacheFilePath(material)
  134. if (localData?.content?.localPath) {
  135. material.url = material.content
  136. material.content = localData.content.localPath
  137. // console.log("🚀 ~ material", material)
  138. }
  139. }
  140. let videoItem = {}
  141. if (material.type === 'VIDEO') {
  142. videoItem = {
  143. currentTime: 0,
  144. duration: 0,
  145. progress: 0,
  146. paused: true,
  147. loop: false,
  148. videoEle: null,
  149. timer: null,
  150. playModel: false,
  151. isprepare: false,
  152. isDrage: false,
  153. muted: false // 是否静音
  154. }
  155. }
  156. list.push({
  157. ...material,
  158. ...videoItem,
  159. iframeRef: null,
  160. tabName: item.name,
  161. isLast: j === itemLength, // 当前知识点
  162. autoPlay: false //加载完成是否自动播放
  163. })
  164. }
  165. }
  166. let item: any = null
  167. if (route.query.kId) {
  168. item = list.find((n: any) => n.materialId == route.query.kId)
  169. const _firstIndex = list.findIndex((n: any) => n.materialId == route.query.kId)
  170. popupData.firstIndex = _firstIndex > -1 ? _firstIndex : 0
  171. }
  172. item = item ? item : list[0] || {}
  173. if (item) {
  174. popupData.tabName = item.tabName
  175. popupData.tabActive = item.knowledgePointId
  176. popupData.itemActive = item.id
  177. popupData.itemName = item.name
  178. popupData.activeIndex = popupData.firstIndex
  179. item.autoPlay = true
  180. item.muted = true
  181. }
  182. console.log('🚀 ~ list', list)
  183. data.itemList = list
  184. }
  185. const getDetail = async () => {
  186. try {
  187. const res: any = await request.get(
  188. state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}`
  189. )
  190. if (Array.isArray(res?.data)) {
  191. data.detail = res.data
  192. }
  193. if (Array.isArray(res?.data?.knowledgePointList)) {
  194. let index = 0
  195. data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
  196. if (Array.isArray(n.materialList)) {
  197. n.materialList = n.materialList.map((item: any) => {
  198. index++
  199. return {
  200. ...item,
  201. materialId: item.id,
  202. id: index + ''
  203. }
  204. })
  205. }
  206. return n
  207. })
  208. getItemList()
  209. }
  210. } catch (error) {}
  211. }
  212. // ifram事件处理
  213. const iframeHandle = (ev: MessageEvent) => {
  214. // console.log(ev.data)
  215. if (ev.data?.api === 'headerTogge') {
  216. // activeData.model = ev.data.show
  217. activeData.model = !activeData.model
  218. }
  219. }
  220. onMounted(() => {
  221. getDetail()
  222. getCourseSchedule()
  223. window.addEventListener('message', iframeHandle)
  224. })
  225. // 返回
  226. const goback = () => {
  227. postMessage({ api: 'goBack' })
  228. }
  229. const swipeRef = ref()
  230. const popupData = reactive({
  231. firstIndex: 0,
  232. open: false,
  233. activeIndex: -1,
  234. tabActive: '',
  235. tabName: '',
  236. itemActive: '',
  237. itemName: ''
  238. })
  239. // 设置当前的激活状态
  240. const setActiveData = (val: any, oldVal: any) => {
  241. handleStopVideo()
  242. handleStopMusicScore()
  243. }
  244. watch(() => popupData.activeIndex, setActiveData)
  245. // 停止所有的播放
  246. const handleStopVideo = () => {
  247. data.itemList.forEach((m: any) => {
  248. m.videoEle?.pause()
  249. })
  250. }
  251. // 停止曲谱的播放
  252. const handleStopMusicScore = () => {
  253. data.itemList.forEach((m: any) => {
  254. m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*')
  255. })
  256. }
  257. // 切换素材
  258. const toggleMaterial = () => {
  259. const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive)
  260. if (index > -1) {
  261. popupData.activeIndex = index
  262. swipeRef.value?.swipeTo(index)
  263. }
  264. }
  265. /** 延迟收起模态框 */
  266. const setModelOpen = () => {
  267. clearTimeout(activeData.timer)
  268. closeToast()
  269. activeData.timer = setTimeout(() => {
  270. activeData.model = false
  271. }, 3000)
  272. }
  273. // 轮播切换
  274. const handleSwipeChange = (val: any) => {
  275. console.log('轮播切换')
  276. popupData.activeIndex = val
  277. const item = data.itemList[val]
  278. if (item) {
  279. popupData.tabActive = item.knowledgePointId
  280. popupData.itemActive = item.id
  281. popupData.itemName = item.name
  282. popupData.tabName = item.tabName
  283. if (item.type == 'SONG') {
  284. activeData.model = true
  285. }
  286. if (item.type === 'VIDEO') {
  287. // console.log("🚀 ~ item", item)
  288. // 自动播放下一个视频
  289. clearTimeout(activeData.timer)
  290. closeToast()
  291. item.currentTime = 0
  292. item.autoPlay = true
  293. item.videoEle && (item.videoEle.currentTime = 0)
  294. nextTick(() => {
  295. item.videoEle?.play()
  296. })
  297. }
  298. }
  299. }
  300. // 上一个知识点, 下一个知识点
  301. const handlePreAndNext = (type: string) => {
  302. if (type === 'up') {
  303. swipeRef.value?.prev()
  304. } else {
  305. swipeRef.value?.next()
  306. }
  307. }
  308. // 去点名,签退
  309. const gotoRollCall = (pageTag: string) => {
  310. postMessage({
  311. api: 'open_app_page',
  312. content: {
  313. action: 'app',
  314. pageTag: pageTag,
  315. url: '',
  316. params: JSON.stringify({ courseId: route.query.courseId })
  317. }
  318. })
  319. }
  320. // 双击
  321. const handleDbClick = (item: any) => {
  322. // console.log(item)
  323. if (item && item.type === 'VIDEO') {
  324. const videoEle: HTMLVideoElement = item.videoEle
  325. if (videoEle) {
  326. if (videoEle.paused) {
  327. closeToast()
  328. videoEle.play()
  329. } else {
  330. showToast('已暂停')
  331. videoEle.pause()
  332. }
  333. }
  334. }
  335. }
  336. // 暂停播放
  337. const handlePaused = (e: Event, m: any) => {
  338. e.stopPropagation()
  339. m.videoEle?.pause()
  340. m.paused = true
  341. }
  342. // 开始播放
  343. const handlePlay = (e: Event, m: any) => {
  344. e.stopPropagation()
  345. closeToast()
  346. m.videoEle?.play()
  347. }
  348. // 调整播放进度
  349. const handleChangeSlider = (m: any) => {
  350. if (m?.videoEle) {
  351. // console.log('进度条', m.progress)
  352. m.currentTime = m.duration * (m.progress / 100)
  353. m.videoEle.currentTime = m.currentTime
  354. }
  355. }
  356. //当前视频播放完
  357. const handleEnded = (m: any) => {
  358. // console.log(m)
  359. if (popupData.activeIndex != data.itemList.length - 1) {
  360. // popupData.activeIndex++
  361. swipeRef.value?.next()
  362. // const nextItem = data.itemList[popupData.activeIndex]
  363. // console.log('🚀 ~ nextItem', nextItem)
  364. }
  365. }
  366. //加载第一帧
  367. const handleFirstFrame = (video: HTMLVideoElement) => {
  368. // console.log("🚀 ~ 加载第一帧", video.videoWidth, video.videoHeight)
  369. const captureImage = function () {
  370. var canvas = document.createElement('canvas')
  371. canvas.width = video.videoWidth
  372. canvas.height = video.videoHeight
  373. canvas?.getContext('2d')?.drawImage(video, 0, 0, canvas.width, canvas.height)
  374. canvas.toBlob((blob) => {
  375. // console.log("🚀 ~ blob", blob)
  376. const imgUrl = URL.createObjectURL(blob as any)
  377. // console.log("🚀 ~ imgUrl", imgUrl)
  378. video.setAttribute('poster', imgUrl)
  379. })
  380. }
  381. captureImage()
  382. }
  383. return () => (
  384. <div class={styles.coursewarePlay}>
  385. <Swipe
  386. style={{ height: '100vh' }}
  387. ref={swipeRef}
  388. showIndicators={false}
  389. loop={false}
  390. vertical
  391. lazyRender={true}
  392. touchable={false}
  393. initialSwipe={popupData.firstIndex}
  394. onChange={handleSwipeChange}
  395. >
  396. {data.itemList.map((m: any, mIndex: number) => {
  397. return (
  398. <SwipeItem class={styles.swipeItem}>
  399. <>
  400. <div
  401. class={styles.itemDiv}
  402. onClick={() => {
  403. clearTimeout(activeData.timer)
  404. if (Date.now() - activeData.nowTime < 300) {
  405. handleDbClick(m)
  406. return
  407. }
  408. activeData.nowTime = Date.now()
  409. activeData.timer = setTimeout(() => {
  410. activeData.model = !activeData.model
  411. setModelOpen()
  412. }, 300)
  413. }}
  414. >
  415. {m.type === 'VIDEO' ? (
  416. <>
  417. <video
  418. playsinline="false"
  419. muted={m.muted}
  420. preload="auto"
  421. class="player"
  422. poster={iconVideobg}
  423. data-vid={m.id}
  424. src={m.content}
  425. loop={m.loop}
  426. autoplay={m.autoPlay}
  427. onLoadedmetadata={(e: Event) => {
  428. const videoEle = e.target as unknown as HTMLVideoElement
  429. m.currentTime = videoEle.currentTime
  430. m.duration = videoEle.duration
  431. m.videoEle = videoEle
  432. m.isprepare = true
  433. console.log('视频准备完成')
  434. }}
  435. onTimeupdate={(e: Event) => {
  436. if (!m.isprepare) return
  437. const videoEle = e.target as unknown as HTMLVideoElement
  438. m.currentTime = videoEle.currentTime
  439. m.progress = Number(
  440. ((videoEle.currentTime / m.duration) * 100).toFixed(1)
  441. )
  442. // console.log('播放',videoEle.currentTime, m.progress)
  443. }}
  444. onPlay={() => {
  445. // 播放
  446. m.paused = false
  447. console.log('播放')
  448. setModelOpen()
  449. m.muted = false
  450. }}
  451. onPause={() => {
  452. //暂停
  453. clearTimeout(activeData.timer)
  454. m.paused = true
  455. }}
  456. onEnded={() => handleEnded(m)}
  457. >
  458. <source src={m.content} type="video/mp4" />
  459. </video>
  460. <Transition name="bottom">
  461. {activeData.model && (
  462. <div class={[styles.bottomFixedContainer]}>
  463. <div class={styles.time}>
  464. <span>{getSecondRPM(m.currentTime)}</span>
  465. <span>{getSecondRPM(m.duration)}</span>
  466. </div>
  467. <div class={styles.slider}>
  468. <Slider
  469. style={{ display: m.isprepare ? 'block' : 'none' }}
  470. buttonSize={16}
  471. step={0.1}
  472. modelValue={m.progress}
  473. onUpdate:modelValue={(val: any) => {
  474. m.progress = val
  475. handleChangeSlider(m)
  476. }}
  477. onDragStart={(e: Event) => {
  478. // 开始拖动,暂停播放
  479. console.log('开始拖动')
  480. // 如果拖动之前,视频是播放状态,拖动完毕后继续播放
  481. if (!m.paused) {
  482. m.isDrage = true
  483. }
  484. handlePaused(e, m)
  485. }}
  486. onDragEnd={(e: Event) => {
  487. console.log('结束拖动')
  488. if (m.isDrage) {
  489. m.isDrage = false
  490. handlePlay(e, m)
  491. }
  492. }}
  493. min={0}
  494. max={100}
  495. />
  496. </div>
  497. <div class={styles.actions}>
  498. <div class={styles.actionBtn}>
  499. {m.paused ? (
  500. <img src={iconplay} onClick={(e: Event) => handlePlay(e, m)} />
  501. ) : (
  502. <img
  503. src={iconpause}
  504. onClick={(e: Event) => handlePaused(e, m)}
  505. />
  506. )}
  507. {m.loop ? (
  508. <img
  509. src={iconLoopActive}
  510. onClick={(e: Event) => {
  511. e.stopPropagation()
  512. m.loop = false
  513. }}
  514. />
  515. ) : (
  516. <img
  517. src={iconLoop}
  518. onClick={(e: Event) => {
  519. e.stopPropagation()
  520. m.loop = true
  521. }}
  522. />
  523. )}
  524. </div>
  525. <div>{m.name}</div>
  526. </div>
  527. </div>
  528. )}
  529. </Transition>
  530. </>
  531. ) : m.type === 'IMG' ? (
  532. <img src={m.content} />
  533. ) : (
  534. <MusicScore
  535. data-vid={m.id}
  536. music={m}
  537. onSetIframe={(el: any) => {
  538. m.iframeRef = el
  539. }}
  540. />
  541. )}
  542. </div>
  543. </>
  544. </SwipeItem>
  545. )
  546. })}
  547. </Swipe>
  548. <Transition name="top">
  549. {activeData.model && (
  550. <div class={styles.headerContainer} ref={headeRef}>
  551. <div class={styles.backBtn} onClick={() => goback()}>
  552. <Icon name={iconBack} />
  553. 返回
  554. </div>
  555. <div class={styles.menu}>{popupData.tabName}</div>
  556. </div>
  557. )}
  558. </Transition>
  559. <Transition name="right">
  560. {activeData.model && (
  561. <div class={styles.rightFixedBtns}>
  562. <div
  563. class={styles.fullBtn}
  564. onClick={() => {
  565. clearTimeout(activeData.timer)
  566. popupData.open = true
  567. }}
  568. >
  569. <img src={iconMenu} />
  570. <span>知识点</span>
  571. </div>
  572. {data.isCourse && (
  573. <>
  574. <div
  575. class={[styles.fullBtn, styles.point]}
  576. onClick={() => gotoRollCall('student_roll_call')}
  577. >
  578. <img src={iconDian} />
  579. <span>点名</span>
  580. </div>
  581. <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
  582. <img src={iconPoint} />
  583. <span>签退</span>
  584. </div>
  585. </>
  586. )}
  587. </div>
  588. )}
  589. </Transition>
  590. <Transition name="left">
  591. {activeData.model && (
  592. <div class={styles.leftFixedBtns}>
  593. {popupData.activeIndex != 0 && (
  594. <div
  595. class={[styles.fullBtn, styles.prePoint]}
  596. onClick={() => handlePreAndNext('up')}
  597. >
  598. <img src={iconUp} />
  599. <span style={{ textAlign: 'center' }}>上一个</span>
  600. </div>
  601. )}
  602. {popupData.activeIndex != data.itemList.length - 1 && (
  603. <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
  604. <span style={{ textAlign: 'center' }}>下一个</span>
  605. <img src={iconDown} />
  606. </div>
  607. )}
  608. </div>
  609. )}
  610. </Transition>
  611. <Popup
  612. class={styles.popup}
  613. overlayClass={styles.overlayClass}
  614. position="right"
  615. round
  616. v-model:show={popupData.open}
  617. onClose={() => {
  618. const item = data.itemList[popupData.activeIndex]
  619. if (item?.type == "VIDEO"){
  620. setModelOpen()
  621. }
  622. }}
  623. >
  624. <Points
  625. data={data.knowledgePointList}
  626. tabActive={popupData.tabActive}
  627. itemActive={popupData.itemActive}
  628. onHandleSelect={(res: any) => {
  629. // console.log(res)
  630. popupData.tabActive = res.tabActive
  631. popupData.itemActive = res.itemActive
  632. popupData.tabName = res.tabName
  633. popupData.open = false
  634. toggleMaterial()
  635. }}
  636. />
  637. </Popup>
  638. </div>
  639. )
  640. }
  641. })