index.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 from 'plyr'
  27. import 'plyr/dist/plyr.css'
  28. import request from '@/helpers/request'
  29. import { state } from '@/state'
  30. import { useRoute } from 'vue-router'
  31. import { listenerMessage, postMessage, promisefiyPostMessage } from '@/helpers/native-message'
  32. import MusicScore from './component/musicScore'
  33. import iconMenu from './image/icon-menu.svg'
  34. import iconDian from './image/icon-dian.svg'
  35. import iconPoint from './image/icon-point.svg'
  36. import iconLoop from './image/icon-loop.svg'
  37. import iconLoopActive from './image/icon-loop-active.svg'
  38. import iconplay from './image/icon-play.svg'
  39. import iconpause from './image/icon-pause.svg'
  40. import iconUp from './image/icon-up.svg'
  41. import iconDown from './image/icon-down.svg'
  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. for (let i = 0; i < data.knowledgePointList.length; i++) {
  122. const item = data.knowledgePointList[i]
  123. for (let j = 0; j < item.materialList.length; j++) {
  124. const material = item.materialList[j]
  125. if (browserInfo.isApp && ['VIDEO', 'IMG'].includes(material.type)){
  126. const localData = await getCacheFilePath(material)
  127. if (localData?.content?.localPath){
  128. material.url = material.content
  129. material.content = localData.content.localPath
  130. // console.log("🚀 ~ material", material)
  131. }
  132. }
  133. if (popupData.itemActive === '') {
  134. popupData.tabName = item.name
  135. popupData.tabActive = material.knowledgePointId
  136. popupData.itemActive = material.id
  137. popupData.itemName = material.name
  138. popupData.activeIndex = 0
  139. }
  140. let videoItem = {}
  141. if (material.type === 'VIDEO') {
  142. videoItem = {
  143. currentTime: 0,
  144. duration: 100,
  145. paused: true,
  146. loop: false,
  147. videoEle: null,
  148. timer: null,
  149. playModel: true
  150. }
  151. }
  152. list.push({
  153. ...material,
  154. ...videoItem,
  155. iframeRef: null
  156. })
  157. }
  158. }
  159. console.log('🚀 ~ list', list)
  160. data.itemList = list
  161. }
  162. const getDetail = async () => {
  163. try {
  164. const res: any = await request.get(
  165. state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}`
  166. )
  167. if (Array.isArray(res?.data)) {
  168. data.detail = res.data
  169. }
  170. if (Array.isArray(res?.data?.knowledgePointList)) {
  171. data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
  172. n.index = 0
  173. return n
  174. })
  175. getItemList()
  176. }
  177. } catch (error) {}
  178. }
  179. // ifram事件处理
  180. const iframeHandle = (ev: MessageEvent) => {
  181. // console.log(ev.data)
  182. if (ev.data?.api === 'headerTogge') {
  183. activeData.model = ev.data.show
  184. }
  185. }
  186. onMounted(() => {
  187. getDetail()
  188. window.addEventListener('message', iframeHandle)
  189. })
  190. // 返回
  191. const goback = () => {
  192. // history.go(-1)
  193. postMessage({ api: 'back' })
  194. }
  195. const swipeRef = ref()
  196. const popupData = reactive({
  197. open: false,
  198. activeIndex: -1,
  199. tabActive: '',
  200. tabName: '',
  201. itemActive: '',
  202. itemName: ''
  203. })
  204. // 设置当前的激活状态
  205. const setActiveData = (val: any, oldVal: any) => {
  206. handleStopVideo()
  207. handleStopMusicScore()
  208. }
  209. watch(() => popupData.activeIndex, setActiveData)
  210. // 停止所有的播放
  211. const handleStopVideo = () => {
  212. data.itemList.forEach((m: any) => {
  213. m.videoEle?.pause()
  214. })
  215. }
  216. // 停止曲谱的播放
  217. const handleStopMusicScore = () => {
  218. data.itemList.forEach((m: any) => {
  219. m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*')
  220. })
  221. }
  222. // 获取name
  223. const setAllName = () => {
  224. const item = data.itemList.find((n: any) => n.id == popupData.itemActive)
  225. const tab = data.knowledgePointList.find((n: any) => n.id == popupData.tabActive)
  226. if (item) {
  227. popupData.itemName = item.name
  228. }
  229. if (tab) {
  230. popupData.tabName = tab.name
  231. }
  232. }
  233. // 切换素材
  234. const toggleMaterial = () => {
  235. const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive)
  236. if (index > -1) {
  237. popupData.activeIndex = index
  238. swipeRef.value?.swipeTo(index)
  239. setAllName()
  240. }
  241. }
  242. // 轮播切换
  243. const handleSwipeChange = (val: any) => {
  244. popupData.activeIndex = val
  245. const item = data.itemList[val]
  246. if (item) {
  247. popupData.tabActive = item.knowledgePointId
  248. popupData.itemActive = item.id
  249. setAllName()
  250. }
  251. }
  252. // 上一个知识点, 下一个知识点
  253. const handlePreAndNext = (type: string) => {
  254. if (type === 'up') {
  255. swipeRef.value?.prev()
  256. } else {
  257. swipeRef.value?.next()
  258. }
  259. }
  260. // 去点名,签退
  261. const gotoRollCall = (pageTag: string) => {
  262. postMessage({
  263. api: 'open_app_page',
  264. content: {
  265. action: 'app',
  266. pageTag: pageTag,
  267. url: '',
  268. params: JSON.stringify({ courseId: route.query.courseId })
  269. }
  270. })
  271. }
  272. // 双击
  273. const handleDbClick = (item: any) => {
  274. // console.log(activeData.item)
  275. if (item && item.type === 'VIDEO') {
  276. const videoEle: HTMLVideoElement = document.querySelector(`[data-vid='${item.id}']`)!
  277. if (videoEle) {
  278. if (videoEle.paused) {
  279. closeToast()
  280. videoEle.play()
  281. } else {
  282. showToast('已暂停')
  283. videoEle.pause()
  284. }
  285. }
  286. item.timer = setTimeout(() => {
  287. activeData.model = false
  288. }, 3000)
  289. console.dir(videoEle)
  290. }
  291. }
  292. // 调整播放进度
  293. const handleChangeSlider = (val: any, m: any) => {
  294. if (m?.videoEle) {
  295. m.videoEle.currentTime = val
  296. }
  297. }
  298. return () => (
  299. <div class={styles.coursewarePlay}>
  300. <Swipe
  301. style={{ height: '100vh' }}
  302. ref={swipeRef}
  303. showIndicators={false}
  304. loop={false}
  305. vertical
  306. lazyRender={true}
  307. onChange={handleSwipeChange}
  308. >
  309. {data.itemList.map((m: any, mIndex: number) => {
  310. return (
  311. <SwipeItem>
  312. <>
  313. <div
  314. class={styles.itemDiv}
  315. onClick={() => {
  316. clearTimeout(activeData.timer)
  317. clearTimeout(m.timer)
  318. if (Date.now() - activeData.nowTime < 300) {
  319. handleDbClick(m)
  320. return
  321. }
  322. activeData.nowTime = Date.now()
  323. activeData.timer = setTimeout(() => {
  324. activeData.model = !activeData.model
  325. }, 300)
  326. }}
  327. >
  328. {m.type === 'VIDEO' ? (
  329. <>
  330. <video
  331. playsinline="false"
  332. preload="auto"
  333. class="player"
  334. data-vid={m.id}
  335. src={m.content}
  336. loop={m.loop}
  337. onLoadedmetadata={(e: Event) => {
  338. const videoEle = e.target as unknown as HTMLVideoElement
  339. // videoEle.currentTime = 0.5
  340. m.currentTime = videoEle.currentTime
  341. m.duration = videoEle.duration
  342. m.videoEle = videoEle
  343. }}
  344. onTimeupdate={(e: Event) => {
  345. const videoEle = e.target as unknown as HTMLVideoElement
  346. m.currentTime = videoEle.currentTime
  347. }}
  348. onPlay={() => {
  349. m.paused = false
  350. }}
  351. onPause={() => {
  352. clearTimeout(m.timer)
  353. m.paused = true
  354. }}
  355. >
  356. <source src={m.content} type="video/mp4" />
  357. </video>
  358. <Transition name="bottom">
  359. {m.playModel && (
  360. <div class={styles.bottomFixedContainer}>
  361. <div class={styles.time}>
  362. <span>{getSecondRPM(m.currentTime)}</span>
  363. <span>{getSecondRPM(m.duration)}</span>
  364. </div>
  365. <div class={styles.slider}>
  366. <Slider
  367. buttonSize={16}
  368. step={0.01}
  369. v-model:modelValue={m.currentTime}
  370. onUpdate:modelValue={(val: any) => handleChangeSlider(val, m)}
  371. min={0}
  372. max={m.duration}
  373. />
  374. </div>
  375. <div class={styles.actions}>
  376. <div>
  377. {m.paused ? (
  378. <Icon
  379. name={iconplay}
  380. onClick={(e: Event) => {
  381. e.stopPropagation()
  382. clearTimeout(m.timer)
  383. console.log('点击播放', m.videoEle)
  384. m.videoEle?.play()
  385. m.paused = false
  386. m.timer = setTimeout(() => {
  387. activeData.model = false
  388. }, 3000)
  389. }}
  390. />
  391. ) : (
  392. <Icon
  393. name={iconpause}
  394. onClick={(e: Event) => {
  395. e.stopPropagation()
  396. console.log('点击暂停')
  397. m.videoEle?.pause()
  398. m.paused = true
  399. }}
  400. />
  401. )}
  402. {m.loop ? (
  403. <Icon
  404. name={iconLoopActive}
  405. onClick={(e: Event) => {
  406. e.stopPropagation()
  407. m.loop = false
  408. }}
  409. />
  410. ) : (
  411. <Icon
  412. name={iconLoop}
  413. onClick={(e: Event) => {
  414. e.stopPropagation()
  415. m.loop = true
  416. }}
  417. />
  418. )}
  419. </div>
  420. <div>{popupData.itemName}</div>
  421. </div>
  422. </div>
  423. )}
  424. </Transition>
  425. </>
  426. ) : m.type === 'IMG' ? (
  427. <img src={m.content} />
  428. ) : (
  429. <MusicScore
  430. data-vid={m.id}
  431. music={m}
  432. onSetIframe={(el: any) => {
  433. m.iframeRef = el
  434. }}
  435. />
  436. )}
  437. {/* <Transition name="van-fade">
  438. {activeData.model && <div class={styles.playModel}></div>}
  439. </Transition> */}
  440. </div>
  441. </>
  442. </SwipeItem>
  443. )
  444. })}
  445. </Swipe>
  446. <Transition name="top">
  447. {activeData.model && (
  448. <div class={styles.headerContainer} ref={headeRef}>
  449. <div class={styles.backBtn} onClick={() => goback()}>
  450. <Icon name={iconBack} />
  451. 返回
  452. </div>
  453. <div class={styles.menu}>{popupData.tabName}</div>
  454. </div>
  455. )}
  456. </Transition>
  457. <Transition name="right">
  458. {activeData.model && (
  459. <div class={styles.rightFixedBtns}>
  460. <div class={styles.fullBtn} onClick={() => (popupData.open = true)}>
  461. <img src={iconMenu} />
  462. <span>知识点</span>
  463. </div>
  464. {route.query.courseId && (
  465. <>
  466. <div
  467. class={[styles.fullBtn, styles.point]}
  468. onClick={() => gotoRollCall('student_roll_call')}
  469. >
  470. <img src={iconDian} />
  471. <span>点名</span>
  472. </div>
  473. <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
  474. <img src={iconPoint} />
  475. <span>签退</span>
  476. </div>
  477. </>
  478. )}
  479. </div>
  480. )}
  481. </Transition>
  482. <Transition name="left">
  483. {activeData.model && (
  484. <div class={styles.leftFixedBtns}>
  485. <div class={[styles.fullBtn, styles.prePoint]} onClick={() => handlePreAndNext('up')}>
  486. <img src={iconUp} />
  487. <span style={{ textAlign: 'center' }}>上一个</span>
  488. </div>
  489. <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
  490. <span style={{ textAlign: 'center' }}>下一个</span>
  491. <img src={iconDown} />
  492. </div>
  493. </div>
  494. )}
  495. </Transition>
  496. <Popup
  497. class={styles.popup}
  498. overlayClass={styles.overlayClass}
  499. position="right"
  500. round
  501. v-model:show={popupData.open}
  502. >
  503. <Points
  504. data={data.knowledgePointList}
  505. tabActive={popupData.tabActive}
  506. itemActive={popupData.itemActive}
  507. onHandleSelect={(res: any) => {
  508. // console.log(res)
  509. popupData.tabActive = res.tabActive
  510. popupData.itemActive = res.itemActive
  511. popupData.open = false
  512. toggleMaterial()
  513. }}
  514. />
  515. </Popup>
  516. </div>
  517. )
  518. }
  519. })