app-ad.vue 6.1 KB

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