index.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. import { closeToast, Icon, Popup, showDialog, showToast } from 'vant';
  2. import {
  3. defineComponent,
  4. onMounted,
  5. reactive,
  6. nextTick,
  7. onUnmounted,
  8. ref,
  9. watch,
  10. Transition
  11. } from 'vue';
  12. import iconBack from './image/back.svg';
  13. import styles from './index.module.less';
  14. import 'plyr/dist/plyr.css';
  15. import { useRoute, useRouter } from 'vue-router';
  16. import {
  17. listenerMessage,
  18. postMessage,
  19. promisefiyPostMessage
  20. } from '@/helpers/native-message';
  21. import MusicScore from './component/musicScore';
  22. import iconMenu from './image/icon-menu.svg';
  23. import iconDian from './image/icon-dian.svg';
  24. import iconPoint from './image/icon-point.svg';
  25. import iconUp from './image/icon-up.svg';
  26. import iconDown from './image/icon-down.svg';
  27. import Points from './component/points';
  28. import { browser, getSecondRPM } from '@/helpers/utils';
  29. import { Vue3Lottie } from 'vue3-lottie';
  30. import playLoadData from './datas/data.json';
  31. import { usePageVisibility, useRect } from '@vant/use';
  32. import VideoPlay from './component/video-play';
  33. import Tool, { ToolItem, ToolType } from './component/tool';
  34. export default defineComponent({
  35. name: 'CoursewarePlay',
  36. setup() {
  37. const pageVisibility = usePageVisibility();
  38. const isPlay = ref(false);
  39. /** 页面显示和隐藏 */
  40. watch(pageVisibility, value => {
  41. const activeItem = data.itemList[popupData.activeIndex];
  42. if (activeItem.type != 'VIDEO') return;
  43. if (value == 'hidden') {
  44. isPlay.value = !activeItem.videoEle?.paused;
  45. togglePlay(activeItem, false);
  46. } else {
  47. // 页面显示,并且
  48. if (isPlay.value) togglePlay(activeItem, true);
  49. }
  50. });
  51. /** 设置播放容器 16:9 */
  52. const parentContainer = reactive({
  53. width: '100vw'
  54. });
  55. const setContainer = () => {
  56. let min = Math.min(screen.width, screen.height);
  57. let max = Math.max(screen.width, screen.height);
  58. let width = min * (16 / 9);
  59. if (width > max) {
  60. parentContainer.width = '100vw';
  61. return;
  62. } else {
  63. parentContainer.width = width + 'px';
  64. }
  65. };
  66. const handleInit = (type = 0) => {
  67. //设置容器16:9
  68. setContainer();
  69. // 横屏
  70. postMessage(
  71. {
  72. api: 'setRequestedOrientation',
  73. content: {
  74. orientation: type
  75. }
  76. },
  77. () => {
  78. console.log(234);
  79. }
  80. );
  81. // 头,包括返回箭头
  82. // postMessage({
  83. // api: 'setTitleBarVisibility',
  84. // content: {
  85. // status: type
  86. // }
  87. // })
  88. // 安卓的状态栏
  89. postMessage({
  90. api: 'setStatusBarVisibility',
  91. content: {
  92. isVisibility: type
  93. }
  94. });
  95. // 进入页面设置常量
  96. postMessage({
  97. api: 'keepScreenLongLight',
  98. content: {
  99. isOpenLight: type ? true : false
  100. }
  101. });
  102. };
  103. handleInit();
  104. onUnmounted(() => {
  105. handleInit(1);
  106. window.removeEventListener('message', iframeHandle);
  107. });
  108. const route = useRoute();
  109. const router = useRouter();
  110. const headeRef = ref();
  111. const data = reactive({
  112. detail: null,
  113. knowledgePointList: [] as any,
  114. itemList: [] as any,
  115. showHead: true,
  116. isCourse: false,
  117. isRecordPlay: false,
  118. videoRefs: {} as any[]
  119. });
  120. const activeData = reactive({
  121. isAutoPlay: true, // 是否自动播放
  122. nowTime: 0,
  123. model: true, // 遮罩
  124. isAnimation: true, // 是否动画
  125. videoBtns: true, // 视频
  126. currentTime: 0,
  127. duration: 0,
  128. timer: null as any,
  129. item: null as any
  130. });
  131. const getTempList = async (materialList: any, name: any) => {
  132. const list: any = [];
  133. const browserInfo = browser();
  134. for (let j = 0; j < materialList.length; j++) {
  135. const material = materialList[j];
  136. list.push({
  137. ...material,
  138. iframeRef: null,
  139. videoEle: null,
  140. tabName: name,
  141. autoPlay: false, //加载完成是否自动播放
  142. isprepare: false, // 视频是否加载完成
  143. isRender: false // 是否渲染了
  144. });
  145. }
  146. return list;
  147. };
  148. const getDetail = async () => {
  149. data.knowledgePointList = [
  150. {
  151. id: '1',
  152. name: '歌曲表演 大鹿',
  153. type: 'VIDEO',
  154. content: 'https://courseware.lexiaoya.cn/%E5%BF%85%E5%AD%A6%E5%BF%85%E7%9C%8B-%E8%90%A8%E5%85%8B%E6%96%AF-1-C4-4.mp4'
  155. },
  156. {
  157. id: '2',
  158. name: '知识 音的高低',
  159. type: 'IMG',
  160. content: 'https://gyt.ks3-cn-beijing.ksyuncs.com/courseware/1686815979899.png'
  161. },
  162. {
  163. id: '3',
  164. name: '欣赏 永远在童话里',
  165. type: 'IMG',
  166. content: 'https://gyt.ks3-cn-beijing.ksyuncs.com/courseware/1686815979899.png'
  167. },
  168. {
  169. id: '4',
  170. name: '唱歌 小红帽',
  171. type: 'SONG',
  172. content: '11707'
  173. }
  174. ];
  175. data.itemList = data.knowledgePointList.map((m: any) => {
  176. return {
  177. ...m,
  178. iframeRef: null,
  179. videoEle: null,
  180. autoPlay: false, //加载完成是否自动播放
  181. isprepare: false, // 视频是否加载完成
  182. isRender: false // 是否渲染了
  183. };
  184. });
  185. };
  186. // ifram事件处理
  187. const iframeHandle = (ev: MessageEvent) => {
  188. if (ev.data?.api === 'headerTogge') {
  189. activeData.model =
  190. ev.data.show || (ev.data.playState == 'play' ? false : true);
  191. }
  192. };
  193. onMounted(() => {
  194. postMessage({
  195. api: 'courseLoading',
  196. content: {
  197. show: false,
  198. type: 'fullscreen'
  199. }
  200. });
  201. getDetail();
  202. window.addEventListener('message', iframeHandle);
  203. });
  204. const playRef = ref();
  205. // 返回
  206. const goback = () => {
  207. try {
  208. playRef.value?.handleOut();
  209. } catch (error) {}
  210. postMessage({ api: 'goBack' });
  211. };
  212. const popupData = reactive({
  213. open: false,
  214. activeIndex: 0,
  215. tabActive: '',
  216. tabName: '',
  217. itemActive: '',
  218. itemName: '',
  219. guideOpen: false,
  220. toolOpen: false // 工具弹窗控制
  221. });
  222. /**停止所有的播放 */
  223. const handleStop = () => {
  224. for (let i = 0; i < data.itemList.length; i++) {
  225. const activeItem = data.itemList[i];
  226. if (activeItem.type === 'VIDEO' && activeItem.videoEle) {
  227. activeItem.videoEle.stop();
  228. }
  229. // console.log('🚀 ~ activeItem:', activeItem)
  230. // 停止曲谱的播放
  231. if (activeItem.type === 'SONG') {
  232. activeItem.iframeRef?.contentWindow?.postMessage(
  233. { api: 'setPlayState' },
  234. '*'
  235. );
  236. }
  237. }
  238. };
  239. // 切换素材
  240. const toggleMaterial = (itemActive: any) => {
  241. const index = data.itemList.findIndex((n: any) => n.id == itemActive);
  242. if (index > -1) {
  243. handleSwipeChange(index);
  244. }
  245. };
  246. /** 延迟收起模态框 */
  247. const setModelOpen = () => {
  248. clearTimeout(activeData.timer);
  249. closeToast();
  250. activeData.timer = setTimeout(() => {
  251. activeData.model = false;
  252. Object.values(data.videoRefs).map((n: any) =>
  253. n.toggleHideControl(false)
  254. );
  255. }, 4000);
  256. };
  257. /** 立即收起所有的模态框 */
  258. const clearModel = () => {
  259. clearTimeout(activeData.timer);
  260. closeToast();
  261. activeData.model = false;
  262. Object.values(data.videoRefs).map((n: any) => n.toggleHideControl(false));
  263. };
  264. const toggleModel = (type: boolean = true) => {
  265. activeData.model = type;
  266. Object.values(data.videoRefs).map((n: any) => n.toggleHideControl(type));
  267. };
  268. // 双击
  269. const handleDbClick = (item: any) => {
  270. if (item && item.type === 'VIDEO') {
  271. const videoEle: HTMLVideoElement = item.videoEle;
  272. if (videoEle) {
  273. if (videoEle.paused) {
  274. closeToast();
  275. videoEle.play();
  276. } else {
  277. showToast('已暂停');
  278. videoEle.pause();
  279. }
  280. }
  281. }
  282. };
  283. // 切换播放
  284. const togglePlay = (m: any, isPlay: boolean) => {
  285. if (isPlay) {
  286. m.videoEle?.play();
  287. } else {
  288. m.videoEle?.pause();
  289. }
  290. };
  291. const showIndex = ref(-4);
  292. const effectIndex = ref(3);
  293. const effects = [
  294. {
  295. prev: {
  296. transform: 'translate3d(0, 0, -800px) rotateX(180deg)'
  297. },
  298. next: {
  299. transform: 'translate3d(0, 0, -800px) rotateX(-180deg)'
  300. }
  301. },
  302. {
  303. prev: {
  304. transform: 'translate3d(-100%, 0, -800px)'
  305. },
  306. next: {
  307. transform: 'translate3d(100%, 0, -800px)'
  308. }
  309. },
  310. {
  311. prev: {
  312. transform: 'translate3d(-50%, 0, -800px) rotateY(80deg)'
  313. },
  314. next: {
  315. transform: 'translate3d(50%, 0, -800px) rotateY(-80deg)'
  316. }
  317. },
  318. {
  319. prev: {
  320. transform: 'translate3d(-100%, 0, -800px) rotateY(-120deg)'
  321. },
  322. next: {
  323. transform: 'translate3d(100%, 0, -800px) rotateY(120deg)'
  324. }
  325. },
  326. // 风车4
  327. {
  328. prev: {
  329. transform: 'translate3d(-50%, 50%, -800px) rotateZ(-14deg)',
  330. opacity: 0
  331. },
  332. next: {
  333. transform: 'translate3d(50%, 50%, -800px) rotateZ(14deg)',
  334. opacity: 0
  335. }
  336. },
  337. // 翻页5
  338. {
  339. prev: {
  340. transform: 'translateZ(-800px) rotate3d(0, -1, 0, 90deg)',
  341. opacity: 0
  342. },
  343. next: {
  344. transform: 'translateZ(-800px) rotate3d(0, 1, 0, 90deg)',
  345. opacity: 0
  346. },
  347. current: { transitionDelay: '700ms' }
  348. }
  349. ];
  350. const acitveTimer = ref();
  351. // 轮播切换
  352. const handleSwipeChange = (index: number) => {
  353. // 如果是当前正在播放 或者是视频最后一个
  354. if (popupData.activeIndex == index) return;
  355. handleStop();
  356. clearTimeout(acitveTimer.value);
  357. checkedAnimation(popupData.activeIndex, index);
  358. popupData.activeIndex = index;
  359. acitveTimer.value = setTimeout(
  360. () => {
  361. const item = data.itemList[index];
  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. }
  370. if (item.type === 'VIDEO') {
  371. // 自动播放下一个视频
  372. clearTimeout(activeData.timer);
  373. closeToast();
  374. item.autoPlay = true;
  375. nextTick(() => {
  376. item.videoEle?.play();
  377. });
  378. }
  379. }
  380. // requestAnimationFrame(() => {
  381. // const _effectIndex = effectIndex.value + 1;
  382. // effectIndex.value =
  383. // _effectIndex >= effects.length - 1 ? 0 : _effectIndex;
  384. // });
  385. },
  386. activeData.isAnimation ? 800 : 0
  387. );
  388. };
  389. /** 是否有转场动画 */
  390. const checkedAnimation = (index: number, nextIndex?: number) => {
  391. const item = data.itemList[index];
  392. const nextItem = data.itemList[nextIndex!];
  393. if (nextItem) {
  394. if (nextItem.knowledgePointId != item.knowledgePointId) {
  395. activeData.isAnimation = true;
  396. return;
  397. }
  398. const videoEle = item.videoEle;
  399. const nextVideo = nextItem.videoEle;
  400. if (videoEle && videoEle.duration < 8 && index < nextIndex!) {
  401. activeData.isAnimation = false;
  402. } else if (nextVideo && nextVideo.duration < 8 && index > nextIndex!) {
  403. activeData.isAnimation = false;
  404. } else {
  405. activeData.isAnimation = true;
  406. }
  407. } else {
  408. activeData.isAnimation = item?.adviseStudyTimeSecond < 8 ? false : true;
  409. }
  410. };
  411. // 上一个知识点, 下一个知识点
  412. const handlePreAndNext = (type: string) => {
  413. if (type === 'up') {
  414. handleSwipeChange(popupData.activeIndex - 1);
  415. } else {
  416. handleSwipeChange(popupData.activeIndex + 1);
  417. }
  418. };
  419. /** 弹窗关闭 */
  420. const handleClosePopup = () => {
  421. const item = data.itemList[popupData.activeIndex];
  422. if (item?.type == 'VIDEO' && !item.videoEle?.paused) {
  423. setModelOpen();
  424. }
  425. };
  426. return () => (
  427. <div id="playContent" class={styles.playContent}>
  428. <div
  429. onClick={() => {
  430. clearTimeout(activeData.timer);
  431. activeData.model = !activeData.model;
  432. Object.values(data.videoRefs).map((n: any) =>
  433. n.toggleHideControl(activeData.model)
  434. );
  435. }}>
  436. <div
  437. class={styles.coursewarePlay}
  438. style={{ width: parentContainer.width }}
  439. onClick={(e: Event) => {
  440. e.stopPropagation();
  441. setModelOpen();
  442. }}>
  443. <div class={styles.wraps}>
  444. {data.itemList.map((m: any, mIndex: number) => {
  445. const isRender =
  446. m.isRender || Math.abs(popupData.activeIndex - mIndex) < 2;
  447. const isEmtry = Math.abs(popupData.activeIndex - mIndex) > 4;
  448. if (isRender) {
  449. m.isRender = true;
  450. }
  451. return isRender ? (
  452. <div
  453. key={'index' + mIndex}
  454. class={[
  455. styles.itemDiv,
  456. popupData.activeIndex === mIndex && styles.itemActive,
  457. activeData.isAnimation && styles.acitveAnimation,
  458. Math.abs(popupData.activeIndex - mIndex) < 2
  459. ? styles.show
  460. : styles.hide
  461. ]}
  462. style={
  463. mIndex < popupData.activeIndex
  464. ? effects[effectIndex.value].prev
  465. : mIndex > popupData.activeIndex
  466. ? effects[effectIndex.value].next
  467. : {}
  468. }
  469. onClick={(e: Event) => {
  470. e.stopPropagation();
  471. clearTimeout(activeData.timer);
  472. if (Date.now() - activeData.nowTime < 300) {
  473. handleDbClick(m);
  474. return;
  475. }
  476. activeData.nowTime = Date.now();
  477. activeData.timer = setTimeout(() => {
  478. activeData.model = !activeData.model;
  479. Object.values(data.videoRefs).map((n: any) =>
  480. n.toggleHideControl(activeData.model)
  481. );
  482. if (activeData.model) {
  483. setModelOpen();
  484. }
  485. }, 300);
  486. }}>
  487. {m.type === 'VIDEO' ? (
  488. <>
  489. <VideoPlay
  490. ref={(v: any) => (data.videoRefs[mIndex] = v)}
  491. item={m}
  492. isEmtry={isEmtry}
  493. onLoadedmetadata={(videoItem: any) => {
  494. m.videoEle = videoItem;
  495. m.isprepare = true;
  496. }}
  497. onTogglePlay={(paused: boolean) => {
  498. m.autoPlay = false;
  499. if (
  500. paused ||
  501. popupData.open ||
  502. popupData.guideOpen
  503. ) {
  504. clearTimeout(activeData.timer);
  505. } else {
  506. setModelOpen();
  507. }
  508. }}
  509. onEnded={() => {
  510. const _index = popupData.activeIndex + 1;
  511. if (_index < data.itemList.length) {
  512. handleSwipeChange(_index);
  513. }
  514. }}
  515. onReset={() => {
  516. if (!m.videoEle?.paused) {
  517. setModelOpen();
  518. }
  519. }}
  520. />
  521. <Transition name="van-fade">
  522. {!m.isprepare && (
  523. <div class={styles.loadWrap}>
  524. <Vue3Lottie
  525. animationData={playLoadData}></Vue3Lottie>
  526. </div>
  527. )}
  528. </Transition>
  529. </>
  530. ) : m.type === 'IMG' ? (
  531. <img src={m.content} />
  532. ) : (
  533. <MusicScore
  534. activeModel={activeData.model}
  535. data-vid={m.id}
  536. music={m}
  537. onSetIframe={(el: any) => {
  538. m.iframeRef = el;
  539. }}
  540. />
  541. )}
  542. </div>
  543. ) : null;
  544. })}
  545. </div>
  546. <Transition name="right">
  547. {activeData.model && (
  548. <div
  549. class={styles.rightFixedBtns}
  550. onClick={(e: Event) => {
  551. e.stopPropagation();
  552. clearTimeout(activeData.timer);
  553. }}>
  554. <div
  555. class={[styles.fullBtn, styles.point]}
  556. onClick={() => (popupData.open = true)}>
  557. <img src={iconMenu} />
  558. <span>课件</span>
  559. </div>
  560. <div
  561. class={[
  562. styles.fullBtn,
  563. popupData.activeIndex == 0 && styles.btnsDisabled
  564. ]}
  565. onClick={() => handlePreAndNext('up')}>
  566. <img src={iconUp} />
  567. <span style={{ textAlign: 'center' }}>上一个</span>
  568. </div>
  569. <div
  570. class={[
  571. styles.fullBtn,
  572. popupData.activeIndex == data.itemList.length - 1 &&
  573. styles.btnsDisabled
  574. ]}
  575. onClick={() => handlePreAndNext('down')}>
  576. <span style={{ textAlign: 'center' }}>下一个</span>
  577. <img src={iconDown} />
  578. </div>
  579. </div>
  580. )}
  581. </Transition>
  582. </div>
  583. </div>
  584. <div
  585. style={{ transform: activeData.model ? '' : 'translateY(-100%)' }}
  586. class={styles.headerContainer}
  587. ref={headeRef}>
  588. <div class={styles.backBtn} onClick={() => goback()}>
  589. <Icon name={iconBack} />
  590. 返回
  591. </div>
  592. <div class={styles.menu}>{popupData.itemName}</div>
  593. </div>
  594. <Popup
  595. class={styles.popup}
  596. style={{ background: 'rgba(0,0,0, 0.75)' }}
  597. overlayClass={styles.overlayClass}
  598. position="right"
  599. round
  600. v-model:show={popupData.open}
  601. onClose={handleClosePopup}>
  602. <Points
  603. data={data.knowledgePointList}
  604. tabActive={popupData.tabActive}
  605. itemActive={popupData.itemActive}
  606. onHandleSelect={(res: any) => {
  607. popupData.open = false;
  608. toggleMaterial(res.itemActive);
  609. }}
  610. />
  611. </Popup>
  612. </div>
  613. );
  614. }
  615. });