const EventType = { Load: 'load', Close: 'close', Error: 'error' } const AdType = { Banner: "banner", RewardedVideo: "rewardedVideo", Interstitial: "interstitial" } const ProviderType = { WeChat: 10018, UserWeChat: 10017, ShanHu: 10020 } Component({ properties: { adpid: { type: String, value: '', observer(newVal, oldVal, changedPath) { } }, plugins: { type: String, value: '[{"name":"uni-ad","version":"1.3.7","provider":"wxf72d316417b6767f"},{"name":"uni-ad-wx","version":"1.0.2","provider":"wxe6129e9cc9619c07"}]', observer(newVal, oldVal, changedPath) { } } }, data: { wxchannel: false, customFullscreen: "", }, created: function () { this.loading = false; this._wxRewardedAd = null; this._wxInterstitialAd = null; this._userInvokeShowFlag = false; this._providerType = ProviderType.ShanHu; this.load(); }, detached: function() { if (this._wxRewardedAd) { this._wxRewardedAd.destroy() } if (this._wxInterstitialAd) { this._wxInterstitialAd.destroy() } }, methods: { load () { if (this.loading) { return } this.loading = true this.errorMessage = null if (this._providerType == ProviderType.ShanHu) { } else if (this._providerType == ProviderType.WeChat) { this.selectComponent('.uniad-plugin-wx').load() } else if (this._providerType == ProviderType.UserWeChat) { this._loadWxAd() } }, show (e) { this.errorMessage = null if (this.loading == true) { this._userInvokeShowFlag = true return } if (this._providerType == ProviderType.ShanHu) { this.selectComponent('.uniad-plugin').show() } else if (this._providerType == ProviderType.WeChat) { this.selectComponent('.uniad-plugin-wx').show() } else if (this._providerType == ProviderType.UserWeChat) { this._showWxAd(e) } }, destroy() { // console.log('组件被销毁时自动关闭并销毁广告实例') }, _onclick () { this.show() }, _canCreateAd () { if (typeof this.adpid === 'string' && this.adpid.length > 0) { return true } else if (typeof this.adpid === 'number') { return true } return false }, _onmpload (e) { this.loading = false this.triggerEvent(EventType.Load, {}) if (this._userInvokeShowFlag) { this._userInvokeShowFlag = false; setTimeout(() => { this.show(); }, 1) } }, _onmpclose (e) { this.triggerEvent(EventType.Close, e.detail) }, _onmperror (e) { this.loading = false this.errorMessage = JSON.stringify(e.detail) this.triggerEvent(EventType.Error, e.detail) }, _onnextchannel (e) { this.setData({ wxchannel: true }); const adData = e.detail[0]; setTimeout(() => { if (adData.provider == 10017) { this._providerType = ProviderType.UserWeChat switch(adData._nt_) { case 4: this.wxAdType = AdType.Banner this.userwx = true this.userUnitId = adData.posid if (adData.tmpl_type === 24) { this.setData({ customFullscreen: 'uni-ad-custom-fullscreen' }) } break; case 9: this.wxAdType = AdType.RewardedVideo this._createRewardedAd(adData.posid) break; case 15: this.wxAdType = AdType.Interstitial this._createInterstitialAd(adData.posid) break; } } else if (adData.provider == 10018) { this._providerType = ProviderType.WeChat if (adData.tmpl_type === 24) { this.setData({ customFullscreen: 'uni-ad-custom-fullscreen' }) } this.loading = true this.selectComponent('.uniad-plugin-wx').setConfig(adData) } }) }, _onwxchannelerror(e) { this.triggerEvent(EventType.Error, e.detail) }, _loadWxAd () { switch (this.wxAdType) { case AdType.RewardedVideo: if (this._wxRewardedAd) { this._wxRewardedAd.load(); } break; case AdType.Interstitial: if (this._wxInterstitialAd) { this._wxInterstitialAd.load(); } break; } }, _showWxAd () { if (this.loading == true) { this._userInvokeShowFlag = true return } switch (this.wxAdType) { case AdType.RewardedVideo: if (!this._wxRewardedAd) { return; } this._wxRewardedAd.show().catch((err) => { this._wxRewardedAd.load().then(() => { this._wxRewardedAd.show(); }).catch((err) => { this.triggerEvent(EventType.Error, err); }); }); break; case AdType.Interstitial: if (!this._wxInterstitialAd) { return; } this._wxInterstitialAd.show().catch((err) => { this._wxInterstitialAd.load().then(() => { this._wxInterstitialAd.show(); }).catch((err) => { this.triggerEvent(EventType.Error, err); }); }); break; } }, _createRewardedAd(adUnitId) { if (this._wxRewardedAd) { return; } this._wxRewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true }); this._wxRewardedAd.onLoad(() => { this.loading = false this.triggerEvent(EventType.Load, {}) if (this._userInvokeShowFlag) { this._userInvokeShowFlag = false; this._wxRewardedAd.show(); } }); this._wxRewardedAd.onError(err => { this.loading = false this.triggerEvent(EventType.Error, err); }); this._wxRewardedAd.onClose(res => { this.triggerEvent(EventType.Close, res); }); this._wxRewardedAd?.load().then(() => { }).catch((err) => { }); this.loading = true }, _createInterstitialAd(adUnitId) { if (this._wxInterstitialAd) { return; } this._wxInterstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId }); this._wxInterstitialAd.onLoad(() => { this.loading = false this.triggerEvent(EventType.Load, {}) if (this._userInvokeShowFlag) { this._userInvokeShowFlag = false; this._wxInterstitialAd.show(); } }); this._wxInterstitialAd.onError(err => { this.loading = false this.triggerEvent(EventType.Error, err); }); this._wxInterstitialAd.onClose(res => { this.triggerEvent(EventType.Close, res); }); this._wxInterstitialAd?.load().then(() => { }).catch((err) => { }); this.loading = true } } })