API:wx.createRewardedVideoAd
页面代码
let rewardedVideoAd = null;
let smsRewardedVideoAd = null;
Page({
data: {},
onLoad() {
let pageObject = this;
this.setData({
showEmailAd: true,
showSmsAd: true,
})
// 广告组件
if (wx.createRewardedVideoAd) {
rewardedVideoAd = wx.createRewardedVideoAd({
adUnitId: 'adunit-09810fd3b7baa527',
multiton:true
})
rewardedVideoAd.onLoad(() => {})
rewardedVideoAd.onError((err) => {
pageObject.setData({
showEmailAd: false,
})
})
rewardedVideoAd.onClose((res) => {
// 用户点击了【关闭广告】按钮
if (res && res.isEnded) {
// 正常播放结束,可以下发游戏奖励
pageObject.addEmailRemindNum();
} else {
// 播放中途退出,不下发游戏奖励
wx.showToast({
title: '获取奖励失败',
icon: 'error',
duration: 2000
})
}
})
smsRewardedVideoAd = wx.createRewardedVideoAd({
adUnitId: 'adunit-e45cdf6582e0ed1c',
multiton:true
})
smsRewardedVideoAd.onLoad(() => {})
smsRewardedVideoAd.onError((err) => {
pageObject.setData({
showSmsAd: false,
})
})
smsRewardedVideoAd.onClose((res) => {
// 用户点击了【关闭广告】按钮
if (res && res.isEnded) {
// 正常播放结束,可以下发游戏奖励
pageObject.addSmsRemindNum();
} else {
// 播放中途退出,不下发游戏奖励
wx.showToast({
title: '获取奖励失败',
icon: 'error',
duration: 2000
})
}
})
}
},
onclickAd() {
// 用户触发广告后,显示激励视频广告
if (rewardedVideoAd) {
rewardedVideoAd.show().catch(() => {
// 失败重试
rewardedVideoAd.load()
.then(() => rewardedVideoAd.show())
.catch(err => {
pageObject.setData({
showEmailAd: false,
})
})
})
}
},
onclickSmsAd() {
// 用户触发广告后,显示激励视频广告
if (smsRewardedVideoAd) {
smsRewardedVideoAd.show().catch(() => {
// 失败重试
smsRewardedVideoAd.load()
.then(() => smsRewardedVideoAd.show())
.catch(err => {
pageObject.setData({
showSmsAd: false,
})
})
})
}
},
addEmailRemindNum() {
wx.showLoading({
title: '获取奖励中',
})
wx.request({
url: getApp().globalData.apiUrl + '/userinfo/updateEmailReminderNum?advert=true',
method: 'POST',
header: {
'content-type': 'application/json', // 默认值
'Authorization': "Bearer " + wx.getStorageSync("skey")
},
success(res) {
getApp().globalData.userInfo.emailRemindNum = res.data.data;
wx.hideLoading()
wx.showToast({
title: '获取奖励成功',
icon: 'success',
duration: 2000
})
}
});
},
addSmsRemindNum() {
wx.showLoading({
title: '获取奖励中',
})
wx.request({
url: getApp().globalData.apiUrl + '/userinfo/updateSmsReminderNum?advert=true',
method: 'POST',
header: {
'content-type': 'application/json', // 默认值
'Authorization': "Bearer " + wx.getStorageSync("skey")
},
success(res) {
getApp().globalData.userInfo.smsRemindNum = res.data.data;
wx.hideLoading()
wx.showToast({
title: '获取奖励成功',
icon: 'success',
duration: 2000
})
}
});
},
})