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 5d44ee5d4f83be22c8e3c5fdaa655547956c4f7a..acf41922608a29e701a47df6d020268e92d13427 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 @@ -1,4 +1,3 @@ - const eventNames = [ 'load', 'close', @@ -7,6 +6,9 @@ const eventNames = [ ] const ERROR_CODE_LIST = [-5001, -5002, -5003, -5004, -5005, -5006] +const EXPIRED_TIME = 1000 * 60 * 30 +const EXPIRED_TEXT = { code: -5008, errMsg: '广告数据已过期,请重新加载' } +const ProviderType = { CSJ: 'csj', GDT: 'gdt' } class RewardedVideoAd { constructor (options = {}) { @@ -23,10 +25,14 @@ class RewardedVideoAd { this._adError = '' this._loadPromiseResolve = null this._loadPromiseReject = null + this._lastLoadTime = 0 + const rewardAd = this._rewardAd = plus.ad.createRewardedVideoAd(options) rewardAd.onLoad((e) => { this._isLoad = true this._dispatchEvent('load', {}) + this._lastLoadTime = Date.now() + if (this._loadPromiseResolve != null) { this._loadPromiseResolve() this._loadPromiseResolve = null @@ -43,7 +49,11 @@ class RewardedVideoAd { const { code, message } = e const data = { code: code, errMsg: message } this._adError = message + if (code === -5008) { + this._isLoad = false + } this._dispatchEvent('error', data) + // TODO if ((code === -5005 || ERROR_CODE_LIST.index(code) === -1) && this._loadPromiseReject != null) { this._loadPromiseReject(data) this._loadPromiseReject = null @@ -52,6 +62,10 @@ class RewardedVideoAd { this._loadAd() } + get isExpired () { + return (Math.abs(Date.now() - this._lastLoadTime) > EXPIRED_TIME) + } + load () { return new Promise((resolve, reject) => { if (this._isLoad) { @@ -66,6 +80,15 @@ class RewardedVideoAd { show () { return new Promise((resolve, reject) => { + const provider = this.getProvider() + if (provider === ProviderType.CSJ && this.isExpired) { + this._isLoad = false + // TODO + this._dispatchEvent('error', EXPIRED_TEXT) + reject(new Error(EXPIRED_TEXT.errMsg)) + return + } + if (this._isLoad) { this._rewardAd.show() resolve()