123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- 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/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 iconVideobg from './image/icon-videobg.png'
- 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,
- isCourse: false
- })
- const activeData = reactive({
- nowTime: 0,
- model: true, // 遮罩
- videoBtns: true, // 视频
- currentTime: 0,
- duration: 0,
- timer: null as any,
- item: null as any
- })
- // 获取缓存路径
- 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 getCourseSchedule = async () => {
- if (!route.query.courseId) return
- try {
- const res = await request.get(
- `${state.platformApi}/courseSchedule/detail/${route.query.courseId}`
- )
- if (res?.data) {
- data.isCourse = res.data.status === 'COMPLETE' ? false : true
- }
- } catch (e) {
- console.log(e)
- }
- }
- const getItemList = async () => {
- const list: any = []
- const browserInfo = browser()
- for (let i = 0; i < data.knowledgePointList.length; i++) {
- const item = data.knowledgePointList[i]
- const itemLength = item.materialList.length - 1
- 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)
- }
- }
- let videoItem = {}
- if (material.type === 'VIDEO') {
- videoItem = {
- currentTime: 0,
- duration: 0,
- progress: 0,
- paused: true,
- loop: false,
- videoEle: null,
- timer: null,
- playModel: false,
- isprepare: false,
- isDrage: false,
- muted: false // 是否静音
- }
- }
- list.push({
- ...material,
- ...videoItem,
- iframeRef: null,
- tabName: item.name,
- isLast: j === itemLength, // 当前知识点
- autoPlay: false //加载完成是否自动播放
- })
- }
- }
- let item: any = null
- if (route.query.kId) {
- item = list.find((n: any) => n.materialId == route.query.kId)
- const _firstIndex = list.findIndex((n: any) => n.materialId == route.query.kId)
- popupData.firstIndex = _firstIndex > -1 ? _firstIndex : 0
- }
- item = item ? item : list[0] || {}
- if (item) {
- popupData.tabName = item.tabName
- popupData.tabActive = item.knowledgePointId
- popupData.itemActive = item.id
- popupData.itemName = item.name
- popupData.activeIndex = popupData.firstIndex
- item.autoPlay = true
- item.muted = true
- }
- 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)) {
- let index = 0
- data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
- if (Array.isArray(n.materialList)) {
- n.materialList = n.materialList.map((item: any) => {
- index++
- return {
- ...item,
- materialId: item.id,
- id: index + ''
- }
- })
- }
- return n
- })
- getItemList()
- }
- } catch (error) {}
- }
- // ifram事件处理
- const iframeHandle = (ev: MessageEvent) => {
- // console.log(ev.data)
- if (ev.data?.api === 'headerTogge') {
- // activeData.model = ev.data.show
- activeData.model = !activeData.model
- }
- }
- onMounted(() => {
- getDetail()
- getCourseSchedule()
- window.addEventListener('message', iframeHandle)
- })
- // 返回
- const goback = () => {
- postMessage({ api: 'goBack' })
- }
- const swipeRef = ref()
- const popupData = reactive({
- firstIndex: 0,
- 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) => {
- const item = data.itemList[popupData.activeIndex]
- if (item?.id != m.id){
- m.autoPlay = false
- m.videoEle?.pause()
- }
- })
- }
- // 停止曲谱的播放
- const handleStopMusicScore = () => {
- data.itemList.forEach((m: any) => {
- m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*')
- })
- }
- // 切换素材
- const toggleMaterial = () => {
- const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive)
- if (index > -1) {
- popupData.activeIndex = index
- swipeRef.value?.swipeTo(index)
- }
- }
- /** 延迟收起模态框 */
- const setModelOpen = () => {
- clearTimeout(activeData.timer)
- closeToast()
- activeData.timer = setTimeout(() => {
- activeData.model = false
- }, 4000)
- }
- // 轮播切换
- const handleSwipeChange = (val: any) => {
- console.log('轮播切换')
- popupData.activeIndex = val
- const item = data.itemList[val]
- if (item) {
- popupData.tabActive = item.knowledgePointId
- popupData.itemActive = item.id
- popupData.itemName = item.name
- popupData.tabName = item.tabName
- if (item.type == 'SONG') {
- activeData.model = true
- }
- if (item.type === 'VIDEO') {
- // console.log("🚀 ~ item", item)
- // 自动播放下一个视频
- clearTimeout(activeData.timer)
- closeToast()
- item.currentTime = 0
- item.videoEle && (item.videoEle.currentTime = 0)
- item.autoPlay = true
- }
- }
- }
- // 上一个知识点, 下一个知识点
- 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(item)
- if (item && item.type === 'VIDEO') {
- const videoEle: HTMLVideoElement = item.videoEle
- if (videoEle) {
- if (videoEle.paused) {
- closeToast()
- videoEle.play()
- } else {
- showToast('已暂停')
- videoEle.pause()
- }
- }
- }
- }
- // 暂停播放
- const handlePaused = (e: Event, m: any) => {
- e.stopPropagation()
- m.videoEle?.pause()
- m.paused = true
- }
- // 开始播放
- const handlePlay = (e: Event, m: any) => {
- e.stopPropagation()
- closeToast()
- m.videoEle?.play()
- }
- // 调整播放进度
- const handleChangeSlider = (m: any) => {
- if (m?.videoEle) {
- // console.log('进度条', m.progress)
- m.currentTime = m.duration * (m.progress / 100)
- m.videoEle.currentTime = m.currentTime
- }
- }
- //当前视频播放完
- const handleEnded = (m: any) => {
- // console.log(m)
- if (popupData.activeIndex != data.itemList.length - 1) {
- swipeRef.value?.next()
- }
- }
- return () => (
- <div class={styles.coursewarePlay}>
- <Swipe
- style={{ height: '100vh' }}
- ref={swipeRef}
- showIndicators={false}
- loop={false}
- vertical
- lazyRender={true}
- touchable={false}
- initialSwipe={popupData.firstIndex}
- onChange={handleSwipeChange}
- >
- {data.itemList.map((m: any, mIndex: number) => {
- return (
- <SwipeItem class={styles.swipeItem}>
- <>
- <div
- class={styles.itemDiv}
- onClick={() => {
- clearTimeout(activeData.timer)
- if (Date.now() - activeData.nowTime < 300) {
- handleDbClick(m)
- return
- }
- activeData.nowTime = Date.now()
- activeData.timer = setTimeout(() => {
- activeData.model = !activeData.model
- setModelOpen()
- }, 300)
- }}
- >
- {m.type === 'VIDEO' ? (
- <>
- <video
- playsinline="false"
- muted={m.muted}
- preload="auto"
- class="player"
- poster={iconVideobg}
- data-vid={m.id}
- src={m.content}
- loop={m.loop}
- autoplay={m.autoPlay}
- onLoadedmetadata={(e: Event) => {
- const videoEle = e.target as unknown as HTMLVideoElement
- m.currentTime = videoEle.currentTime
- m.duration = videoEle.duration
- m.videoEle = videoEle
- m.isprepare = true
- }}
- onTimeupdate={(e: Event) => {
- if (!m.isprepare) return
- const videoEle = e.target as unknown as HTMLVideoElement
- m.currentTime = videoEle.currentTime
- m.progress = Number(
- ((videoEle.currentTime / m.duration) * 100).toFixed(1)
- )
- }}
- onPlay={() => {
- // 播放
- m.paused = false
- console.log('播放')
- setModelOpen()
- m.muted = false
- }}
- onPause={() => {
- //暂停
- clearTimeout(activeData.timer)
- m.paused = true
- }}
- onEnded={() => handleEnded(m)}
- >
- <source src={m.content} type="video/mp4" />
- </video>
- <Transition name="bottom">
- {activeData.model && (
- <div class={[styles.bottomFixedContainer]}>
- <div class={styles.time}>
- <span>{getSecondRPM(m.currentTime)}</span>
- <span>{getSecondRPM(m.duration)}</span>
- </div>
- <div class={styles.slider}>
- <Slider
- style={{ display: m.isprepare ? 'block' : 'none' }}
- buttonSize={16}
- step={0.1}
- modelValue={m.progress}
- onUpdate:modelValue={(val: any) => {
- m.progress = val
- handleChangeSlider(m)
- }}
- onDragStart={(e: Event) => {
- // 开始拖动,暂停播放
- console.log('开始拖动')
- // 如果拖动之前,视频是播放状态,拖动完毕后继续播放
- if (!m.paused) {
- m.isDrage = true
- }
- handlePaused(e, m)
- }}
- onDragEnd={(e: Event) => {
- console.log('结束拖动')
- if (m.isDrage) {
- m.isDrage = false
- handlePlay(e, m)
- }
- }}
- min={0}
- max={100}
- />
- </div>
- <div class={styles.actions}>
- <div class={styles.actionBtn}>
- {m.paused ? (
- <img src={iconplay} onClick={(e: Event) => handlePlay(e, m)} />
- ) : (
- <img
- src={iconpause}
- onClick={(e: Event) => handlePaused(e, m)}
- />
- )}
- {m.loop ? (
- <img
- src={iconLoopActive}
- onClick={(e: Event) => {
- e.stopPropagation()
- m.loop = false
- }}
- />
- ) : (
- <img
- src={iconLoop}
- onClick={(e: Event) => {
- e.stopPropagation()
- m.loop = true
- }}
- />
- )}
- </div>
- <div>{m.name}</div>
- </div>
- </div>
- )}
- </Transition>
- </>
- ) : m.type === 'IMG' ? (
- <img src={m.content} />
- ) : (
- <MusicScore
- data-vid={m.id}
- music={m}
- onSetIframe={(el: any) => {
- m.iframeRef = el
- }}
- />
- )}
- </div>
- </>
- </SwipeItem>
- )
- })}
- </Swipe>
- <Transition name="top">
- {activeData.model && (
- <div class={styles.headerContainer} ref={headeRef}>
- <div class={styles.backBtn} onClick={() => goback()}>
- <Icon name={iconBack} />
- 返回
- </div>
- <div class={styles.menu}>{popupData.tabName}</div>
- </div>
- )}
- </Transition>
- <Transition name="right">
- {activeData.model && (
- <div class={styles.rightFixedBtns}>
- <div
- class={styles.fullBtn}
- onClick={() => {
- clearTimeout(activeData.timer)
- popupData.open = true
- }}
- >
- <img src={iconMenu} />
- <span>知识点</span>
- </div>
- {data.isCourse && (
- <>
- <div
- class={[styles.fullBtn, styles.point]}
- onClick={() => gotoRollCall('student_roll_call')}
- >
- <img src={iconDian} />
- <span>点名</span>
- </div>
- <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
- <img src={iconPoint} />
- <span>签退</span>
- </div>
- </>
- )}
- </div>
- )}
- </Transition>
- <Transition name="left">
- {activeData.model && (
- <div class={styles.leftFixedBtns}>
- {popupData.activeIndex != 0 && (
- <div
- class={[styles.fullBtn, styles.prePoint]}
- onClick={() => handlePreAndNext('up')}
- >
- <img src={iconUp} />
- <span style={{ textAlign: 'center' }}>上一个</span>
- </div>
- )}
- {popupData.activeIndex != data.itemList.length - 1 && (
- <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
- <span style={{ textAlign: 'center' }}>下一个</span>
- <img src={iconDown} />
- </div>
- )}
- </div>
- )}
- </Transition>
- <Popup
- class={styles.popup}
- overlayClass={styles.overlayClass}
- position="right"
- round
- v-model:show={popupData.open}
- onClose={() => {
- const item = data.itemList[popupData.activeIndex]
- if (item?.type == "VIDEO"){
- setModelOpen()
- }
- }}
- >
- <Points
- data={data.knowledgePointList}
- tabActive={popupData.tabActive}
- itemActive={popupData.itemActive}
- onHandleSelect={(res: any) => {
- // console.log(res)
- popupData.tabActive = res.tabActive
- popupData.itemActive = res.itemActive
- popupData.tabName = res.tabName
- popupData.open = false
- toggleMaterial()
- }}
- />
- </Popup>
- </div>
- )
- }
- })
|