diff --git a/lib/apis.js b/lib/apis.js index 79ae1fbecb5749f7d4d1434ea7bf8db5eaaf6a87..d9e6d7188cae1934fc979274d88fe101c9215225 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 333c9e6ffed97c9653b4d006caf0de19dceb9898..dd0c32dfd69472c0f4d767d69f8a42c0777aa519 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 0000000000000000000000000000000000000000..27425744935172b5b24764db024135dc183c4a62 --- /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 a6a3ea032a26b8b4655f54c9c183ad36777afd92..eb0008c5aaed4a110006256f86983fd6a717769e 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 495d35af303bb8aa2aa2683b90b2c497633eb88c..6d650abbe9a456aa1b31d6cff6d74469c9d30ade 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'