index.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. autoPlay: j === itemLength
  172. })
  173. }
  174. }
  175. let item: any = null
  176. if (route.query.kId) {
  177. item = list.find((n: any) => n.id == route.query.kId)
  178. const _firstIndex = list.findIndex((n: any) => n.id == route.query.kId)
  179. popupData.firstIndex = _firstIndex > -1 ? _firstIndex : 0
  180. } else {
  181. item = list[0] || {}
  182. }
  183. if (item) {
  184. popupData.tabName = item.tabName
  185. popupData.tabActive = item.knowledgePointId
  186. popupData.itemActive = item.id
  187. popupData.itemName = item.name
  188. popupData.activeIndex = popupData.firstIndex
  189. }
  190. console.log('🚀 ~ list', list)
  191. data.itemList = list
  192. }
  193. const getDetail = async () => {
  194. try {
  195. const res: any = await request.get(
  196. state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}`
  197. )
  198. if (Array.isArray(res?.data)) {
  199. data.detail = res.data
  200. }
  201. if (Array.isArray(res?.data?.knowledgePointList)) {
  202. data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
  203. n.index = 0
  204. return n
  205. })
  206. getItemList()
  207. }
  208. } catch (error) {}
  209. }
  210. // ifram事件处理
  211. const iframeHandle = (ev: MessageEvent) => {
  212. // console.log(ev.data)
  213. if (ev.data?.api === 'headerTogge') {
  214. activeData.model = ev.data.show
  215. }
  216. }
  217. onMounted(() => {
  218. getDetail()
  219. getCourseSchedule()
  220. window.addEventListener('message', iframeHandle)
  221. })
  222. // 返回
  223. const goback = () => {
  224. postMessage({ api: 'goBack' })
  225. }
  226. const swipeRef = ref()
  227. const popupData = reactive({
  228. firstIndex: 0,
  229. open: false,
  230. activeIndex: -1,
  231. tabActive: '',
  232. tabName: '',
  233. itemActive: '',
  234. itemName: ''
  235. })
  236. // 设置当前的激活状态
  237. const setActiveData = (val: any, oldVal: any) => {
  238. handleStopVideo()
  239. handleStopMusicScore()
  240. }
  241. watch(() => popupData.activeIndex, setActiveData)
  242. // 停止所有的播放
  243. const handleStopVideo = () => {
  244. data.itemList.forEach((m: any) => {
  245. m.videoEle?.pause()
  246. })
  247. }
  248. // 停止曲谱的播放
  249. const handleStopMusicScore = () => {
  250. data.itemList.forEach((m: any) => {
  251. m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*')
  252. })
  253. }
  254. // 切换素材
  255. const toggleMaterial = () => {
  256. const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive)
  257. if (index > -1) {
  258. popupData.activeIndex = index
  259. swipeRef.value?.swipeTo(index)
  260. }
  261. }
  262. // 轮播切换
  263. const handleSwipeChange = (val: any) => {
  264. popupData.activeIndex = val
  265. const item = data.itemList[val]
  266. if (item) {
  267. popupData.tabActive = item.knowledgePointId
  268. popupData.itemActive = item.id
  269. popupData.itemName = item.name
  270. popupData.tabName = item.tabName
  271. activeData.model = true
  272. if (item.type === 'VIDEO') {
  273. item.playModel = true
  274. }
  275. }
  276. }
  277. // 上一个知识点, 下一个知识点
  278. const handlePreAndNext = (type: string) => {
  279. if (type === 'up') {
  280. swipeRef.value?.prev()
  281. } else {
  282. swipeRef.value?.next()
  283. }
  284. }
  285. // 去点名,签退
  286. const gotoRollCall = (pageTag: string) => {
  287. postMessage({
  288. api: 'open_app_page',
  289. content: {
  290. action: 'app',
  291. pageTag: pageTag,
  292. url: '',
  293. params: JSON.stringify({ courseId: route.query.courseId })
  294. }
  295. })
  296. }
  297. // 双击
  298. const handleDbClick = (item: any) => {
  299. // console.log(item)
  300. if (item && item.type === 'VIDEO') {
  301. const videoEle: HTMLVideoElement = item.videoEle
  302. if (videoEle) {
  303. console.log(videoEle)
  304. if (videoEle.paused) {
  305. closeToast()
  306. videoEle.play()
  307. } else {
  308. showToast('已暂停')
  309. videoEle.pause()
  310. }
  311. }
  312. item.timer = setTimeout(() => {
  313. activeData.model = false
  314. }, 3000)
  315. }
  316. }
  317. // 暂停播放
  318. const handlePaused = (e: Event, m: any) => {
  319. e.stopPropagation()
  320. console.log('暂停')
  321. m.videoEle?.pause()
  322. m.paused = true
  323. }
  324. // 开始播放
  325. const handlePlay = (e: Event, m: any) => {
  326. e.stopPropagation()
  327. clearTimeout(m.timer)
  328. closeToast()
  329. m.videoEle?.play()
  330. m.paused = false
  331. m.timer = setTimeout(() => {
  332. activeData.model = false
  333. }, 3000)
  334. }
  335. // 调整播放进度
  336. const handleChangeSlider = (m: any) => {
  337. if (m?.videoEle) {
  338. // console.log('进度条', m.progress)
  339. m.currentTime = m.duration * (m.progress / 100)
  340. m.videoEle.currentTime = m.currentTime
  341. }
  342. }
  343. //当前视频播放完
  344. const handleEnded = (m: any) => {
  345. // console.log(m)
  346. // 自动播放下一个知识点
  347. if (m.autoPlay) {
  348. if (popupData.activeIndex != data.itemList.length - 1) {
  349. popupData.activeIndex++
  350. swipeRef.value?.next()
  351. const nextItem = data.itemList[popupData.activeIndex]
  352. nextTick(() => {
  353. nextItem.videoEle?.play()
  354. })
  355. console.log('🚀 ~ nextItem', nextItem)
  356. }
  357. }
  358. }
  359. //加载第一帧
  360. const handleFirstFrame = (video: HTMLVideoElement) => {
  361. // console.log("🚀 ~ 加载第一帧", video.videoWidth, video.videoHeight)
  362. const captureImage = function () {
  363. var canvas = document.createElement('canvas')
  364. canvas.width = video.videoWidth
  365. canvas.height = video.videoHeight
  366. canvas?.getContext('2d')?.drawImage(video, 0, 0, canvas.width, canvas.height)
  367. canvas.toBlob((blob) => {
  368. // console.log("🚀 ~ blob", blob)
  369. const imgUrl = URL.createObjectURL(blob as any)
  370. // console.log("🚀 ~ imgUrl", imgUrl)
  371. video.setAttribute('poster', imgUrl)
  372. })
  373. }
  374. captureImage()
  375. }
  376. return () => (
  377. <div class={styles.coursewarePlay}>
  378. <Swipe
  379. style={{ height: '100vh' }}
  380. ref={swipeRef}
  381. showIndicators={false}
  382. loop={false}
  383. vertical
  384. lazyRender={true}
  385. initialSwipe={popupData.firstIndex}
  386. onChange={handleSwipeChange}
  387. >
  388. {data.itemList.map((m: any, mIndex: number) => {
  389. return (
  390. <SwipeItem>
  391. <>
  392. <div
  393. class={styles.itemDiv}
  394. onClick={() => {
  395. clearTimeout(activeData.timer)
  396. clearTimeout(m.timer)
  397. if (Date.now() - activeData.nowTime < 300) {
  398. handleDbClick(m)
  399. return
  400. }
  401. activeData.nowTime = Date.now()
  402. activeData.timer = setTimeout(() => {
  403. activeData.model = !activeData.model
  404. }, 300)
  405. }}
  406. >
  407. {m.type === 'VIDEO' ? (
  408. <>
  409. <video
  410. playsinline="false"
  411. preload="auto"
  412. class="player"
  413. poster={iconVideobg}
  414. data-vid={m.id}
  415. src={m.content}
  416. loop={m.loop}
  417. // onLoadeddata={(e: Event) =>
  418. // handleFirstFrame(e.target as unknown as HTMLVideoElement)
  419. // }
  420. onLoadedmetadata={(e: Event) => {
  421. const videoEle = e.target as unknown as HTMLVideoElement
  422. m.currentTime = videoEle.currentTime
  423. m.duration = videoEle.duration
  424. m.videoEle = videoEle
  425. m.isprepare = true
  426. m.playModel = true
  427. console.log('视频准备完成')
  428. }}
  429. onTimeupdate={(e: Event) => {
  430. if (!m.isprepare) return
  431. const videoEle = e.target as unknown as HTMLVideoElement
  432. m.currentTime = videoEle.currentTime
  433. m.progress = Number(
  434. ((videoEle.currentTime / m.duration) * 100).toFixed(1)
  435. )
  436. // console.log('播放',videoEle.currentTime, m.progress)
  437. }}
  438. onPlay={() => {
  439. // 播放
  440. m.paused = 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. {m.playModel && (
  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>
  490. {m.paused ? (
  491. <Icon
  492. name={iconplay}
  493. onClick={(e: Event) => handlePlay(e, m)}
  494. />
  495. ) : (
  496. <Icon
  497. name={iconpause}
  498. onClick={(e: Event) => handlePaused(e, m)}
  499. />
  500. )}
  501. {m.loop ? (
  502. <Icon
  503. name={iconLoopActive}
  504. onClick={(e: Event) => {
  505. e.stopPropagation()
  506. m.loop = false
  507. }}
  508. />
  509. ) : (
  510. <Icon
  511. name={iconLoop}
  512. onClick={(e: Event) => {
  513. e.stopPropagation()
  514. m.loop = true
  515. }}
  516. />
  517. )}
  518. </div>
  519. <div>{m.name}</div>
  520. </div>
  521. </div>
  522. )}
  523. </Transition>
  524. </>
  525. ) : m.type === 'IMG' ? (
  526. <img src={m.content} />
  527. ) : (
  528. <MusicScore
  529. data-vid={m.id}
  530. music={m}
  531. onSetIframe={(el: any) => {
  532. m.iframeRef = el
  533. }}
  534. />
  535. )}
  536. {/* <Transition name="van-fade">
  537. {activeData.model && <div class={styles.playModel}></div>}
  538. </Transition> */}
  539. </div>
  540. </>
  541. </SwipeItem>
  542. )
  543. })}
  544. </Swipe>
  545. <Transition name="top">
  546. {activeData.model && (
  547. <div class={styles.headerContainer} ref={headeRef}>
  548. <div class={styles.backBtn} onClick={() => goback()}>
  549. <Icon name={iconBack} />
  550. 返回
  551. </div>
  552. <div class={styles.menu}>{popupData.tabName}</div>
  553. </div>
  554. )}
  555. </Transition>
  556. <Transition name="right">
  557. {activeData.model && (
  558. <div class={styles.rightFixedBtns}>
  559. <div class={styles.fullBtn} onClick={() => (popupData.open = true)}>
  560. <img src={iconMenu} />
  561. <span>知识点</span>
  562. </div>
  563. {data.isCourse && (
  564. <>
  565. <div
  566. class={[styles.fullBtn, styles.point]}
  567. onClick={() => gotoRollCall('student_roll_call')}
  568. >
  569. <img src={iconDian} />
  570. <span>点名</span>
  571. </div>
  572. <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
  573. <img src={iconPoint} />
  574. <span>签退</span>
  575. </div>
  576. </>
  577. )}
  578. </div>
  579. )}
  580. </Transition>
  581. <Transition name="left">
  582. {activeData.model && (
  583. <div class={styles.leftFixedBtns}>
  584. {popupData.activeIndex != 0 && (
  585. <div
  586. class={[styles.fullBtn, styles.prePoint]}
  587. onClick={() => handlePreAndNext('up')}
  588. >
  589. <img src={iconUp} />
  590. <span style={{ textAlign: 'center' }}>上一个</span>
  591. </div>
  592. )}
  593. {popupData.activeIndex != data.itemList.length - 1 && (
  594. <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
  595. <span style={{ textAlign: 'center' }}>下一个</span>
  596. <img src={iconDown} />
  597. </div>
  598. )}
  599. </div>
  600. )}
  601. </Transition>
  602. <Popup
  603. class={styles.popup}
  604. overlayClass={styles.overlayClass}
  605. position="right"
  606. round
  607. v-model:show={popupData.open}
  608. >
  609. <Points
  610. data={data.knowledgePointList}
  611. tabActive={popupData.tabActive}
  612. itemActive={popupData.itemActive}
  613. onHandleSelect={(res: any) => {
  614. // console.log(res)
  615. popupData.tabActive = res.tabActive
  616. popupData.itemActive = res.itemActive
  617. popupData.tabName = res.tabName
  618. popupData.open = false
  619. toggleMaterial()
  620. }}
  621. />
  622. </Popup>
  623. </div>
  624. )
  625. }
  626. })