import { unlockChapter } from '../../api/api.js' import { apple } from '../../config/config' const novelPlugin = requirePlugin('novel-plugin') Component({ properties: { novelManagerId: { type: Number, value: -1, }, bookId: { type: String, value: '', }, chapterIndex: { type: Number, value: -1, }, chapterId: { type: String, value: '', }, originalId: { type: String, value: '', }, }, data: { countdownNum: 3, isCounting: false }, created() { this.rewardVideoLoad() }, ready() { setTimeout(() => { this.startCountdown() }, 500) }, detached() { this.stopCountdown(); // 页面卸载时清除定时器 }, methods: { unlock() { this.stopCountdown(); this.rewardVideoPlayFn() }, // 激励视频加载 rewardVideoLoad() { if (wx.createRewardedVideoAd) { let adId = apple.ads.rewardAdId this.videoAd = wx.createRewardedVideoAd({ adUnitId: adId }) this.videoAd.offClose() this.videoAd.onLoad(() => { console.log('激励加载成功') }) this.videoAd.onError((err) => { console.log('激励失败', err) }) this.videoAd.onClose((res) => { const novelManager = novelPlugin.getNovelManager(this.properties.novelManagerId) if (res && res.isEnded) { // for (let index = 0; index < getApp().globalData.unlockChapterNum; index++) { // } let params = { chapterNo: this.properties.chapterIndex, novelId: getApp().globalData.novelId, type: 1 } unlockChapter(params).then(res => { console.log(res) novelManager.paymentCompleted() console.log('章节解锁成功', this.properties.chapterIndex) wx.showToast({ title: '解锁成功', icon: 'none' }) }) this.videoAd.offClose() } else { novelManager.closeChargeDialog() console.log('关闭解锁组件') wx.showToast({ title: '广告解锁失败,请重试', icon: 'none' }) } }) } }, // 激励视频展示 rewardVideoPlayFn() { wx.showLoading({ title: '解锁视频中', }) this.videoAd.show().then(() => { console.log('激励视频播放成功') }).catch((err) => { console.log('激励视频播放失败', err) wx.showToast({ title: '观看人数太多,请稍后再试', icon: 'none' }) }).then(() => { wx.hideLoading() }) }, startCountdown() { this.setData({ isCounting: true }); let seconds = 3; this.countdownTimer = setInterval(() => { seconds--; this.setData({ countdownNum: seconds }); if (seconds <= 0) { this.stopCountdown(); this.onCountdownEnd(); } }, 1000); }, stopCountdown() { clearInterval(this.countdownTimer); this.setData({ isCounting: false }); }, onCountdownEnd() { this.rewardVideoPlayFn() } }, })