index.tsx 21 KB

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