提交 4e660eec 编写于 作者: d-u-a's avatar d-u-a

update: 新增插屏广告API uni.createInterstitialAd

上级 2391124e
......@@ -240,7 +240,8 @@ const third = [
const ad = [
'createRewardedVideoAd',
'createFullScreenVideoAd'
'createFullScreenVideoAd',
'createInterstitialAd'
]
const apis = [
......
......@@ -223,6 +223,7 @@
"title": "广告",
"apiList": {
"uni.createRewardedVideoAd": true,
"uni.createFullScreenVideoAd": true
"uni.createFullScreenVideoAd": true,
"uni.'createInterstitialAd'": true
}
}]
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)
}
......@@ -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()
}
......
......@@ -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'
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册