index.tsx 21 KB

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