index.tsx 17 KB

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