| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- 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
- }
- }
- })
|