index.tsx 23 KB

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