From 4e660eec4b93fb2eb3021196e1d140b35aa68cb8 Mon Sep 17 00:00:00 2001 From: handongxun Date: Tue, 6 Apr 2021 15:17:36 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E6=96=B0=E5=A2=9E=E6=8F=92=E5=B1=8F?= =?UTF-8?q?=E5=B9=BF=E5=91=8AAPI=20uni.createInterstitialAd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apis.js | 3 +- lib/modules.json | 3 +- .../service/api/ad/interstitial-ad.js | 115 ++++++++++++++++++ .../service/api/ad/rewarded-video-ad.js | 17 ++- src/platforms/app-plus/service/api/index.js | 1 + 5 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 src/platforms/app-plus/service/api/ad/interstitial-ad.js diff --git a/lib/apis.js b/lib/apis.js index 79ae1fbec..d9e6d7188 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -240,7 +240,8 @@ const third = [ const ad = [ 'createRewardedVideoAd', - 'createFullScreenVideoAd' + 'createFullScreenVideoAd', + 'createInterstitialAd' ] const apis = [ diff --git a/lib/modules.json b/lib/modules.json index 333c9e6ff..dd0c32dfd 100644 --- a/lib/modules.json +++ b/lib/modules.json @@ -223,6 +223,7 @@ "title": "广告", "apiList": { "uni.createRewardedVideoAd": true, - "uni.createFullScreenVideoAd": true + "uni.createFullScreenVideoAd": true, + "uni.'createInterstitialAd'": true } }] diff --git a/src/platforms/app-plus/service/api/ad/interstitial-ad.js b/src/platforms/app-plus/service/api/ad/interstitial-ad.js new file mode 100644 index 000000000..274257449 --- /dev/null +++ b/src/platforms/app-plus/service/api/ad/interstitial-ad.js @@ -0,0 +1,115 @@ +const eventNames = [ + 'load', + 'close', + 'error', + 'adClicked' +] + +class InterstitialAd { + constructor (options = {}) { + const _callbacks = this._callbacks = {} + eventNames.forEach(item => { + _callbacks[item] = [] + const name = item[0].toUpperCase() + item.substr(1) + this[`on${name}`] = function (callback) { + _callbacks[item].push(callback) + } + }) + + this._isLoad = false + this._isLoading = false + this._adError = '' + this._loadPromiseResolve = null + this._loadPromiseReject = null + + const ad = this._ad = plus.ad.createInterstitialAd(options) + ad.onLoad((e) => { + this._isLoad = true + this._isLoading = false + this._dispatchEvent('load', {}) + + if (this._loadPromiseResolve != null) { + this._loadPromiseResolve() + this._loadPromiseResolve = null + } + }) + ad.onClose((e) => { + this._isLoad = false + this._isLoading = false + this._dispatchEvent('close', {}) + }) + ad.onError((e) => { + this._isLoading = false + + const { code, message } = e + const data = { code: code, errMsg: message } + this._adError = message + + this._dispatchEvent('error', data) + + if (this._loadPromiseReject != null) { + this._loadPromiseReject(data) + this._loadPromiseReject = null + } + }) + ad.onAdClicked((e) => { + this._dispatchEvent('adClicked', {}) + }) + } + + load () { + return new Promise((resolve, reject) => { + this._loadPromiseResolve = resolve + this._loadPromiseReject = reject + if (this._isLoading) { + return + } + if (this._isLoad) { + resolve() + return + } + this._loadAd() + }) + } + + show () { + return new Promise((resolve, reject) => { + if (this._isLoading) { + return + } + + if (this._isLoad) { + this._ad.show() + resolve() + } else { + reject(new Error(this._adError)) + } + }) + } + + getProvider () { + return this._ad.getProvider() + } + + destroy () { + this._ad.destroy() + } + + _loadAd () { + this._isLoad = false + this._isLoading = true + this._ad.load() + } + + _dispatchEvent (name, data) { + this._callbacks[name].forEach(callback => { + if (typeof callback === 'function') { + callback(data || {}) + } + }) + } +} + +export function createInterstitialAd (options) { + return new InterstitialAd(options) +} diff --git a/src/platforms/app-plus/service/api/ad/rewarded-video-ad.js b/src/platforms/app-plus/service/api/ad/rewarded-video-ad.js index a6a3ea032..eb0008c5a 100644 --- a/src/platforms/app-plus/service/api/ad/rewarded-video-ad.js +++ b/src/platforms/app-plus/service/api/ad/rewarded-video-ad.js @@ -24,6 +24,7 @@ class RewardedVideoAd { this._preload = options.preload !== undefined ? options.preload : true this._isLoad = false + this._isLoading = false this._adError = '' this._loadPromiseResolve = null this._loadPromiseReject = null @@ -32,6 +33,7 @@ class RewardedVideoAd { const rewardAd = this._rewardAd = plus.ad.createRewardedVideoAd(options) rewardAd.onLoad((e) => { this._isLoad = true + this._isLoading = false this._lastLoadTime = Date.now() this._dispatchEvent('load', {}) @@ -41,6 +43,8 @@ class RewardedVideoAd { } }) rewardAd.onClose((e) => { + this._isLoad = false + this._isLoading = false if (this._preload) { this._loadAd() } @@ -50,6 +54,7 @@ class RewardedVideoAd { this._dispatchEvent('verify', { isValid: e.isValid }) }) rewardAd.onError((e) => { + this._isLoading = false const { code, message } = e const data = { code: code, errMsg: message } this._adError = message @@ -78,18 +83,25 @@ class RewardedVideoAd { load () { return new Promise((resolve, reject) => { + this._loadPromiseResolve = resolve + this._loadPromiseReject = reject + if (this._isLoading) { + return + } if (this._isLoad) { resolve() return } - this._loadPromiseResolve = resolve - this._loadPromiseReject = reject this._loadAd() }) } show () { return new Promise((resolve, reject) => { + if (this._isLoading) { + return + } + const provider = this.getProvider() if (provider === ProviderType.CSJ && this.isExpired) { this._isLoad = false @@ -118,6 +130,7 @@ class RewardedVideoAd { _loadAd () { this._isLoad = false + this._isLoading = true this._rewardAd.load() } diff --git a/src/platforms/app-plus/service/api/index.js b/src/platforms/app-plus/service/api/index.js index 495d35af3..6d650abbe 100644 --- a/src/platforms/app-plus/service/api/index.js +++ b/src/platforms/app-plus/service/api/index.js @@ -81,3 +81,4 @@ export * from './ui/request-component-info' export * from './ad/ad' export * from './ad/rewarded-video-ad' export * from './ad/full-screen-video-ad' +export * from './ad/interstitial-ad' -- GitLab