提交 8a1a36a5 编写于 作者: d-u-a's avatar d-u-a

add api uni.createRewardedVideoAd

上级 47436782
......@@ -201,6 +201,10 @@ const third = [
'setPageMeta'
]
const ad = [
'createRewardedVideoAd'
]
const apis = [
...base,
...network,
......@@ -214,7 +218,8 @@ const apis = [
...event,
...file,
...canvas,
...third
...third,
...ad
]
module.exports = apis
......@@ -206,4 +206,10 @@
"uni.base64ToArrayBuffer": true,
"uni.arrayBufferToBase64": true
}
}, {
"name": "ad",
"title": "广告",
"apiList": {
"uni.createRewardedVideoAd": true
}
}]
const eventNames = [
'load',
'close',
'error'
]
class RewardedVideoAd {
constructor (adpid) {
this._options = {
adpid: adpid
}
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 = false
this._loadPromiseResolve = null
this._loadPromiseReject = null
const rewardAd = this._rewardAd = plus.ad.createRewardedVideoAd(this._options)
rewardAd.onLoad((e) => {
this._isLoad = true
this._dispatchEvent('load', {})
if (this._loadPromiseResolve != null) {
this._loadPromiseResolve()
this._loadPromiseResolve = null
}
})
rewardAd.onClose((e) => {
this._loadAd()
this._dispatchEvent('close', { isEnded: e.isEnded })
})
rewardAd.onError((e) => {
const { code } = e
this._adError = (code && code < 5005)
this._dispatchEvent('error', e)
if (code === 5005 && this._loadPromiseReject != null) {
this._loadPromiseReject(e)
this._loadPromiseReject = null
}
})
this._loadAd()
}
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._rewardAd.show()
resolve()
} else {
reject(new Error(''))
}
})
}
_loadAd () {
if (this._adError) {
return
}
this._isLoad = false
this._rewardAd.load()
}
_dispatchEvent (name, data) {
this._callbacks[name].forEach(callback => {
if (typeof callback === 'function') {
callback(data || {})
}
})
}
}
export function createRewardedVideoAd ({
adpid = ''
} = {}) {
return new RewardedVideoAd(adpid)
}
......@@ -69,3 +69,5 @@ export {
export * from './ui/tab-bar'
export * from './ui/request-component-info'
export * from './ad/rewarded-video-ad'
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册