index.tsx 15 KB

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