export class WeChatAdManager { private bannerAd: any; private interstitialAd: any; constructor() { this.initBannerAd(); this.initInterstitialAd(); } private initBannerAd() { let wx = (window as any)["wx"]; // var size = screen.windowSize if (wx.createCustomAd) { this.bannerAd = wx.createCustomAd({ adUnitId: 'adunit-977eecc0430480e5', style: { left: 0, top: 0, width: 350 } }); this.bannerAd.onError((err) => { console.log('bannerAd onError', err); }); } } private initInterstitialAd() { let wx = (window as any)["wx"]; if (wx.createInterstitialAd) { this.interstitialAd = wx.createInterstitialAd({ adUnitId: 'adunit-81dd4aeed2e21106' }); this.interstitialAd.onLoad(() => { console.log('interstitialAd onLoad'); }); this.interstitialAd.onError((err) => { console.log('interstitialAd onError', err); }); this.interstitialAd.onClose(() => { console.log('interstitialAd onClose'); }); } } private rewardVideo2 public showRewardVideoAd(callback: Function) { let wx = (window as any)["wx"]; let id = "adunit-c35014ce7d96a211" if (id == '') { callback(true); return; } if (this.rewardVideo2 != null) { this.rewardVideo2.offClose(fun); } let rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: id, }); this.rewardVideo2 = rewardedVideoAd; rewardedVideoAd.load().then(() => { wx.showToast({ title: "加载中,请稍后", icon: 'success',//图标,支持"success"、"loading"  duration: 1500,//提示的延迟时间,单位毫秒,默认:1500  mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false  success: function () { }, fail: function () { }, complete: function () { } }) console.log('激励视频 广告加载成功'); rewardedVideoAd.show(); }); rewardedVideoAd.onError(err => { console.log('激励视频 广告显示失败', err); wx.showToast({ title: "请稍后再试", icon: 'fail',//图标,支持"success"、"loading"  duration: 1500,//提示的延迟时间,单位毫秒,默认:1500  mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false  success: function () { }, fail: function () { }, complete: function () { } }) callback(false); }) var fun = function (res) { if (res && res.isEnded) { console.log('res: ', res); callback(true); rewardedVideoAd.offClose(fun); } else { console.log('播放中途退出'); callback(false); } } rewardedVideoAd.onClose(fun); } public showBannerAd() { if (this.bannerAd) { this.bannerAd.show() } } public hideBannerAd() { if (this.bannerAd) { this.bannerAd.showhide() } } public showInterstitialAd() { if (this.interstitialAd) { this.interstitialAd.show().catch((err) => { console.error('插屏广告显示失败', err) }) } } }