f-ad.vue
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<template>
<view>
<!-- #ifdef MP-WEIXIN -->
<view class="adContent">
<block v-for="(item,index) in typeArr" :key="index">
<block v-if="item===1">
<!-- banner广告 -->
<ad unit-id="adunit-ecdb31747eeb025d" ad-intervals="30"></ad>
</block>
<block v-if="item===2">
<!-- 视频广告 -->
<ad unit-id="adunit-3e70c6aaf0d18a5c" ad-type="video" ad-theme="white"></ad>
</block>
<block v-if="item===3">
<!-- 原生模板广告 -->
<ad-custom unit-id="adunit-ce4c3aa5a5a8aea3"></ad-custom>
</block>
</block>
</view>
<!-- #endif -->
</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
let rewardedVideoAd = null // 定义激励视频广告
let insertScreenVideoAd = null // 定义插屏视频广告
export default {
props:{
//1.banner广告 2.视频广告 3.原生模板广告 4.插屏视频广告 5.激励视频广告
typeArray:{
type: Array,
default: function() {
return [];
},
}
},
data() {
return {
_isExcitationLoaded:false, //激励广告是否加载好
_isInsertScreenLoaded:false,//插屏广告是否加载好
typeArr:[1]
};
},
created() {
// #ifdef MP-WEIXIN
this.typeArr = this.typeArray
this.typeArr.forEach(item=>{
if(item===5){
this.excitationAd('adunit-fb0093bcd1f2f790') //激励
}else if(item===4){
this.insertScreenAd('adunit-9d97cc3babda47e5') //插屏
}
})
// #endif
},
mounted(e) {
},
//方法
methods: {
// 激励广告
excitationAd(adUnitId){
this._isExcitationLoaded = false
// 在页面onLoad回调事件中创建激励视频广告实例
if (uni.createRewardedVideoAd) {
rewardedVideoAd = uni.createRewardedVideoAd({
adUnitId: adUnitId
})
rewardedVideoAd.onLoad(() => {
this._isExcitationLoaded = true
})
rewardedVideoAd.onError((err) => {})
rewardedVideoAd.onClose((res) => {
// 用户点击了【关闭广告】按钮
if (res && res.isEnded) {
// 正常播放结束,可以下发游戏奖励
this.$emit('excitationAdCallback',1)
} else {
// 播放中途退出,不下发游戏奖励
this.$emit('excitationAdCallback',2)
}
})
}
},
// 显示激励广告
showExcitationAd(){
if (this._isExcitationLoaded) {
rewardedVideoAd.show().catch(() => {
// 失败重试
rewardedVideoAd.load()
.then(() => rewardedVideoAd.show())
.catch(err => {
console.log('激励视频 广告显示失败')
})
})
}
},
// 插屏广告
insertScreenAd(adUnitId){
this._isInsertScreenLoaded = false
// 在页面onLoad回调事件中创建激励视频广告实例
if (uni.createInterstitialAd) {
insertScreenVideoAd = uni.createInterstitialAd({
adUnitId: adUnitId
})
insertScreenVideoAd.onLoad(() => {
this._isInsertScreenLoaded = true
console.log('插屏广告加载完')
this.showInsertScreenAd()
})
insertScreenVideoAd.onError((err) => {
console.log(err,'插屏广告拉取失败')
})
insertScreenVideoAd.onClose((res) => {
console.log('插屏广告关闭')
})
}
},
// 显示插屏广告
showInsertScreenAd(){
console.log('显示插屏广告')
if (this._isInsertScreenLoaded) {
insertScreenVideoAd.show().catch((err) => {
console.error(err,'插屏广告显示失败')
})
}
}
}
};
</script>
<style lang="scss" scoped>
.adContent{
width: 100%;
}
</style>