index.tsx 19 KB

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