app-ad.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view>
  3. <!--#ifndef MP-ALIPAY-->
  4. <ad v-if="type=== ``" :unit-id="unitId" @load="onAdLoad" @error="onAdError" @close="onAdClose" ad-intervals="0"/>
  5. <ad v-else-if="type === `video`" :unit-id="unitId" :ad-type="type" :ad-theme="theme" @load="onAdLoad" @error="onAdError" @close="onAdClose"/>
  6. <ad v-else-if="type === `grid`" :unit-id="unitId" :ad-theme="theme" :ad-type="type" @load="onAdLoad" @error="onAdError" @close="onAdClose" grid-opacity="0.8" grid-count="5"/>
  7. <!--#endif-->
  8. <!--#ifdef MP-WEIXIN-->
  9. <img v-else-if="type === `rewarded-video`" class="ad-img" @click="showRewardedVideoAd" :src="picUrl"/>
  10. <img v-else-if="type === `interstitial`" class="ad-img" @click="showInterstitialAd" :src="picUrl"/>
  11. <!--#endif-->
  12. <view v-if="type ===`before-video`" class="ad-video">
  13. <video :src="videoUrl" :ad-unit-id="unitId" :poster="picUrl" @adplay="onAdPlay" @adload="onAdLoad" @adclose="onAdClose" @aderror="onAdError"/>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: "app-ad",
  20. props: {
  21. type: String,
  22. unitId: String,
  23. picUrl: String,
  24. videoUrl: String,
  25. // #ifndef MP-ALIPAY
  26. theme: {
  27. type: String,
  28. default() {
  29. return 'white';
  30. }
  31. },
  32. //#endif
  33. //#ifdef MP-WEIXIN
  34. couponUrl: String,
  35. couponParams: Object,
  36. //#endif
  37. },
  38. //#ifdef MP-WEIXIN
  39. data() {
  40. return {
  41. //单屏问题
  42. rewardedVideoAd: null,
  43. interstitialAd: null
  44. }
  45. },
  46. //#endif
  47. //#ifdef MP-WEIXIN
  48. created: function () {
  49. this.init();
  50. },
  51. //#endif
  52. methods: {
  53. onAdLoad() {},
  54. onAdPlay() {},
  55. onAdClose() {},
  56. onAdError() {},
  57. //#ifdef MP-WEIXIN
  58. init: function () {
  59. switch (this.type) {
  60. case 'rewarded-video':
  61. this.initRewardedVideoAd();
  62. break;
  63. case 'interstitial':
  64. this.initInterstitialAd();
  65. break;
  66. default:
  67. break;
  68. }
  69. },
  70. //#endif
  71. //#ifdef MP-WEIXIN
  72. initRewardedVideoAd: function () {
  73. if (wx.createRewardedVideoAd) {
  74. this.rewardedVideoAd = wx.createRewardedVideoAd({adUnitId: this.unitId});
  75. this.rewardedVideoAd.onClose((res) => {
  76. if (res && res.isEnded) {
  77. this.getUserCoupon();
  78. }
  79. });
  80. }
  81. },
  82. //#endif
  83. //#ifdef MP-WEIXIN
  84. initInterstitialAd: function () {
  85. if (wx.createInterstitialAd) {
  86. this.interstitialAd = wx.createInterstitialAd({adUnitId: this.unitId});
  87. }
  88. },
  89. //#endif
  90. //#ifdef MP-WEIXIN
  91. showRewardedVideoAd: function () {
  92. this.rewardedVideoAd.show().catch(() => {
  93. this.rewardedVideoAd.load().then(() => this.rewardedVideoAd.show()).catch(err => {
  94. uni.showToast({title: err.errMsg, icon: 'none'});
  95. })
  96. });
  97. },
  98. //#endif
  99. //#ifdef MP-WEIXIN
  100. showInterstitialAd: function () {
  101. this.interstitialAd.show().catch((err) => {
  102. uni.showToast({title: err.errMsg, icon: 'none'});
  103. });
  104. },
  105. //#endif
  106. //#ifdef MP-WEIXIN
  107. getUserCoupon() {
  108. this.$request({
  109. url: this.couponUrl,
  110. method: 'POST',
  111. data: Object.assign({}, this.couponParams)
  112. }).then(info => {
  113. if (info.code === 0) {
  114. this.$store.dispatch('page/actionSetCoupon', {
  115. list: info.data.list,
  116. type: 'award'
  117. })
  118. } else {
  119. uni.showToast({title: info.msg, icon: 'none'});
  120. }
  121. })
  122. },
  123. //#endif
  124. }
  125. }
  126. </script>
  127. <style scoped lang="scss">
  128. //#ifdef MP-WEIXIN
  129. .ad-img {
  130. display: block;
  131. height: 139px;
  132. width: 100vw;
  133. }
  134. //#endif
  135. .ad-video {
  136. height: 230px;
  137. width: 100vw;
  138. video {
  139. height: 100%;
  140. width: 100%;
  141. }
  142. }
  143. </style>