From d53b1754d71ee91a3315f16d4295630191d8b8df Mon Sep 17 00:00:00 2001 From: handongxun Date: Thu, 15 Apr 2021 13:45:06 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E9=87=8D=E6=9E=84=E5=85=A8=E5=B1=8F?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E5=92=8C=E6=8F=92=E5=B1=8F=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=85=AC=E7=94=A8=E4=B8=80=E4=B8=AA=E5=9F=BA?= =?UTF-8?q?=E7=B1=BB=E5=87=8F=E5=B0=91=E4=BB=A3=E7=A0=81=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app-plus/service/api/ad/ad-base.js | 156 ++++++++++++++++++ .../service/api/ad/full-screen-video-ad.js | 103 +----------- .../service/api/ad/interstitial-ad.js | 112 +------------ 3 files changed, 167 insertions(+), 204 deletions(-) create mode 100644 src/platforms/app-plus/service/api/ad/ad-base.js diff --git a/src/platforms/app-plus/service/api/ad/ad-base.js b/src/platforms/app-plus/service/api/ad/ad-base.js new file mode 100644 index 000000000..4d210eae0 --- /dev/null +++ b/src/platforms/app-plus/service/api/ad/ad-base.js @@ -0,0 +1,156 @@ +const eventTypes = { + load: 'load', + close: 'close', + error: 'error', + adClicked: 'adClicked' +} + +const eventNames = [ + eventTypes.load, + eventTypes.close, + eventTypes.error, + eventTypes.adClicked +] + +class AdBase { + constructor (adInstance, 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._preload = options.preload !== undefined ? options.preload : false + + this._isLoaded = false + this._isLoading = false + this._adError = '' + this._loadPromiseResolve = null + this._loadPromiseReject = null + this._showPromiseResolve = null + this._showPromiseReject = null + + const ad = this._ad = adInstance + ad.onLoad((e) => { + this._isLoaded = true + this._isLoading = false + this._dispatchEvent(eventTypes.load, {}) + + if (this._loadPromiseResolve != null) { + this._loadPromiseResolve() + this._loadPromiseResolve = null + } + if (this._showPromiseResolve != null) { + this._showPromiseResolve() + this._showPromiseResolve = null + this._showAd() + } + }) + ad.onClose((e) => { + this._isLoaded = false + this._isLoading = false + this._dispatchEvent(eventTypes.close, { isEnded: e.isEnded }) + + if (this._preload === true) { + this._loadAd() + } + }) + ad.onError((e) => { + this._isLoading = false + + const data = { + code: e.code, + errMsg: e.message + } + + this._adError = data + + this._dispatchEvent(eventTypes.error, data) + + const promiseError = new Error(JSON.stringify(this._adError)) + promiseError.code = e.code + promiseError.errMsg = e.message + + if (this._loadPromiseReject != null) { + this._loadPromiseReject(promiseError) + this._loadPromiseReject = null + } + + if (this._showPromiseReject != null) { + this._showPromiseReject(promiseError) + this._showPromiseReject = null + } + }) + ad.onAdClicked && ad.onAdClicked((e) => { + this._dispatchEvent(eventTypes.adClicked, {}) + }) + } + + load () { + return new Promise((resolve, reject) => { + this._loadPromiseResolve = resolve + this._loadPromiseReject = reject + if (this._isLoading) { + return + } + + if (this._isLoaded) { + resolve() + } else { + this._loadAd() + } + }) + } + + show () { + return new Promise((resolve, reject) => { + this._showPromiseResolve = resolve + this._showPromiseReject = reject + + if (this._isLoading) { + return + } + + if (this._isLoaded) { + this._showAd() + resolve() + } else { + this._loadAd() + } + }) + } + + destroy () { + this._ad.destroy() + } + + getProvider () { + return this._ad.getProvider() + } + + _loadAd () { + this._adError = '' + this._isLoaded = false + this._isLoading = true + this._ad.load() + } + + _showAd () { + this._ad.show() + } + + _dispatchEvent (name, data) { + this._callbacks[name].forEach(callback => { + if (typeof callback === 'function') { + callback(data || {}) + } + }) + } +} + +export { + AdBase +} diff --git a/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js b/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js index 8f5efbc54..bb9053def 100644 --- a/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js +++ b/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js @@ -1,103 +1,10 @@ -const eventNames = [ - 'load', - 'close', - 'error', - 'adClicked' -] +import { + AdBase +} from './ad-base.js' -class FullScreenVideoAd { +class FullScreenVideoAd extends AdBase { 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._adError = '' - this._loadPromiseResolve = null - this._loadPromiseReject = null - this._lastLoadTime = 0 - - const ad = this._ad = plus.ad.createFullScreenVideoAd(options) - ad.onLoad((e) => { - this._isLoad = true - this._lastLoadTime = Date.now() - this._dispatchEvent('load', {}) - - if (this._loadPromiseResolve != null) { - this._loadPromiseResolve() - this._loadPromiseResolve = null - } - }) - ad.onClose((e) => { - this._isLoad = false - this._dispatchEvent('close', { isEnded: e.isEnded }) - }) - ad.onError((e) => { - const { code, message } = e - const data = { code: code, errMsg: message } - this._adError = message - if (code === -5008) { - this._isLoad = false - } - 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) => { - if (this._isLoad) { - resolve() - return - } - this._loadPromiseResolve = resolve - this._loadPromiseReject = reject - this._loadAd() - }) - } - - show () { - return new Promise((resolve, reject) => { - 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._ad.load() - } - - _dispatchEvent (name, data) { - this._callbacks[name].forEach(callback => { - if (typeof callback === 'function') { - callback(data || {}) - } - }) + super(plus.ad.createFullScreenVideoAd(options), options) } } diff --git a/src/platforms/app-plus/service/api/ad/interstitial-ad.js b/src/platforms/app-plus/service/api/ad/interstitial-ad.js index 274257449..adbc4143d 100644 --- a/src/platforms/app-plus/service/api/ad/interstitial-ad.js +++ b/src/platforms/app-plus/service/api/ad/interstitial-ad.js @@ -1,112 +1,12 @@ -const eventNames = [ - 'load', - 'close', - 'error', - 'adClicked' -] +import { + AdBase +} from './ad-base.js' -class InterstitialAd { +class InterstitialAd extends AdBase { 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) - } - }) + super(plus.ad.createInterstitialAd(options), options) - 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 || {}) - } - }) + this.load() } } -- GitLab