提交 588a40b9 编写于 作者: d-u-a's avatar d-u-a

update: 新增互动广告API

上级 f46bdab5
......@@ -242,7 +242,8 @@ const third = [
const ad = [
'createRewardedVideoAd',
'createFullScreenVideoAd',
'createInterstitialAd'
'createInterstitialAd',
'createInteractiveAd'
]
const apis = [
......
......@@ -225,6 +225,7 @@
"apiList": {
"uni.createRewardedVideoAd": true,
"uni.createFullScreenVideoAd": true,
"uni.'createInterstitialAd'": true
"uni.'createInterstitialAd'": true,
"uni.'createInteractiveAd'": true
}
}]
......@@ -152,5 +152,7 @@ class AdBase {
}
export {
eventTypes,
eventNames,
AdBase
}
import {
requireNativePlugin
} from '../../bridge'
import {
eventTypes,
eventNames
} from './ad-base.js'
const sdkCache = {}
const sdkQueue = {}
function initSDK (options) {
const provider = options.provider
if (typeof sdkCache[provider] === 'object') {
options.success(sdkCache[provider])
return
}
if (!sdkQueue[provider]) {
sdkQueue[provider] = []
}
sdkQueue[provider].push(options)
if (sdkCache[provider] === true) {
return
}
sdkCache[provider] = true
const plugin = requireNativePlugin(provider)
if (!plugin || !plugin.initSDK) {
sdkQueue[provider].forEach((item) => {
item.fail({
code: -1,
message: 'provider [' + provider + '] invalid'
})
})
sdkQueue[provider].length = 0
sdkCache[provider] = false
return
}
options.__plugin = plugin
plugin.initSDK((res) => {
const isSuccess = (res.code === 1 || res.code === '1')
if (isSuccess) {
sdkCache[provider] = plugin
} else {
sdkCache[provider] = false
}
sdkQueue[provider].forEach((item) => {
if (isSuccess) {
item.success(item.__plugin)
} else {
item.fail(res)
}
})
sdkQueue[provider].length = 0
})
}
class InteractiveAd {
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._ad = null
this._adError = ''
this._adpid = options.adpid
this._provider = options.provider
this._isLoaded = false
this._isLoading = false
this._loadPromiseResolve = null
this._loadPromiseReject = null
this._showPromiseResolve = null
this._showPromiseReject = null
setTimeout(() => {
this._init()
})
}
_init () {
this._adError = ''
initSDK({
provider: this._provider,
success: (res) => {
this._ad = res
this._loadAd()
},
fail: (err) => {
this._adError = err
this._dispatchEvent(eventTypes.error, err)
}
})
}
getProvider () {
return this._provider
}
load () {
return new Promise((resolve, reject) => {
this._loadPromiseResolve = resolve
this._loadPromiseReject = reject
if (this._isLoading) {
return
}
if (this._adError) {
this._init()
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._adError) {
this._init()
return
}
if (this._isLoaded) {
this._showAd()
resolve()
} else {
this._loadAd()
}
})
}
destroy () {
if (this._ad !== null && this._ad.destroy) {
this._ad.destroy({
adpid: this._adpid
})
}
}
_loadAd () {
if (this._ad !== null) {
if (this._isLoading === true) {
return
}
this._isLoading = true
this._ad.loadData({
adpid: this._adpid
}, (res) => {
this._isLoaded = true
this._isLoading = false
this._dispatchEvent(eventTypes.load, res)
if (this._loadPromiseResolve != null) {
this._loadPromiseResolve()
this._loadPromiseResolve = null
}
if (this._showPromiseResolve != null) {
this._showPromiseResolve()
this._showPromiseResolve = null
this._showAd()
}
}, (err) => {
this._isLoading = false
this._dispatchEvent(eventTypes.error, err)
if (this._showPromiseReject != null) {
this._showPromiseReject(this._createError(err))
this._showPromiseReject = null
}
})
}
}
_showAd () {
if (this._ad !== null && this._isLoaded === true) {
this._ad.show({
adpid: this._adpid
}, (res) => {
this._isLoaded = false
}, (err) => {
this._isLoaded = false
this._dispatchEvent(eventTypes.error, err)
if (this._showPromiseReject != null) {
this._showPromiseReject(this._createError(err))
this._showPromiseReject = null
}
})
}
}
_createError (err) {
const error = new Error(JSON.stringify(err))
error.code = err.code
error.errMsg = err.message
return error
}
_dispatchEvent (name, data) {
this._callbacks[name].forEach(callback => {
if (typeof callback === 'function') {
callback(data || {})
}
})
}
}
export function createInteractiveAd (options) {
if (!options.provider) {
return new Error('provider invalid')
}
if (!options.adpid) {
return new Error('adpid invalid')
}
return new InteractiveAd(options)
}
......@@ -6,7 +6,7 @@ class InterstitialAd extends AdBase {
constructor (options = {}) {
super(plus.ad.createInterstitialAd(options), options)
this.load()
this._loadAd()
}
}
......
......@@ -82,3 +82,4 @@ export * from './ad/ad'
export * from './ad/rewarded-video-ad'
export * from './ad/full-screen-video-ad'
export * from './ad/interstitial-ad'
export * from './ad/interactive-ad'
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册