index.tsx 23 KB

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