Skip to content

Commit

Permalink
feat(ad): 支持新策略
Browse files Browse the repository at this point in the history
  • Loading branch information
hdx committed Nov 29, 2024
1 parent e6b7371 commit 77499c8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<view @click="_onclick">
<slot :options="options" :loading="loading" :error="errorMessage" />
<!-- #ifdef MP-WEIXIN -->
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
<!-- #endif -->
<!-- #ifdef MP-ALIPAY -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<view @click="_onclick">
<slot :options="options" :loading="loading" :error="errorMessage" />
<!-- #ifdef MP-WEIXIN -->
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
<!-- #endif -->
<!-- #ifdef MP-ALIPAY -->
Expand Down
106 changes: 58 additions & 48 deletions packages/uni-components/lib/ad/ad.mixin.mp-weixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ export default {
data () {
return {
loading: false,
userwx: false,
userUnitId: "",
wxchannel: false,
errorMessage: null
}
},
created () {
this._ad = null
this._loading = false
this._wxRewardedAd = null
this._wxInterstitialAd = null
this._providerType = ProviderType.ShanHu
if (this.preload && this._canCreateAd()) {
this.load()
Expand Down Expand Up @@ -153,26 +158,28 @@ export default {

_onnextchannel (e) {
this.wxchannel = true
const adData = e[1];
const adData = e.detail[0];
this.$nextTick(() => {
if (adData.provider == 10017) {
this._providerType = ProviderType.UserWeChat
switch(e._nt_) {
switch(adData._nt_) {
case 4:
case 21:
case 22:
case 23:
this.wxAdType = AdType.Banner
this.userwx = true
this.userUnitId = adData.posid
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
this.selectComponent('.uniad-plugin-wx').setConfig(e)
this.selectComponent('.uniad-plugin-wx').setConfig(adData)
}
})
},
Expand Down Expand Up @@ -223,50 +230,49 @@ export default {
},

_loadWxAd () {
this._userInvokeLoadFlag = true
switch (this.wxAdType) {
case AdType.RewardedVideo:
if (!this.rewardedAd) {
return;
if (this._wxRewardedAd) {
this._wxRewardedAd.load();
}
this.rewardedAd.load();
break;
case AdType.Interstitial:
if (!this.interstitialAd) {
return;
if (this._wxInterstitialAd) {
this._wxInterstitialAd.load();
}
this.interstitialAd.load();
break;
}
},

// 加载/显示广告
_showWxAd (options) {
this._urlCallback = options || this.urlCallback;
if (this.loading == true) {
this._userInvokeShowFlag = true
return
}
switch (this.wxAdType) {
case AdType.RewardedVideo:
if (!this.rewardedAd) {
this._userInvokeShowFlag = true
if (!this._wxRewardedAd) {
return;
}
this.rewardedAd.show().catch((err) => {
this.rewardedAd.load().then(() => {
this.rewardedAd.show();
this._wxRewardedAd.show().catch((err) => {
this._wxRewardedAd.load().then(() => {
this._wxRewardedAd.show();
}).catch((err) => {
this.triggerEvent(EventType.Error, err);
this._dispatchEvent(EventType.Error, err);
});
});
break;
case AdType.Interstitial:
if (!this.interstitialAd) {
this._userInvokeShowFlag = true
if (!this._wxInterstitialAd) {
return;
}
this.interstitialAd.show().catch((err) => {
this.interstitialAd.load().then(() => {
this.interstitialAd.show();
this._wxInterstitialAd.show().catch((err) => {
this._wxInterstitialAd.load().then(() => {
this._wxInterstitialAd.show();
}).catch((err) => {
this.triggerEvent(EventType.Error, err);
this._dispatchEvent(EventType.Error, err);
});
});
break;
Expand All @@ -275,60 +281,64 @@ export default {

// 微信激励视频
_createRewardedAd(adUnitId) {
if (this.rewardedAd) {
if (this._wxRewardedAd) {
return;
}

this.rewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true });
this._wxRewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true });

this.rewardedAd.onLoad(() => {
this.triggerEvent(EventType.Load, {})
this._wxRewardedAd.onLoad(() => {
this.loading = false
this._dispatchEvent(EventType.Load, {})
if (this._userInvokeShowFlag) {
this._userInvokeShowFlag = false;
this.rewardedAd.show();
this._wxRewardedAd.show();
}
});

this.rewardedAd.onError(err => {
this.triggerEvent(EventType.Error, err);
this._wxRewardedAd.onError(err => {
this.loading = false
this._dispatchEvent(EventType.Error, err);
});

this.rewardedAd.onClose(res => {
this._wxRewardedAd.onClose(res => {
if (res.isEnded) {
this._callServer()
} else {
this.triggerEvent(EventType.Close, res);
this._dispatchEvent(EventType.Close, res);
}
});

this.rewardedAd.load().catch((err) => {});
this.loading = true
},

// 微信插屏
_createInterstitialAd(adUnitId) {
if (this.interstitialAd) {
if (this._wxInterstitialAd) {
return;
}

this.interstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId });
this._wxInterstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId });

this.interstitialAd.onLoad(() => {
this.triggerEvent(EventType.Load, {})
this._wxInterstitialAd.onLoad(() => {
this.loading = false
this._dispatchEvent(EventType.Load, {})
if (this._userInvokeShowFlag) {
this._userInvokeShowFlag = false;
this.interstitialAd.show();
this._wxInterstitialAd.show();
}
});

this.interstitialAd.onError(err => {
this.triggerEvent(EventType.Error, err);
this._wxInterstitialAd.onError(err => {
this.loading = false
this._dispatchEvent(EventType.Error, err);
});

this.interstitialAd.onClose(res => {
this.triggerEvent(EventType.Close, res);
this._wxInterstitialAd.onClose(res => {
this._dispatchEvent(EventType.Close, res);
});

this.interstitialAd.load().catch((err) => {});
this.loading = true
},

_callServer() {
Expand All @@ -341,15 +351,15 @@ export default {
data: callbackData,
secretType: 'both',
success: (res) => {
this.triggerEvent(EventType.Close, res);
this._dispatchEvent(EventType.Close, res);
},
fail: (err) => {
this.triggerEvent(EventType.Error, err);
this._dispatchEvent(EventType.Error, err);
}
})
},
fail: (err) => {
this.triggerEvent(EventType.Error, err);
this._dispatchEvent(EventType.Error, err);
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/uni-components/lib/uniad/uniad.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
@load="_onmpload"
@close="_onmpclose"
@error="_onmperror"
@nextChannel="_onnextchannel"
/>
<!-- #ifdef MP-WEIXIN -->
<ad-custom v-if="userwx" :unit-id="userUnitId"></ad-custom>
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
<!-- #endif -->
</view>
Expand Down

0 comments on commit 77499c8

Please sign in to comment.