index.tsx 21 KB

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