index.tsx 24 KB

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