index.tsx 23 KB

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