index.tsx 19 KB

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