import {httpRequest} from "../../utils/httpReques"; const { getPlayletManager } = tt; Component({ pm: undefined, data: { adBanner: null, videoAlbumId: null, videoEpisodeId: null, lockStatus: false, hasSetBackRecommend: false, hasSetRecommend: false, }, ready() { const pm = getPlayletManager(); this.pm = pm; pm.onPlay((e) => { console.log("触发开始播放onPlay回调,时间是:", new Date()); this.setData({ lockStatus: false }) }); pm.onPause((e) => { console.log('触发暂停播放onPause回调', e) }) pm.onEnded((e) => { console.log('触发播放到末尾onEnded回调', e) }) pm.onError((e) => { console.log('触发onError回调', e) }) // 播放进度变化时 pm.onTimeUpdate((e) => { this.playTimeListener(e); // 播放进度变化时触发,返回当前播放时间点及视频总时长,单位:秒(s)。event.detail = { currentTime, duration }。 }) pm.onChangeEpisode((e) => { console.log('切换回调', e) //当前切换到的集 tt.setStorageSync('currentvideo', {'album_id': e.albumId,episode_id:e.episodeId, seq:e.seq,status: e.status, scene: e.scene}); //当前切换到的解锁集,播放记录用 if(e.status=='free'){ tt.setStorageSync('lastfreevideo', {'album_id': e.albumId,episode_id:e.episodeId, seq:e.seq,status: e.status, scene: e.scene}); } let lock = false; // let watchInfo = tt.getStorageSync('watchInfo') // if(e.scene =='first_play' && watchInfo?.album_id != e.albumId){ // watchInfo = tt.getStorageSync('watchInfo') // console.log('1') // } // const checklock = watchInfo?.isvip != 0 && (watchInfo?.chackpay == 2 || watchInfo?.chackpay == 3) // console.log(watchInfo) // console.log(checklock) // if(e.status =='lock' || (e.scene =='first_play' && checklock)){ if(e.status =='lock'){ lock = true; } //更新剧集信息 this.setData({ lockStatus: lock, videoAlbumId: e.albumId, videoEpisodeId: e.episodeId, }) // 在切换到锁定的集 或者 切换了剧(官方完播推荐) 之后,需要刷新数据 const watchInfo = tt.getStorageSync('watchInfo') if(e.status =='lock' || e.album_id != watchInfo.album_id){ this.emitInfoToParent('change',{album_id: this.data.videoAlbumId,episode_id:this.data.videoEpisodeId}) } }) pm.onShareSuccess((res) => { console.log('shareSuccess-uuuu', res); }) pm.onShareFail((res) => { console.log('shareFail-uuuu', err); }) pm.onClickUnlock((e) => { console.log('aaa') this.adOpen() }) this.adLoad() }, methods: { //向父组件传值 emitInfoToParent(val:string,data?:any) { const instance = this.selectOwnerComponent(); instance.emitInfoFromChildren(val,data); }, adLoad() { let isAdBanner: any = this.data.adBanner; // const context: any = tt.createVideoContext(`${videoInfo.id}`); // this.watchInfo.isvip = 0; if (!isAdBanner) { const adBanner: any = tt.createRewardedVideoAd({ adUnitId: "wh0y4vcxsbk7ixwktg", }); // 监听错误 adBanner.onError((err: any) => { tt.hideLoading({}); switch (err.errCode) { case 1004: // 无合适的广告 break; default: // 更多请参考错误码文档 } }); // 监听视频播放完成 adBanner.onClose((res: any) => { console.log(res, "resres") if (res.isEnded) { this.emitInfoToParent('advunlock',{album_id: this.data.videoAlbumId,episode_id:this.data.videoEpisodeId}) this.setData({ lockStatus: false }) } else { tt.showToast({ title: '看完广告方可解锁', icon: 'none', duration: 2000 }) } }); // 预加载资源 adBanner.load(); this.setData({ adBanner: adBanner, }) } }, adOpen() { let adBanner: any = this.data.adBanner; tt.showToast({ title: `广告加载中...`, icon: 'none', }); adBanner.show().then(() => { // res: any console.log("视频广告展示"); }); }, openPay() { this.pm.toggleCustomDialog() }, //播放进度条监听 playTimeListener(e) { // console.log('eee',e); // 返回推荐-触发 if (e.currentTime >= 10 && !this.data.hasSetBackRecommend && tt.canIUse('back-recommend')) { console.log('backReCommend') this.pm.setRecommendConfig({ entryType: 2, switchStatus: true, }); this.setData({ hasSetBackRecommend: true }) } // 完播推荐-触发 if (e.duration - e.currentTime <= 10 && !this.data.hasSetRecommend) { console.log('reCommend') this.reCommend(); this.setData({ hasSetRecommend: true }) } }, //完播推荐 async reCommend(){ const chapterList = tt.getStorageSync('chapterList'); const currentVideo = tt.getStorageSync('currentvideo'); if(!!chapterList && !!currentVideo) { const total = this.countChapterListTotal(); if (currentVideo.seq === total) { let albumId = await this.setNextPart(currentVideo) // albumId = "7446342228814856730" let configItem = { entryType: 1, switchStatus: true, } if(albumId){ configItem = { ...configItem, data: { albumId: albumId, seq: 1 } } } this.pm.setRecommendConfig(configItem); } } }, //原来的拼剧接口 setNextPart(currentVideo){ return new Promise(resolve=>{ httpRequest('/getNextPart', 'post', { album_id: currentVideo.album_id, episode_id: currentVideo.episode_id }).then((res) => { if (res.album_id) { resolve(res.album_id) } resolve() }).catch(e=>{ resolve() }) }) }, //计算总集数 countChapterListTotal() { let total = 0; const chapterList = tt.getStorageSync('chapterList'); if(!chapterList){ return total; } let dataList = [...chapterList.freeList,...chapterList.lockList] dataList.sort((a, b) => {return +b.end_episode_no - +a.end_episode_no}) if(dataList.length>0){ total = dataList[0].end_episode_no } return total; }, }, })