index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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: item.coverImg
  124. };
  125. });
  126. }
  127. data.itemList = data.knowledgePointList.map((m: any, index: number) => {
  128. if (!popupData.itemActive) {
  129. popupData.itemActive = m.id;
  130. popupData.itemName = m.name;
  131. }
  132. return {
  133. ...m,
  134. iframeRef: null,
  135. videoEle: null,
  136. autoPlay: false, //加载完成是否自动播放
  137. isprepare: false, // 视频是否加载完成
  138. isRender: false // 是否渲染了
  139. };
  140. });
  141. };
  142. // ifram事件处理
  143. const iframeHandle = (ev: MessageEvent) => {
  144. if (ev.data?.api === 'headerTogge') {
  145. activeData.model =
  146. ev.data.show || (ev.data.playState == 'play' ? false : true);
  147. }
  148. };
  149. onMounted(() => {
  150. postMessage({
  151. api: 'courseLoading',
  152. content: {
  153. show: false,
  154. type: 'fullscreen'
  155. }
  156. });
  157. getDetail();
  158. window.addEventListener('message', iframeHandle);
  159. });
  160. const playRef = ref();
  161. // 返回
  162. const goback = () => {
  163. try {
  164. playRef.value?.handleOut();
  165. } catch (error) {}
  166. postMessage({ api: 'goBack' });
  167. // router.back()
  168. };
  169. const popupData = reactive({
  170. open: false,
  171. activeIndex: 0,
  172. itemActive: '',
  173. itemName: ''
  174. });
  175. // 切换素材
  176. const toggleMaterial = (itemActive: any) => {
  177. const index = data.itemList.findIndex((n: any) => n.id == itemActive);
  178. if (index > -1) {
  179. handleSwipeChange(index);
  180. }
  181. };
  182. /** 延迟收起模态框 */
  183. const setModelOpen = () => {
  184. clearTimeout(activeData.timer);
  185. closeToast();
  186. activeData.model = true;
  187. activeData.timer = setTimeout(() => {
  188. activeData.model = false;
  189. }, 4000);
  190. };
  191. // 双击
  192. const handleDbClick = (item: any) => {
  193. if (item && ['VIDEO'].includes(item.type)) {
  194. console.log('双击');
  195. }
  196. };
  197. const effectIndex = ref(3);
  198. const effects = [
  199. {
  200. prev: {
  201. transform: 'translate3d(0, 0, -800px) rotateX(180deg)'
  202. },
  203. next: {
  204. transform: 'translate3d(0, 0, -800px) rotateX(-180deg)'
  205. }
  206. },
  207. {
  208. prev: {
  209. transform: 'translate3d(-100%, 0, -800px)'
  210. },
  211. next: {
  212. transform: 'translate3d(100%, 0, -800px)'
  213. }
  214. },
  215. {
  216. prev: {
  217. transform: 'translate3d(-50%, 0, -800px) rotateY(80deg)'
  218. },
  219. next: {
  220. transform: 'translate3d(50%, 0, -800px) rotateY(-80deg)'
  221. }
  222. },
  223. {
  224. prev: {
  225. transform: 'translate3d(-100%, 0, -800px) rotateY(-120deg)'
  226. },
  227. next: {
  228. transform: 'translate3d(100%, 0, -800px) rotateY(120deg)'
  229. }
  230. },
  231. // 风车4
  232. {
  233. prev: {
  234. transform: 'translate3d(-50%, 50%, -800px) rotateZ(-14deg)',
  235. opacity: 0
  236. },
  237. next: {
  238. transform: 'translate3d(50%, 50%, -800px) rotateZ(14deg)',
  239. opacity: 0
  240. }
  241. },
  242. // 翻页5
  243. {
  244. prev: {
  245. transform: 'translateZ(-800px) rotate3d(0, -1, 0, 90deg)',
  246. opacity: 0
  247. },
  248. next: {
  249. transform: 'translateZ(-800px) rotate3d(0, 1, 0, 90deg)',
  250. opacity: 0
  251. },
  252. current: { transitionDelay: '700ms' }
  253. }
  254. ];
  255. const acitveTimer = ref();
  256. // 轮播切换
  257. const handleSwipeChange = (index: number) => {
  258. // 如果是当前正在播放 或者是视频最后一个
  259. if (popupData.activeIndex == index) return;
  260. clearTimeout(acitveTimer.value);
  261. const item = data.itemList[index];
  262. popupData.activeIndex = index;
  263. popupData.itemActive = item.id;
  264. popupData.itemName = item.name;
  265. if (item.type == 'MUSIC') {
  266. activeData.model = true;
  267. }
  268. };
  269. // 上一个知识点, 下一个知识点
  270. const handlePreAndNext = (type: string) => {
  271. if (type === 'up') {
  272. handleSwipeChange(popupData.activeIndex - 1);
  273. } else {
  274. handleSwipeChange(popupData.activeIndex + 1);
  275. }
  276. };
  277. /** 弹窗关闭 */
  278. const handleClosePopup = () => {
  279. setModelOpen();
  280. };
  281. return () => (
  282. <div id="playContent" class={styles.playContent}>
  283. <div onClick={() => setModelOpen()} onTouchmove={() => setModelOpen()}>
  284. <div
  285. class={styles.coursewarePlay}
  286. style={{ width: parentContainer.width }}>
  287. <div class={styles.wraps}>
  288. {data.itemList.map((m: any, mIndex: number) => {
  289. const isRender =
  290. m.isRender || Math.abs(popupData.activeIndex - mIndex) < 2;
  291. const isEmtry = Math.abs(popupData.activeIndex - mIndex) > 4;
  292. if (isRender) {
  293. m.isRender = true;
  294. }
  295. return isRender ? (
  296. <div
  297. key={'index' + mIndex}
  298. class={[
  299. styles.itemDiv,
  300. popupData.activeIndex === mIndex && styles.itemActive,
  301. activeData.isAnimation && styles.acitveAnimation,
  302. Math.abs(popupData.activeIndex - mIndex) < 2
  303. ? styles.show
  304. : styles.hide
  305. ]}
  306. style={
  307. mIndex < popupData.activeIndex
  308. ? effects[effectIndex.value].prev
  309. : mIndex > popupData.activeIndex
  310. ? effects[effectIndex.value].next
  311. : {}
  312. }
  313. onClick={(e: Event) => {
  314. if (Date.now() - activeData.nowTime < 300) {
  315. handleDbClick(m);
  316. return;
  317. }
  318. activeData.nowTime = Date.now();
  319. }}>
  320. {m.type === 'IMG' && <img src={m.content} />}
  321. {m.type === 'VIDEO' && (
  322. <VideoItem
  323. item={m}
  324. show={popupData.activeIndex === mIndex}
  325. pageVisibility={pageVisibility.value}
  326. showModel={activeData.model}
  327. isEmtry={isEmtry}
  328. onLoadedmetadata={() => {
  329. m.isprepare = true;
  330. }}
  331. onEnded={() => {
  332. const _index = popupData.activeIndex + 1;
  333. if (_index < data.itemList.length) {
  334. handleSwipeChange(_index);
  335. }
  336. }}
  337. />
  338. )}
  339. {m.type === 'SONG' && (
  340. <AudioItem
  341. item={m}
  342. show={popupData.activeIndex === mIndex}
  343. pageVisibility={pageVisibility.value}
  344. showModel={activeData.model}
  345. isEmtry={isEmtry}
  346. onEnded={() => {
  347. const _index = popupData.activeIndex + 1;
  348. if (_index < data.itemList.length) {
  349. handleSwipeChange(_index);
  350. }
  351. }}
  352. />
  353. )}
  354. {m.type === 'MUSIC' && (
  355. <MusicScore
  356. pageVisibility={pageVisibility.value}
  357. show={popupData.activeIndex === mIndex}
  358. activeModel={activeData.model}
  359. data-vid={m.id}
  360. music={m}
  361. />
  362. )}
  363. {m.type === 'VIDEO' && (
  364. <Transition name="van-fade">
  365. {!m.isprepare && (
  366. <div class={styles.loadWrap}>
  367. <Vue3Lottie
  368. style={{ width: '100%', height: '100%' }}
  369. animationData={playLoadData}></Vue3Lottie>
  370. </div>
  371. )}
  372. </Transition>
  373. )}
  374. </div>
  375. ) : (
  376. <div
  377. key={'index' + mIndex}
  378. class={[
  379. styles.itemDiv,
  380. popupData.activeIndex === mIndex && styles.itemActive,
  381. activeData.isAnimation && styles.acitveAnimation,
  382. Math.abs(popupData.activeIndex - mIndex) < 2
  383. ? styles.show
  384. : styles.hide
  385. ]}
  386. style={
  387. mIndex < popupData.activeIndex
  388. ? effects[effectIndex.value].prev
  389. : mIndex > popupData.activeIndex
  390. ? effects[effectIndex.value].next
  391. : {}
  392. }></div>
  393. );
  394. })}
  395. </div>
  396. <Transition name="right">
  397. {activeData.model && (
  398. <div
  399. class={styles.rightFixedBtns}
  400. onClick={(e: Event) => {
  401. e.stopPropagation();
  402. clearTimeout(activeData.timer);
  403. }}>
  404. <div
  405. class={[styles.fullBtn, styles.point]}
  406. onClick={() => (popupData.open = true)}>
  407. <img src={iconMenu} />
  408. <span>课件</span>
  409. </div>
  410. <div
  411. class={[
  412. styles.fullBtn,
  413. popupData.activeIndex == 0 && styles.btnsDisabled
  414. ]}
  415. onClick={() => handlePreAndNext('up')}>
  416. <img src={iconUp} />
  417. <span style={{ textAlign: 'center' }}>上一个</span>
  418. </div>
  419. <div
  420. class={[
  421. styles.fullBtn,
  422. popupData.activeIndex == data.itemList.length - 1 &&
  423. styles.btnsDisabled
  424. ]}
  425. onClick={() => handlePreAndNext('down')}>
  426. <span style={{ textAlign: 'center' }}>下一个</span>
  427. <img src={iconDown} />
  428. </div>
  429. </div>
  430. )}
  431. </Transition>
  432. </div>
  433. </div>
  434. <div
  435. style={{ transform: activeData.model ? '' : 'translateY(-100%)' }}
  436. class={styles.headerContainer}
  437. ref={headeRef}>
  438. <div class={styles.backBtn} onClick={() => goback()}>
  439. <Icon name={iconBack} />
  440. 返回
  441. </div>
  442. <div class={styles.menu}>{popupData.itemName}</div>
  443. </div>
  444. <Popup
  445. class={styles.popup}
  446. style={{ background: 'rgba(0,0,0, 0.75)' }}
  447. overlayClass={styles.overlayClass}
  448. position="right"
  449. round
  450. v-model:show={popupData.open}
  451. onClose={handleClosePopup}>
  452. <Points
  453. data={data.knowledgePointList}
  454. itemActive={popupData.itemActive}
  455. onHandleSelect={(res: any) => {
  456. popupData.open = false;
  457. toggleMaterial(res.itemActive);
  458. }}
  459. />
  460. </Popup>
  461. </div>
  462. );
  463. }
  464. });