wx-xcx ad.js
var interstitialAd = null;
module.exports = {
// 激勵視頻廣告
showRewardVideoAd(adId, closeEnd, closeNotEnd) {
let pages = getCurrentPages()
let currentPage = pages[pages.length - 1]
let rewardVideoAd = currentPage[adId]
if (!rewardVideoAd) {
// 初始化視頻組件
rewardVideoAd = wx.createRewardedVideoAd({
adUnitId: adId,
multiton: true
})
currentPage[adId] = rewardVideoAd
}
// rewardVideoAd.offLoad()
rewardVideoAd.offError()
rewardVideoAd.offClose()
// rewardVideoAd.onLoad(() => { console.log('on load') })
rewardVideoAd.onError((err) => {
console.error(err)
uni.showToast({
title: '正在加載,請稍后再試',
icon: 'none'
})
})
rewardVideoAd.onClose((status) => {
// 基礎庫2.1.0以前的版本兼容,status是一個undefined
if (status && status.isEnded || status === undefined) {
// 正常播放結束,發放獎勵
closeEnd ? closeEnd() : void(0)
} else {
// 未完成播放主動關閉
closeNotEnd ? closeNotEnd() : void(0)
}
})
uni.showToast({
title: '獲取中',
icon: "loading",
mask: true
})
// 展示視頻組件
rewardVideoAd.show().catch(() => {
// 失敗重試
rewardVideoAd.load().then(() => rewardVideoAd.show()).catch(err => {
uni.showToast({
title: '激勵視頻 廣告顯示失敗',
icon: 'none'
})
})
})
},
// 插屏廣告
load(id) {
if (uni.createInterstitialAd) {
interstitialAd = uni.createInterstitialAd({
adUnitId: id
})
interstitialAd.onLoad(() => {
console.log('插屏廣告加載中')
})
interstitialAd.onError((err) => {
console.log('加載錯誤', err)
})
interstitialAd.onClose((res) => {
console.log('插屏廣告關閉', res)
})
}
},
show() {
if (interstitialAd) {
interstitialAd.show().catch((err) => {
console.error(err)
})
}
}
}

浙公網安備 33010602011771號