import { Button, closeToast, Icon, Popup, showToast, Slider, Swipe, SwipeItem, Tab, Tabs } from 'vant' import { defineComponent, onMounted, reactive, nextTick, onUnmounted, ref, watch, Transition, computed } from 'vue' import iconBack from './image/back.svg' import styles from './index.module.less' import Plyr from 'plyr' import 'plyr/dist/plyr.css' import request from '@/helpers/request' import { state } from '@/state' import { useRoute } from 'vue-router' import { listenerMessage, postMessage, promisefiyPostMessage } from '@/helpers/native-message' import MusicScore from './component/musicScore' import iconMenu from './image/icon-menu.svg' import iconDian from './image/icon-dian.svg' import iconPoint from './image/icon-point.svg' import iconLoop from './image/icon-loop.svg' import iconLoopActive from './image/icon-loop-active.svg' import iconplay from './image/icon-play.svg' import iconpause from './image/icon-pause.svg' import iconUp from './image/icon-up.svg' import iconDown from './image/icon-down.svg' import Points from './component/points' import { browser, getSecondRPM } from '@/helpers/utils' import { useRect } from '@vant/use' export default defineComponent({ name: 'CoursewarePlay', setup() { const handleInit = (type = 0) => { // 横屏 postMessage({ api: 'setRequestedOrientation', content: { orientation: type } }) // 头,包括返回箭头 postMessage({ api: 'setTitleBarVisibility', content: { status: type } }) // 安卓的状态栏 postMessage({ api: 'setStatusBarVisibility', content: { isVisibility: type } }) } handleInit() onUnmounted(() => { handleInit(1) window.removeEventListener('message', iframeHandle) }) const route = useRoute() const headeRef = ref() const data = reactive({ detail: null, knowledgePointList: [] as any, itemList: [] as any, showHead: true }) const activeData = reactive({ nowTime: 0, model: true, // 遮罩 videoBtns: true, // 视频 currentTime: 0, duration: 0, timer: null as any, item: null as any }) watch( () => activeData.model, () => { const videoItem = data.itemList.find((n) => n.id === popupData.itemActive) // 阴影切换的时候,具体去切换某个视频的控件 if (videoItem && videoItem.type === 'VIDEO') { videoItem.playModel = activeData.model } } ) // 获取缓存路径 const getCacheFilePath = async (material: any) => { const res = await promisefiyPostMessage({ api: 'getCourseFilePath', content: { url: material.content, localPath: '', materialId: material.id, updateTime: material.updateTime, type: material.type // SONG VIDEO IMAGE } }) console.log('缓存路径返回', res) return res } const getItemList = async () => { const list: any = [] const browserInfo = browser() for (let i = 0; i < data.knowledgePointList.length; i++) { const item = data.knowledgePointList[i] for (let j = 0; j < item.materialList.length; j++) { const material = item.materialList[j] if (browserInfo.isApp && ['VIDEO', 'IMG'].includes(material.type)){ const localData = await getCacheFilePath(material) if (localData?.content?.localPath){ material.url = material.content material.content = localData.content.localPath // console.log("🚀 ~ material", material) } } if (popupData.itemActive === '') { popupData.tabName = item.name popupData.tabActive = material.knowledgePointId popupData.itemActive = material.id popupData.itemName = material.name popupData.activeIndex = 0 } let videoItem = {} if (material.type === 'VIDEO') { videoItem = { currentTime: 0, duration: 100, paused: true, loop: false, videoEle: null, timer: null, playModel: true } } list.push({ ...material, ...videoItem, iframeRef: null }) } } console.log('🚀 ~ list', list) data.itemList = list } const getDetail = async () => { try { const res: any = await request.get( state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}` ) if (Array.isArray(res?.data)) { data.detail = res.data } if (Array.isArray(res?.data?.knowledgePointList)) { data.knowledgePointList = res.data.knowledgePointList.map((n: any) => { n.index = 0 return n }) getItemList() } } catch (error) {} } // ifram事件处理 const iframeHandle = (ev: MessageEvent) => { // console.log(ev.data) if (ev.data?.api === 'headerTogge') { activeData.model = ev.data.show } } onMounted(() => { getDetail() window.addEventListener('message', iframeHandle) }) // 返回 const goback = () => { // history.go(-1) postMessage({ api: 'back' }) } const swipeRef = ref() const popupData = reactive({ open: false, activeIndex: -1, tabActive: '', tabName: '', itemActive: '', itemName: '' }) // 设置当前的激活状态 const setActiveData = (val: any, oldVal: any) => { handleStopVideo() handleStopMusicScore() } watch(() => popupData.activeIndex, setActiveData) // 停止所有的播放 const handleStopVideo = () => { data.itemList.forEach((m: any) => { m.videoEle?.pause() }) } // 停止曲谱的播放 const handleStopMusicScore = () => { data.itemList.forEach((m: any) => { m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*') }) } // 获取name const setAllName = () => { const item = data.itemList.find((n: any) => n.id == popupData.itemActive) const tab = data.knowledgePointList.find((n: any) => n.id == popupData.tabActive) if (item) { popupData.itemName = item.name } if (tab) { popupData.tabName = tab.name } } // 切换素材 const toggleMaterial = () => { const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive) if (index > -1) { popupData.activeIndex = index swipeRef.value?.swipeTo(index) setAllName() } } // 轮播切换 const handleSwipeChange = (val: any) => { popupData.activeIndex = val const item = data.itemList[val] if (item) { popupData.tabActive = item.knowledgePointId popupData.itemActive = item.id setAllName() } } // 上一个知识点, 下一个知识点 const handlePreAndNext = (type: string) => { if (type === 'up') { swipeRef.value?.prev() } else { swipeRef.value?.next() } } // 去点名,签退 const gotoRollCall = (pageTag: string) => { postMessage({ api: 'open_app_page', content: { action: 'app', pageTag: pageTag, url: '', params: JSON.stringify({ courseId: route.query.courseId }) } }) } // 双击 const handleDbClick = (item: any) => { // console.log(activeData.item) if (item && item.type === 'VIDEO') { const videoEle: HTMLVideoElement = document.querySelector(`[data-vid='${item.id}']`)! if (videoEle) { if (videoEle.paused) { closeToast() videoEle.play() } else { showToast('已暂停') videoEle.pause() } } item.timer = setTimeout(() => { activeData.model = false }, 3000) console.dir(videoEle) } } // 调整播放进度 const handleChangeSlider = (val: any, m: any) => { if (m?.videoEle) { m.videoEle.currentTime = val } } return () => (