import { defineComponent, PropType } from 'vue' import styles from './index.module.less' import Plyr from 'plyr' import 'plyr/dist/plyr.css' import { Button, Icon, Loading, Toast } from 'vant' import TCPlayer from 'tcplayer.js' import 'tcplayer.js/dist/tcplayer.css' import iconVideoPlay from '@/common/images/icon_video_play.png' import { browser } from '@/helpers/utils' import { state } from '@/state' import { listenerMessage, postMessage } from '@/helpers/native-message' export default defineComponent({ name: 'col-video', props: { trySee: { // 是否试看 type: Boolean, default: false }, freeTitleStatus: { type: Boolean, default: true }, // 试看比例 freeRate: { type: Number, default: 100 }, setting: { type: Object, default: () => {} }, controls: Boolean, height: String, src: { type: String, default: '' }, poster: { type: String, default: '' }, styleValue: { type: Object, default: () => ({}) }, preload: { type: String as PropType<'auto' | 'metadata' | 'none'>, default: 'auto' }, currentTime: { type: Boolean, default: true }, playsinline: { type: Boolean, default: true }, onPlay: { type: Function, default: () => {} }, isBuy: { // 是否把购买方法在外部调用 type: Boolean, default: false }, onBuyEmit: { type: Function, default: () => {} } }, data() { return { videoID: 'video' + Date.now() + Math.floor(Math.random() * 100), player: null as any, // playTime: 0, loading: true, // 首次进入加载中 trySeeOver: false, // 试看是否结束 showSeeStatus: true // 是否显示试看状态 } }, mounted() { this._init() listenerMessage('setVideoPlayer', result => { const content = result?.content if (content.status === 'pause') { this.player.pause() } }) }, beforeUnmount() { postMessage({ api: 'limitScreenRecord', content: { type: 0 } }) }, computed: { computedSeeStatus() { // console.log( // this.showSeeStatus, // this.trySee, // this.trySeeOver, // 'this.showSeeStatus, this.trySee' // ) return this.showSeeStatus && this.trySee }, playTime() { // 允许播放时间 const player = this.player const playTime = (player.duration() * this.freeRate) / 100 return playTime || 0 } }, methods: { _init() { // const controls = [ // 'play-large', // 'play', // 'progress', // 'captions', // 'fullscreen' // ] // if (this.currentTime) { // controls.push('current-time') // } // const params: any = { // controls: controls, // ...this.setting, // invertTime: false // } // if (browser().iPhone) { // params.fullscreen = { // enabled: true, // fallback: 'force', // iosNative: true // } // } // this.player = new Plyr((this as any).$refs.video, params) // this.player.elements.container // ? (this.player.elements.container.style.height = this.height || '210px') // : null // if (this.preload === 'none') { // this.loading = false // } // this.player.on('loadedmetadata', () => { // this.loading = false // if (this.trySee) { // this.domPlayVisibility() // } else { // this.domPlayVisibility(false) // } // // 监听播放事件 // const _this = this // this.player.on('timeupdate', () => { // const players = _this.player // if (players.currentTime >= this.playTime && _this.trySee) { // players.pause() // _this.trySeeOver = true // _this.showSeeStatus = true // _this.domPlayVisibility() // 试看结束后隐藏播放按钮 // } // }) // }) // this.player.on('play', () => { // postMessage( // { // api: 'getDeviceStatus', // content: { // type: 'video' // } // }, // (res: any) => { // // 判断是否在录屏中, 如果在录屏则不允许播放 // if (res.content.status == '1') { // Toast('为了保证数据安全,请不要录屏') // this.player.pause() // } // } // ) // postMessage({ // api: 'limitScreenRecord', // content: { // type: 1 // } // }) // this.onPlay && this.onPlay() // }) // this.player.on('enterfullscreen', () => { // console.log('fullscreen') // const i = document.createElement('i') // i.id = 'fullscreen-back' // i.className = 'van-icon van-icon-arrow-left video-back' // i.addEventListener('click', () => { // this.player.fullscreen.exit() // }) // console.log(document.getElementsByClassName('plyr')) // document.getElementsByClassName('plyr')[0].appendChild(i) // }) // this.player.on('exitfullscreen', () => { // console.log('exitfullscreen') // const i = document.getElementById('fullscreen-back') // i && i.remove() // }) const Button = TCPlayer.getComponent('Button') const BigPlayButton = TCPlayer.getComponent('BigPlayButton') BigPlayButton.prototype.createEl = function () { const el = Button.prototype.createEl.call(this) const _html = '' el.appendChild( TCPlayer.dom.createEl('div', { className: 'vjs-button-icon', innerHTML: _html }) ) return el } this.player = TCPlayer(this.videoID, { appID: '', controls: true }) // player-container-id 为播放器容器 ID,必须与 html 中一致 if (this.player) { this.player.src(this.src) // url 播放地址 this.player.poster(this.poster || '') if (this.preload === 'none') { this.loading = false } this.player.on('loadstart', () => { this.loading = false if (this.trySee) { this.domPlayVisibility() } else { this.domPlayVisibility(false) } // 监听播放事件 this.player.on('timeupdate', () => { const players = this.player if (players.currentTime() >= this.playTime && this.trySee) { players.pause() this.trySeeOver = true this.showSeeStatus = true this.domPlayVisibility() // 试看结束后隐藏播放按钮 } }) }) this.player.on('play', () => { postMessage( { api: 'getDeviceStatus', content: { type: 'video' } }, (res: any) => { // 判断是否在录屏中, 如果在录屏则不允许播放 if (res.content.status == '1') { Toast('为了保证数据安全,请不要录屏') this.player.pause() } } ) postMessage({ api: 'limitScreenRecord', content: { type: 1 } }) this.onPlay && this.onPlay() }) this.player.on('fullscreenchange', () => { if (this.player.isFullscreen()) { console.log('fullscreen') const i = document.createElement('i') i.id = 'fullscreen-back' i.className = 'van-icon van-icon-arrow-left video-back' i.addEventListener('click', () => { this.player.exitFullscreen() }) // console.log(document.getElementsByClassName('video-js')) document.getElementsByClassName('video-js')[0].appendChild(i) } else { console.log('exitfullscreen') const i = document.getElementById('fullscreen-back') i && i.remove() } }) } }, // 操作功能 domPlayVisibility(hide = true) { const controls = document.querySelector('.vjs-big-play-button') const controls2 = document.querySelector('.vjs-control-bar') if (hide) { controls?.setAttribute('style', 'display:none') controls2?.setAttribute('style', 'display:none') } else { controls?.removeAttribute('style') setTimeout(() => { controls2?.removeAttribute('style') }, 200) } }, onClickPlay() { this.player.play() this.domPlayVisibility(false) this.showSeeStatus = false }, onBuy() { if (this.isBuy) { this.onBuyEmit() return } this.$router.back() }, onReplay() { this.player.currentTime(0) this.player.play() this.domPlayVisibility(false) this.trySeeOver = false this.showSeeStatus = false } }, unmounted() { this.player?.pause() this.player?.src('') this.player?.dispose() }, render() { return (
免费试看 {/* {this.freeTitleStatus ? '试看' : '领取'} */}
{/*每课时可试看{this.freeRate}%
*/} > ) : ( <> {state.platformType === 'STUDENT' ? ({this.freeTitleStatus ? '免费试看结束,购买完整课程后继续学习' : '试看结束,领取课程后继续学习'}
) : (若需完整观看,请下载酷乐秀领取或购买
)} {state.platformType === 'STUDENT' && ( )}