这是做项目中写的一个小Demo(uniapp===>vue2),各位可以参考借用。来给自己的小程序增加一份收入~话不多说直接上代码。
使用之前请先用 wx.getSystemInfoSync() 检查返回对象的 SDKVersion 判断基础库版本号后再使用该 API(小游戏端要求 >= 2.0.4, 小程序端要求 >= 2.6.0)。
data(){
return{
videoAd:"",
}
},
methods:{
advert() {
this.videoAd = wx.createRewardedVideoAd({
adUnitId: "" // 申请激励广告单元ID
});
this.videoAd.onError(err => {
console.log("err", err)
})
this.videoAd.load()
.then(() => this.videoAd.show())
.catch(err => {
console.log("err", err)
this.videoAd.load()
.then(() => this.videoAd.show())
})
this.videoAd.onClose(res => {
console.log("res", res);
// 用户点击了【关闭广告】按钮
if (res && res.isEnded || res === undefined) {
// 广告正常播放结束,业务逻辑
}
else {
// 播放中途退出
}
})
}},