index.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. // history.go(-1)
  206. postMessage({ api: 'back' })
  207. }
  208. const swipeRef = ref()
  209. const popupData = reactive({
  210. firstIndex: 0,
  211. open: false,
  212. activeIndex: -1,
  213. tabActive: '',
  214. tabName: '',
  215. itemActive: '',
  216. itemName: ''
  217. })
  218. // 设置当前的激活状态
  219. const setActiveData = (val: any, oldVal: any) => {
  220. handleStopVideo()
  221. handleStopMusicScore()
  222. }
  223. watch(() => popupData.activeIndex, setActiveData)
  224. // 停止所有的播放
  225. const handleStopVideo = () => {
  226. data.itemList.forEach((m: any) => {
  227. m.videoEle?.pause()
  228. })
  229. }
  230. // 停止曲谱的播放
  231. const handleStopMusicScore = () => {
  232. data.itemList.forEach((m: any) => {
  233. m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*')
  234. })
  235. }
  236. // 切换素材
  237. const toggleMaterial = () => {
  238. const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive)
  239. if (index > -1) {
  240. popupData.activeIndex = index
  241. swipeRef.value?.swipeTo(index)
  242. }
  243. }
  244. // 轮播切换
  245. const handleSwipeChange = (val: any) => {
  246. popupData.activeIndex = val
  247. const item = data.itemList[val]
  248. if (item) {
  249. popupData.tabActive = item.knowledgePointId
  250. popupData.itemActive = item.id
  251. popupData.itemName = item.name
  252. popupData.tabName = item.tabName
  253. }
  254. }
  255. // 上一个知识点, 下一个知识点
  256. const handlePreAndNext = (type: string) => {
  257. if (type === 'up') {
  258. swipeRef.value?.prev()
  259. } else {
  260. swipeRef.value?.next()
  261. }
  262. }
  263. // 去点名,签退
  264. const gotoRollCall = (pageTag: string) => {
  265. postMessage({
  266. api: 'open_app_page',
  267. content: {
  268. action: 'app',
  269. pageTag: pageTag,
  270. url: '',
  271. params: JSON.stringify({ courseId: route.query.courseId })
  272. }
  273. })
  274. }
  275. // 双击
  276. const handleDbClick = (item: any) => {
  277. // console.log(activeData.item)
  278. if (item && item.type === 'VIDEO') {
  279. const videoEle: HTMLVideoElement = document.querySelector(`[data-vid='${item.id}']`)!
  280. if (videoEle) {
  281. if (videoEle.paused) {
  282. closeToast()
  283. videoEle.play()
  284. } else {
  285. showToast('已暂停')
  286. videoEle.pause()
  287. }
  288. }
  289. item.timer = setTimeout(() => {
  290. activeData.model = false
  291. }, 3000)
  292. // console.dir(videoEle)
  293. }
  294. }
  295. // 调整播放进度
  296. const handleChangeSlider = (val: any, m: any) => {
  297. if (m?.videoEle) {
  298. m.videoEle.currentTime = val
  299. }
  300. }
  301. //当前视频播放完
  302. const handleEnded = (m: any) => {
  303. // console.log(m)
  304. // 自动播放下一个知识点
  305. if (m.autoPlay) {
  306. if (popupData.activeIndex != data.itemList.length - 1) {
  307. popupData.activeIndex++
  308. swipeRef.value?.next()
  309. const nextItem = data.itemList[popupData.activeIndex]
  310. nextTick(() => {
  311. nextItem.videoEle?.play()
  312. })
  313. console.log('🚀 ~ nextItem', nextItem)
  314. }
  315. }
  316. }
  317. //加载第一帧
  318. const handleFirstFrame = (video: HTMLVideoElement) => {
  319. // console.log("🚀 ~ 加载第一帧", video.videoWidth, video.videoHeight)
  320. const captureImage = function () {
  321. var canvas = document.createElement('canvas')
  322. canvas.width = video.videoWidth
  323. canvas.height = video.videoHeight
  324. canvas?.getContext('2d')?.drawImage(video, 0, 0, canvas.width, canvas.height)
  325. canvas.toBlob((blob) => {
  326. // console.log("🚀 ~ blob", blob)
  327. const imgUrl = URL.createObjectURL(blob as any)
  328. // console.log("🚀 ~ imgUrl", imgUrl)
  329. video.setAttribute('poster', imgUrl)
  330. })
  331. }
  332. captureImage()
  333. }
  334. return () => (
  335. <div class={styles.coursewarePlay}>
  336. <Swipe
  337. style={{ height: '100vh' }}
  338. ref={swipeRef}
  339. showIndicators={false}
  340. loop={false}
  341. vertical
  342. lazyRender={true}
  343. initialSwipe={popupData.firstIndex}
  344. onChange={handleSwipeChange}
  345. >
  346. {data.itemList.map((m: any, mIndex: number) => {
  347. return (
  348. <SwipeItem>
  349. <>
  350. <div
  351. class={styles.itemDiv}
  352. onClick={() => {
  353. clearTimeout(activeData.timer)
  354. clearTimeout(m.timer)
  355. if (Date.now() - activeData.nowTime < 300) {
  356. handleDbClick(m)
  357. return
  358. }
  359. activeData.nowTime = Date.now()
  360. activeData.timer = setTimeout(() => {
  361. activeData.model = !activeData.model
  362. }, 300)
  363. }}
  364. >
  365. {m.type === 'VIDEO' ? (
  366. <>
  367. <video
  368. playsinline="false"
  369. preload="auto"
  370. class="player"
  371. crossorigin="anonymous"
  372. poster={iconVideobg}
  373. data-vid={m.id}
  374. src={m.content}
  375. loop={m.loop}
  376. onLoadeddata={(e: Event) =>
  377. handleFirstFrame(e.target as unknown as HTMLVideoElement)
  378. }
  379. onLoadedmetadata={(e: Event) => {
  380. const videoEle = e.target as unknown as HTMLVideoElement
  381. m.currentTime = videoEle.currentTime
  382. m.duration = videoEle.duration
  383. m.videoEle = videoEle
  384. }}
  385. onTimeupdate={(e: Event) => {
  386. const videoEle = e.target as unknown as HTMLVideoElement
  387. m.currentTime = videoEle.currentTime
  388. }}
  389. onPlay={() => {
  390. // 播放
  391. m.paused = false
  392. }}
  393. onPause={() => {
  394. //暂停
  395. clearTimeout(m.timer)
  396. m.paused = true
  397. }}
  398. onEnded={() => handleEnded(m)}
  399. >
  400. <source src={m.content} type="video/mp4" />
  401. </video>
  402. <Transition name="bottom">
  403. {m.playModel && (
  404. <div class={styles.bottomFixedContainer}>
  405. <div class={styles.time}>
  406. <span>{getSecondRPM(m.currentTime)}</span>
  407. <span>{getSecondRPM(m.duration)}</span>
  408. </div>
  409. <div class={styles.slider}>
  410. <Slider
  411. buttonSize={16}
  412. step={0.01}
  413. v-model:modelValue={m.currentTime}
  414. onUpdate:modelValue={(val: any) => handleChangeSlider(val, m)}
  415. min={0}
  416. max={m.duration}
  417. />
  418. </div>
  419. <div class={styles.actions}>
  420. <div>
  421. {m.paused ? (
  422. <Icon
  423. name={iconplay}
  424. onClick={(e: Event) => {
  425. e.stopPropagation()
  426. clearTimeout(m.timer)
  427. closeToast()
  428. m.videoEle?.play()
  429. m.paused = false
  430. m.timer = setTimeout(() => {
  431. activeData.model = false
  432. }, 3000)
  433. }}
  434. />
  435. ) : (
  436. <Icon
  437. name={iconpause}
  438. onClick={(e: Event) => {
  439. e.stopPropagation()
  440. console.log('点击暂停')
  441. m.videoEle?.pause()
  442. m.paused = true
  443. }}
  444. />
  445. )}
  446. {m.loop ? (
  447. <Icon
  448. name={iconLoopActive}
  449. onClick={(e: Event) => {
  450. e.stopPropagation()
  451. m.loop = false
  452. }}
  453. />
  454. ) : (
  455. <Icon
  456. name={iconLoop}
  457. onClick={(e: Event) => {
  458. e.stopPropagation()
  459. m.loop = true
  460. }}
  461. />
  462. )}
  463. </div>
  464. <div>{popupData.itemName}</div>
  465. </div>
  466. </div>
  467. )}
  468. </Transition>
  469. </>
  470. ) : m.type === 'IMG' ? (
  471. <img src={m.content} />
  472. ) : (
  473. <MusicScore
  474. data-vid={m.id}
  475. music={m}
  476. onSetIframe={(el: any) => {
  477. m.iframeRef = el
  478. }}
  479. />
  480. )}
  481. {/* <Transition name="van-fade">
  482. {activeData.model && <div class={styles.playModel}></div>}
  483. </Transition> */}
  484. </div>
  485. </>
  486. </SwipeItem>
  487. )
  488. })}
  489. </Swipe>
  490. <Transition name="top">
  491. {activeData.model && (
  492. <div class={styles.headerContainer} ref={headeRef}>
  493. <div class={styles.backBtn} onClick={() => goback()}>
  494. <Icon name={iconBack} />
  495. 返回
  496. </div>
  497. <div class={styles.menu}>{popupData.tabName}</div>
  498. </div>
  499. )}
  500. </Transition>
  501. <Transition name="right">
  502. {activeData.model && (
  503. <div class={styles.rightFixedBtns}>
  504. <div class={styles.fullBtn} onClick={() => (popupData.open = true)}>
  505. <img src={iconMenu} />
  506. <span>知识点</span>
  507. </div>
  508. {route.query.courseId && (
  509. <>
  510. <div
  511. class={[styles.fullBtn, styles.point]}
  512. onClick={() => gotoRollCall('student_roll_call')}
  513. >
  514. <img src={iconDian} />
  515. <span>点名</span>
  516. </div>
  517. <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
  518. <img src={iconPoint} />
  519. <span>签退</span>
  520. </div>
  521. </>
  522. )}
  523. </div>
  524. )}
  525. </Transition>
  526. <Transition name="left">
  527. {activeData.model && (
  528. <div class={styles.leftFixedBtns}>
  529. {popupData.activeIndex != 0 && (
  530. <div
  531. class={[styles.fullBtn, styles.prePoint]}
  532. onClick={() => handlePreAndNext('up')}
  533. >
  534. <img src={iconUp} />
  535. <span style={{ textAlign: 'center' }}>上一个</span>
  536. </div>
  537. )}
  538. {popupData.activeIndex != data.itemList.length - 1 && (
  539. <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
  540. <span style={{ textAlign: 'center' }}>下一个</span>
  541. <img src={iconDown} />
  542. </div>
  543. )}
  544. </div>
  545. )}
  546. </Transition>
  547. <Popup
  548. class={styles.popup}
  549. overlayClass={styles.overlayClass}
  550. position="right"
  551. round
  552. v-model:show={popupData.open}
  553. >
  554. <Points
  555. data={data.knowledgePointList}
  556. tabActive={popupData.tabActive}
  557. itemActive={popupData.itemActive}
  558. onHandleSelect={(res: any) => {
  559. // console.log(res)
  560. popupData.tabActive = res.tabActive
  561. popupData.itemActive = res.itemActive
  562. popupData.tabName = res.tabName
  563. popupData.open = false
  564. toggleMaterial()
  565. }}
  566. />
  567. </Popup>
  568. </div>
  569. )
  570. }
  571. })