WeChatAdManager.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. export class WeChatAdManager {
  2. private bannerAd: any;
  3. private interstitialAd: any;
  4. constructor() {
  5. this.initBannerAd();
  6. this.initInterstitialAd();
  7. }
  8. private initBannerAd() {
  9. let wx = (window as any)["wx"];
  10. // var size = screen.windowSize
  11. if (wx.createCustomAd) {
  12. this.bannerAd = wx.createCustomAd({
  13. adUnitId: 'adunit-977eecc0430480e5',
  14. style: {
  15. left: 0,
  16. top: 0,
  17. width: 350
  18. }
  19. });
  20. this.bannerAd.onError((err) => {
  21. console.log('bannerAd onError', err);
  22. });
  23. }
  24. }
  25. private initInterstitialAd() {
  26. let wx = (window as any)["wx"];
  27. if (wx.createInterstitialAd) {
  28. this.interstitialAd = wx.createInterstitialAd({
  29. adUnitId: 'adunit-81dd4aeed2e21106'
  30. });
  31. this.interstitialAd.onLoad(() => {
  32. console.log('interstitialAd onLoad');
  33. });
  34. this.interstitialAd.onError((err) => {
  35. console.log('interstitialAd onError', err);
  36. });
  37. this.interstitialAd.onClose(() => {
  38. console.log('interstitialAd onClose');
  39. });
  40. }
  41. }
  42. private rewardVideo2
  43. public showRewardVideoAd(callback: Function) {
  44. let wx = (window as any)["wx"];
  45. let id = "adunit-c35014ce7d96a211"
  46. if (id == '') {
  47. callback(true);
  48. return;
  49. }
  50. if (this.rewardVideo2 != null) {
  51. this.rewardVideo2.offClose(fun);
  52. }
  53. let rewardedVideoAd = wx.createRewardedVideoAd({
  54. adUnitId: id,
  55. });
  56. this.rewardVideo2 = rewardedVideoAd;
  57. rewardedVideoAd.load().then(() => {
  58. wx.showToast({
  59. title: "加载中,请稍后",
  60. icon: 'success',//图标,支持"success"、"loading" 
  61. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  62. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  63. success: function () { },
  64. fail: function () { },
  65. complete: function () { }
  66. })
  67. console.log('激励视频 广告加载成功');
  68. rewardedVideoAd.show();
  69. });
  70. rewardedVideoAd.onError(err => {
  71. console.log('激励视频 广告显示失败', err);
  72. wx.showToast({
  73. title: "请稍后再试",
  74. icon: 'fail',//图标,支持"success"、"loading" 
  75. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  76. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  77. success: function () { },
  78. fail: function () { },
  79. complete: function () { }
  80. })
  81. callback(false);
  82. })
  83. var fun = function (res) {
  84. if (res && res.isEnded) {
  85. console.log('res: ', res);
  86. callback(true);
  87. rewardedVideoAd.offClose(fun);
  88. } else {
  89. console.log('播放中途退出');
  90. callback(false);
  91. }
  92. }
  93. rewardedVideoAd.onClose(fun);
  94. }
  95. public showBannerAd() {
  96. if (this.bannerAd) {
  97. this.bannerAd.show()
  98. }
  99. }
  100. public hideBannerAd() {
  101. if (this.bannerAd) {
  102. this.bannerAd.showhide()
  103. }
  104. }
  105. public showInterstitialAd() {
  106. if (this.interstitialAd) {
  107. this.interstitialAd.show().catch((err) => {
  108. console.error('插屏广告显示失败', err)
  109. })
  110. }
  111. }
  112. }