index.tsx 19 KB

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