index.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. muted: (i === 0 && j === 0) ? true : false,
  165. }
  166. }
  167. list.push({
  168. ...material,
  169. ...videoItem,
  170. iframeRef: null,
  171. tabName: item.name,
  172. isLast: j === itemLength, // 当前知识点
  173. autoPlay: j === itemLength
  174. })
  175. }
  176. }
  177. let item: any = null
  178. if (route.query.kId) {
  179. item = list.find((n: any) => n.id == route.query.kId)
  180. const _firstIndex = list.findIndex((n: any) => n.id == route.query.kId)
  181. popupData.firstIndex = _firstIndex > -1 ? _firstIndex : 0
  182. } else {
  183. item = list[0] || {}
  184. }
  185. if (item) {
  186. popupData.tabName = item.tabName
  187. popupData.tabActive = item.knowledgePointId
  188. popupData.itemActive = item.id
  189. popupData.itemName = item.name
  190. popupData.activeIndex = popupData.firstIndex
  191. }
  192. console.log('🚀 ~ list', list)
  193. data.itemList = list
  194. }
  195. const getDetail = async () => {
  196. try {
  197. const res: any = await request.get(
  198. state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}`
  199. )
  200. if (Array.isArray(res?.data)) {
  201. data.detail = res.data
  202. }
  203. if (Array.isArray(res?.data?.knowledgePointList)) {
  204. data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
  205. n.index = 0
  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 handleSwipeChange = (val: any) => {
  267. console.log('轮播切换')
  268. popupData.activeIndex = val
  269. const item = data.itemList[val]
  270. if (item) {
  271. popupData.tabActive = item.knowledgePointId
  272. popupData.itemActive = item.id
  273. popupData.itemName = item.name
  274. popupData.tabName = item.tabName
  275. if (item.type == 'SONG'){
  276. activeData.model = true
  277. }
  278. }
  279. }
  280. // 上一个知识点, 下一个知识点
  281. const handlePreAndNext = (type: string) => {
  282. if (type === 'up') {
  283. swipeRef.value?.prev()
  284. } else {
  285. swipeRef.value?.next()
  286. }
  287. }
  288. // 去点名,签退
  289. const gotoRollCall = (pageTag: string) => {
  290. postMessage({
  291. api: 'open_app_page',
  292. content: {
  293. action: 'app',
  294. pageTag: pageTag,
  295. url: '',
  296. params: JSON.stringify({ courseId: route.query.courseId })
  297. }
  298. })
  299. }
  300. // 双击
  301. const handleDbClick = (item: any) => {
  302. // console.log(item)
  303. if (item && item.type === 'VIDEO') {
  304. const videoEle: HTMLVideoElement = item.videoEle
  305. if (videoEle) {
  306. if (videoEle.paused) {
  307. closeToast()
  308. videoEle.play()
  309. } else {
  310. showToast('已暂停')
  311. videoEle.pause()
  312. }
  313. }
  314. }
  315. }
  316. // 暂停播放
  317. const handlePaused = (e: Event, m: any) => {
  318. e.stopPropagation()
  319. m.videoEle?.pause()
  320. m.paused = true
  321. }
  322. // 开始播放
  323. const handlePlay = (e: Event, m: any) => {
  324. e.stopPropagation()
  325. clearTimeout(m.timer)
  326. closeToast()
  327. m.videoEle?.play()
  328. }
  329. // 调整播放进度
  330. const handleChangeSlider = (m: any) => {
  331. if (m?.videoEle) {
  332. // console.log('进度条', m.progress)
  333. m.currentTime = m.duration * (m.progress / 100)
  334. m.videoEle.currentTime = m.currentTime
  335. }
  336. }
  337. //当前视频播放完
  338. const handleEnded = (m: any) => {
  339. // console.log(m)
  340. if (popupData.activeIndex != data.itemList.length - 1) {
  341. popupData.activeIndex++
  342. swipeRef.value?.next()
  343. const nextItem = data.itemList[popupData.activeIndex]
  344. if (nextItem.type === 'VIDEO') {
  345. nextTick(() => {
  346. // 自动播放下一个视频
  347. clearTimeout(m.timer)
  348. closeToast()
  349. nextItem.videoEle?.play()
  350. })
  351. }
  352. console.log('🚀 ~ nextItem', nextItem)
  353. }
  354. }
  355. //加载第一帧
  356. const handleFirstFrame = (video: HTMLVideoElement) => {
  357. // console.log("🚀 ~ 加载第一帧", video.videoWidth, video.videoHeight)
  358. const captureImage = function () {
  359. var canvas = document.createElement('canvas')
  360. canvas.width = video.videoWidth
  361. canvas.height = video.videoHeight
  362. canvas?.getContext('2d')?.drawImage(video, 0, 0, canvas.width, canvas.height)
  363. canvas.toBlob((blob) => {
  364. // console.log("🚀 ~ blob", blob)
  365. const imgUrl = URL.createObjectURL(blob as any)
  366. // console.log("🚀 ~ imgUrl", imgUrl)
  367. video.setAttribute('poster', imgUrl)
  368. })
  369. }
  370. captureImage()
  371. }
  372. return () => (
  373. <div class={styles.coursewarePlay}>
  374. <Swipe
  375. style={{ height: '100vh' }}
  376. ref={swipeRef}
  377. showIndicators={false}
  378. loop={false}
  379. vertical
  380. lazyRender={true}
  381. initialSwipe={popupData.firstIndex}
  382. onChange={handleSwipeChange}
  383. >
  384. {data.itemList.map((m: any, mIndex: number) => {
  385. return (
  386. <SwipeItem class={styles.swipeItem}>
  387. <>
  388. <div
  389. class={styles.itemDiv}
  390. onClick={() => {
  391. clearTimeout(activeData.timer)
  392. clearTimeout(m.timer)
  393. if (Date.now() - activeData.nowTime < 300) {
  394. handleDbClick(m)
  395. return
  396. }
  397. activeData.nowTime = Date.now()
  398. activeData.timer = setTimeout(() => {
  399. activeData.model = !activeData.model
  400. }, 300)
  401. }}
  402. >
  403. {m.type === 'VIDEO' ? (
  404. <>
  405. <video
  406. playsinline="false"
  407. muted={m.muted}
  408. preload="auto"
  409. class="player"
  410. poster={iconVideobg}
  411. data-vid={m.id}
  412. src={m.content}
  413. loop={m.loop}
  414. autoplay={mIndex === 0 ? true : false}
  415. onLoadedmetadata={(e: Event) => {
  416. const videoEle = e.target as unknown as HTMLVideoElement
  417. m.currentTime = videoEle.currentTime
  418. m.duration = videoEle.duration
  419. m.videoEle = videoEle
  420. m.isprepare = true
  421. m.playModel = true
  422. console.log('视频准备完成')
  423. }}
  424. onTimeupdate={(e: Event) => {
  425. if (!m.isprepare) return
  426. const videoEle = e.target as unknown as HTMLVideoElement
  427. m.currentTime = videoEle.currentTime
  428. m.progress = Number(
  429. ((videoEle.currentTime / m.duration) * 100).toFixed(1)
  430. )
  431. // console.log('播放',videoEle.currentTime, m.progress)
  432. }}
  433. onPlay={() => {
  434. // 播放
  435. m.paused = false
  436. console.log('播放')
  437. m.timer = setTimeout(() => {
  438. activeData.model = false
  439. }, 3000)
  440. m.muted = false
  441. }}
  442. onPause={() => {
  443. //暂停
  444. clearTimeout(m.timer)
  445. m.paused = true
  446. }}
  447. onEnded={() => handleEnded(m)}
  448. >
  449. <source src={m.content} type="video/mp4" />
  450. </video>
  451. <Transition name="bottom">
  452. {activeData.model && (
  453. <div class={[styles.bottomFixedContainer]}>
  454. <div class={styles.time}>
  455. <span>{getSecondRPM(m.currentTime)}</span>
  456. <span>{getSecondRPM(m.duration)}</span>
  457. </div>
  458. <div class={styles.slider}>
  459. <Slider
  460. style={{ display: m.isprepare ? 'block' : 'none' }}
  461. buttonSize={16}
  462. step={0.1}
  463. modelValue={m.progress}
  464. onUpdate:modelValue={(val: any) => {
  465. m.progress = val
  466. handleChangeSlider(m)
  467. }}
  468. onDragStart={(e: Event) => {
  469. // 开始拖动,暂停播放
  470. console.log('开始拖动')
  471. // 如果拖动之前,视频是播放状态,拖动完毕后继续播放
  472. if (!m.paused) {
  473. m.isDrage = true
  474. }
  475. handlePaused(e, m)
  476. }}
  477. onDragEnd={(e: Event) => {
  478. console.log('结束拖动')
  479. if (m.isDrage) {
  480. m.isDrage = false
  481. handlePlay(e, m)
  482. }
  483. }}
  484. min={0}
  485. max={100}
  486. />
  487. </div>
  488. <div class={styles.actions}>
  489. <div class={styles.actionBtn}>
  490. {m.paused ? (
  491. <img src={iconplay} onClick={(e: Event) => handlePlay(e, m)} />
  492. ) : (
  493. <img
  494. src={iconpause}
  495. onClick={(e: Event) => handlePaused(e, m)}
  496. />
  497. )}
  498. {m.loop ? (
  499. <img
  500. src={iconLoopActive}
  501. onClick={(e: Event) => {
  502. e.stopPropagation()
  503. m.loop = false
  504. }}
  505. />
  506. ) : (
  507. <img
  508. src={iconLoop}
  509. onClick={(e: Event) => {
  510. e.stopPropagation()
  511. m.loop = true
  512. }}
  513. />
  514. )}
  515. </div>
  516. <div>{m.name}</div>
  517. </div>
  518. </div>
  519. )}
  520. </Transition>
  521. </>
  522. ) : m.type === 'IMG' ? (
  523. <img src={m.content} />
  524. ) : (
  525. <MusicScore
  526. data-vid={m.id}
  527. music={m}
  528. onSetIframe={(el: any) => {
  529. m.iframeRef = el
  530. }}
  531. />
  532. )}
  533. {/* <Transition name="van-fade">
  534. {activeData.model && <div class={styles.playModel}></div>}
  535. </Transition> */}
  536. </div>
  537. </>
  538. </SwipeItem>
  539. )
  540. })}
  541. </Swipe>
  542. <Transition name="top">
  543. {activeData.model && (
  544. <div class={styles.headerContainer} ref={headeRef}>
  545. <div class={styles.backBtn} onClick={() => goback()}>
  546. <Icon name={iconBack} />
  547. 返回
  548. </div>
  549. <div class={styles.menu}>{popupData.tabName}</div>
  550. </div>
  551. )}
  552. </Transition>
  553. <Transition name="right">
  554. {activeData.model && (
  555. <div class={styles.rightFixedBtns}>
  556. <div class={styles.fullBtn} onClick={() => (popupData.open = true)}>
  557. <img src={iconMenu} />
  558. <span>知识点</span>
  559. </div>
  560. {data.isCourse && (
  561. <>
  562. <div
  563. class={[styles.fullBtn, styles.point]}
  564. onClick={() => gotoRollCall('student_roll_call')}
  565. >
  566. <img src={iconDian} />
  567. <span>点名</span>
  568. </div>
  569. <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
  570. <img src={iconPoint} />
  571. <span>签退</span>
  572. </div>
  573. </>
  574. )}
  575. </div>
  576. )}
  577. </Transition>
  578. <Transition name="left">
  579. {activeData.model && (
  580. <div class={styles.leftFixedBtns}>
  581. {popupData.activeIndex != 0 && (
  582. <div
  583. class={[styles.fullBtn, styles.prePoint]}
  584. onClick={() => handlePreAndNext('up')}
  585. >
  586. <img src={iconUp} />
  587. <span style={{ textAlign: 'center' }}>上一个</span>
  588. </div>
  589. )}
  590. {popupData.activeIndex != data.itemList.length - 1 && (
  591. <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
  592. <span style={{ textAlign: 'center' }}>下一个</span>
  593. <img src={iconDown} />
  594. </div>
  595. )}
  596. </div>
  597. )}
  598. </Transition>
  599. <Popup
  600. class={styles.popup}
  601. overlayClass={styles.overlayClass}
  602. position="right"
  603. round
  604. v-model:show={popupData.open}
  605. >
  606. <Points
  607. data={data.knowledgePointList}
  608. tabActive={popupData.tabActive}
  609. itemActive={popupData.itemActive}
  610. onHandleSelect={(res: any) => {
  611. // console.log(res)
  612. popupData.tabActive = res.tabActive
  613. popupData.itemActive = res.itemActive
  614. popupData.tabName = res.tabName
  615. popupData.open = false
  616. toggleMaterial()
  617. }}
  618. />
  619. </Popup>
  620. </div>
  621. )
  622. }
  623. })