import { httpRequest } from "../../utils/httpReques"; const { getPlayletManager }: any = tt; Component({ data: { adBanner: null, videoAlbumId: null, videoEpisodeId: null, lockStatus: false, hasSetBackRecommend: false, hasSetRecommend: false, pm: null, adCountdown: 0, timerAd: null }, inject: ["adCountdown", "adCountdownShowAd"], observers: { // 点击组件 A 的 button 后,触发该监听器的执行 adCountdown(val) { console.log(val, "5555555adCountdownadCountdown") this.setData({ adCountdown: val, }) }, adCountdownShowAd(val) { console.log(val, "adCountdownShowAd_adCountdownShowAd") if (val) { this.adOpen(); } } }, ready() { const pm = getPlayletManager(); pm.onPlay(() => { console.log("触发开始播放onPlay回调,时间是:", new Date()); // this.setData({ // lockStatus: false // }) }); pm.onPause((e: any) => { console.log('触发暂停播放onPause回调', e) }) pm.onEnded((e: any) => { console.log('触发播放到末尾onEnded回调', e) }) pm.onError((e: any) => { console.log('触发onError回调', e) }) // 播放进度变化时 pm.onTimeUpdate((e: any) => { this.playTimeListener(e); // 播放进度变化时触发,返回当前播放时间点及视频总时长,单位:秒(s)。event.detail = { currentTime, duration }。 }) pm.onChangeEpisode((e: any) => { 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; 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: any) => { console.log('shareSuccess-uuuu', res); }) pm.onShareFail((err: any) => { console.log('shareFail-uuuu', err); }) pm.onClickUnlock(() => { console.log('aaa') this.adOpen() }) this.setData({ pm: pm }) 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("视频广告展示"); }); }, hadelAdOpen() { this.emitInfoToParent('adCountdownChange') this.adOpen(); }, openPay() { const pm: any = this.data.pm; this.emitInfoToParent('adCountdownChange') pm.toggleCustomDialog() }, //播放进度条监听 playTimeListener(e: any) { const pm: any = this.data.pm; // console.log('eee',e); // 返回推荐-触发 if (e.currentTime >= 10 && !this.data.hasSetBackRecommend && tt.canIUse('back-recommend')) { console.log('backReCommend') 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: any = { entryType: 1, switchStatus: true, } if (albumId) { configItem = { ...configItem, data: { albumId: albumId, seq: 1 } } } this.pm.setRecommendConfig(configItem); } } }, //原来的拼剧接口 setNextPart(currentVideo: any) { return new Promise((resolve: any) => { httpRequest('/getNextPart', 'post', { album_id: currentVideo.album_id, episode_id: currentVideo.episode_id }).then((res: any) => { if (res.album_id) { resolve(res.album_id) } resolve() }).catch(() => { 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; }, }, detached() { const is_timerAd = this.data.timerAd; if (is_timerAd) { clearInterval(is_timerAd); } } })