admyself.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view v-if="opshow2">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <view class="content" v-if="aitem.atype=='banner'">
  5. <ad :unit-id="aitem.adpid"></ad>
  6. </view>
  7. <!-- #endif -->
  8. <!-- #ifndef MP-WEIXIN -->
  9. <view class="content" v-if="opshow2&&aitem&&aitem.atype=='banner'">
  10. <ad :adpid="aitem.adpids"></ad>
  11. </view>
  12. <view class="content" v-if="opshow2&&aitem&&aitem.atype=='mvideo'">
  13. <ad-rewarded-video :adpid="aitem.adpids" :loadnext="true" v-slot:default="{loading, error}" @load="onadload"
  14. @close="onadclose" @error="onaderror">
  15. </ad-rewarded-video>
  16. </view>
  17. <view class="content" v-else-if="opshow2&&aitem&&aitem.atype=='pscreen'">
  18. <ad-interstitial :adpid="aitem.adpids" :loadnext="true" v-slot:default="{loading, error}" @load="onadload"
  19. @close="onadclose" @error="onaderror">
  20. </ad-interstitial>
  21. </view>
  22. <!-- #endif -->
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: "admyself",
  28. props: ['opshow'],
  29. data() {
  30. return {
  31. opshow2: true,
  32. aitem: {
  33. atype: 0,
  34. adpid: '',
  35. adpids: []
  36. },
  37. advlist: []
  38. };
  39. },
  40. created() {
  41. let that = this;
  42. // #ifdef MP-WEIXIN
  43. if (that.$config) {
  44. let ntime = Math.round(new Date() / 1000);
  45. // console.log('admyself-广告配置获取成功', that.$config.advlist)
  46. that.advlist = that.$config.advlist;
  47. let path = '';
  48. let pages = getCurrentPages(); // 获取当前页面栈的实例,以数组形式按栈的顺序给出,第一个元素为首页,最后一个元素为当前页面
  49. let page = pages[pages.length - 1];
  50. let adtype = page.route; // 当前的页面路由(小程序可以,H5也可以)
  51. console.log(path)
  52. if (that.advlist) {
  53. let item = that.advlist[adtype]
  54. if (item) {
  55. that.aitem = item
  56. }
  57. }
  58. // console.log('admyself-advlist', that.advlist)
  59. // console.log('admyself-adtype', adtype)
  60. // console.log('admyself-aitem', JSON.stringify(that.aitem))
  61. if (that.aitem) {
  62. if (that.aitem.atype == 'mvideo') {
  63. if (ntime - that.$adtime.mvideo_time > 15) {
  64. that.opshow2 = false;
  65. that.$adtime.mvideo_time = ntime;
  66. let videoAd = null;
  67. // 在页面onLoad回调事件中创建激励视频广告实例
  68. if (wx.createRewardedVideoAd) {
  69. videoAd = wx.createRewardedVideoAd({
  70. adUnitId: that.aitem.adpid
  71. })
  72. videoAd.onLoad(() => {})
  73. videoAd.onError((err) => {})
  74. videoAd.onClose((res) => {})
  75. }
  76. setTimeout(function() {
  77. // 用户触发广告后,显示激励视频广告
  78. if (videoAd) {
  79. console.log('激励视频 展示开始')
  80. videoAd.show().catch(() => {
  81. // 失败重试
  82. videoAd.load()
  83. .then(() => videoAd.show())
  84. .catch(err => {
  85. console.log('激励视频 广告显示失败')
  86. })
  87. })
  88. }
  89. }, 1000)
  90. } else {
  91. console.log('激励视频 广告显示失败-' + (ntime - that.$adtime.mvideo_time))
  92. }
  93. } else if (that.aitem.atype == 'pscreen') {
  94. if (ntime - that.$adtime.pscreen_time > 15) {
  95. that.opshow2 = false;
  96. that.$adtime.pscreen_time = ntime;
  97. // 在页面中定义插屏广告
  98. let interstitialAd = null
  99. // 在页面onLoad回调事件中创建插屏广告实例
  100. if (wx.createInterstitialAd) {
  101. interstitialAd = wx.createInterstitialAd({
  102. adUnitId: that.aitem.adpid
  103. })
  104. interstitialAd.onLoad(() => {})
  105. interstitialAd.onError((err) => {})
  106. interstitialAd.onClose(() => {})
  107. }
  108. setTimeout(function() {
  109. // 在适合的场景显示插屏广告
  110. if (interstitialAd) {
  111. // console.log('插屏广告 展示开始')
  112. interstitialAd.show().catch((err) => {
  113. console.error(err)
  114. })
  115. }
  116. }, 1000)
  117. } else {
  118. // console.log('插屏广告 广告显示失败-' + (ntime - that.$adtime.pscreen_time))
  119. }
  120. } else if (that.aitem.atype == 'banner') {
  121. if (ntime - that.$adtime.banner_time > 15) {
  122. // console.log('banner 展示开始' + that.aitem.adpid)
  123. that.opshow2 = true;
  124. that.$adtime.banner_time = ntime;
  125. } else {
  126. // console.log('banner广告 广告显示失败-' + (ntime - that.$adtime.banner_time))
  127. }
  128. }
  129. }
  130. } else {
  131. console.log('admyself', '广告配置获取失败')
  132. }
  133. // #endif
  134. },
  135. methods: {
  136. onadload(e) {
  137. // console.log('广告数据加载成功');
  138. if (this.opshow) {
  139. e.show();
  140. }
  141. },
  142. onadclose(e) {
  143. if (this.aitem.atype == 'mvideo') {
  144. //激励视频
  145. const detail = e.detail
  146. // 用户点击了【关闭广告】按钮
  147. if (detail && detail.isEnded) {
  148. // 正常播放结束
  149. console.log("onadclose " + detail.isEnded);
  150. } else {
  151. // 播放中途退出
  152. console.log("onadclose " + detail.isEnded);
  153. }
  154. } else if (this.aitem.atype == 'mvideo') {
  155. //插屏广告
  156. console.log("onadclose", e);
  157. }
  158. },
  159. onaderror(e) {
  160. // 广告加载失败
  161. console.log("onaderror: ", e.detail);
  162. },
  163. }
  164. }
  165. </script>
  166. <style lang="scss">
  167. .content {
  168. padding: 0rpx 20rpx 20rpx 20rpx;
  169. border-radius: 25rpx;
  170. }
  171. </style>