方法一
<template>
<view>
<button @click="elan()">看广告</button>
</view>
</template>
<script>
export default {
data() {
return {
videoAd: null, // 激励视频广告实例
}
},
onLoad() {
// #ifdef MP-WEIXIN
if (wx.createRewardedVideoAd) {
this.videoAd = wx.createRewardedVideoAd({
adUnitId: 'adunit-111111111111' // 替换为你的微信小程序广告单元ID
});
this.videoAd.onLoad(() => {
console.log('激励视频广告加载成功');
// 可以在这里显示加载成功的提示给用户
});
this.videoAd.onError((err) => {
console.error('激励视频广告加载失败', err);
// 可以在这里显示加载失败的提示给用户
});
this.videoAd.onClose((res) => {
console.log('激励视频广告关闭', res);
if (res.isEnded) {
uni.showToast({
icon: 'none',
title: ' 任务完成 '
})
this.jihui(1);
} else {
uni.showToast({
icon: 'none',
title: ' 请完整看完广告 '
})
}
// 可以在这里处理用户看完广告后的逻辑,比如给用户奖励
});
// 尝试预加载广告(可选)
this.videoAd.load().catch((err) => {
console.error('激励视频广告预加载失败', err);
});
} else {
console.error('当前微信版本不支持激励视频广告');
}
// #endif
},
methods: {
// 点击时间
elan() {
// #ifdef MP-WEIXIN
if (this.videoAd) {
this.videoAd.show().catch(() => {
// 如果广告未准备好,可以稍后再试或者给用户提示
uni.showToast({
title: '广告未准备好,请稍后再试',
icon: 'none'
});
});
} else {
console.error('激励视频广告实例未初始化');
}
// #endif
},
}
}
</script>
<style>
</style>
方法二
// 点击事件
adv() {
let ad = null;
if (wx.createRewardedVideoAd) {
ad = wx.createRewardedVideoAd({
adUnitId:''
})
ad.onLoad(() => {
console.log('onLoad')
})
ad.onError((err) => {
console.log('onError')
})
ad.onClose((res) => {
if (res && res.isEnded) {
console.log('用户看完了---关闭窗口继续播放电影')
} else {
console.log('没看完就关闭了')
}
})}
if (ad) {
ad.show().catch(() => {
// 失败重试
ad.load().then(() => ad.show().catch(err => {
uni.hideLoading();
})
})
}
},