index.tsx 17 KB

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