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

add api uni.createRewardedVideoAd

上级 47436782
...@@ -201,6 +201,10 @@ const third = [ ...@@ -201,6 +201,10 @@ const third = [
'setPageMeta' 'setPageMeta'
] ]
const ad = [
'createRewardedVideoAd'
]
const apis = [ const apis = [
...base, ...base,
...network, ...network,
...@@ -214,7 +218,8 @@ const apis = [ ...@@ -214,7 +218,8 @@ const apis = [
...event, ...event,
...file, ...file,
...canvas, ...canvas,
...third ...third,
...ad
] ]
module.exports = apis module.exports = apis
...@@ -206,4 +206,10 @@ ...@@ -206,4 +206,10 @@
"uni.base64ToArrayBuffer": true, "uni.base64ToArrayBuffer": true,
"uni.arrayBufferToBase64": true "uni.arrayBufferToBase64": true
} }
}, {
"name": "ad",
"title": "广告",
"apiList": {
"uni.createRewardedVideoAd": true
}
}] }]
...@@ -213,6 +213,10 @@ var serviceContext = (function () { ...@@ -213,6 +213,10 @@ var serviceContext = (function () {
'setPageMeta' 'setPageMeta'
]; ];
const ad = [
'createRewardedVideoAd'
];
const apis = [ const apis = [
...base, ...base,
...network, ...network,
...@@ -226,7 +230,8 @@ var serviceContext = (function () { ...@@ -226,7 +230,8 @@ var serviceContext = (function () {
...event, ...event,
...file, ...file,
...canvas, ...canvas,
...third ...third,
...ad
]; ];
var apis_1 = apis; var apis_1 = apis;
...@@ -6483,7 +6488,7 @@ var serviceContext = (function () { ...@@ -6483,7 +6488,7 @@ var serviceContext = (function () {
} }
for (const name in formData) { for (const name in formData) {
if (formData.hasOwnProperty(name)) { if (formData.hasOwnProperty(name)) {
uploader.addData(name, formData[name]); uploader.addData(name, String(formData[name]));
} }
} }
if (files && files.length) { if (files && files.length) {
...@@ -8791,6 +8796,92 @@ var serviceContext = (function () { ...@@ -8791,6 +8796,92 @@ var serviceContext = (function () {
: requestComponentInfo(pageVm, queue, callback); : requestComponentInfo(pageVm, queue, callback);
} }
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._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._dispatchEvent('close', {isEnded: e.isEnded});
});
rewardAd.onError((e) => {
const detail = {code: e.code, message: e.message};
this._dispatchEvent('error', detail);
if(this._loadPromisereject != null) {
this._loadPromisereject(detail);
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();
}
})
}
_loadAd () {
this._isLoad = false;
this._rewardAd.load();
}
_dispatchEvent(name, data) {
this._callbacks[name].forEach(callback => {
if (typeof callback === 'function') {
callback(data || {});
}
});
}
}
function createRewardedVideoAd ({
adpid = ''
} = {}) {
return new RewardedVideoAd(adpid)
}
var api = /*#__PURE__*/Object.freeze({ var api = /*#__PURE__*/Object.freeze({
...@@ -8920,7 +9011,8 @@ var serviceContext = (function () { ...@@ -8920,7 +9011,8 @@ var serviceContext = (function () {
setTabBarStyle: setTabBarStyle$2, setTabBarStyle: setTabBarStyle$2,
hideTabBar: hideTabBar$2, hideTabBar: hideTabBar$2,
showTabBar: showTabBar$2, showTabBar: showTabBar$2,
requestComponentInfo: requestComponentInfo$2 requestComponentInfo: requestComponentInfo$2,
createRewardedVideoAd: createRewardedVideoAd
}); });
var platformApi = Object.assign(Object.create(null), api, eventApis); var platformApi = Object.assign(Object.create(null), api, eventApis);
...@@ -8956,7 +9048,7 @@ var serviceContext = (function () { ...@@ -8956,7 +9048,7 @@ var serviceContext = (function () {
return page && page.$page.id return page && page.$page.id
} }
const eventNames = [ const eventNames$1 = [
'canplay', 'canplay',
'play', 'play',
'pause', 'pause',
...@@ -9021,7 +9113,7 @@ var serviceContext = (function () { ...@@ -9021,7 +9113,7 @@ var serviceContext = (function () {
this.id = id; this.id = id;
this._callbacks = {}; this._callbacks = {};
this._options = {}; this._options = {};
eventNames.forEach(name => { eventNames$1.forEach(name => {
this._callbacks[name.toLowerCase()] = []; this._callbacks[name.toLowerCase()] = [];
}); });
props.forEach(item => { props.forEach(item => {
...@@ -9075,7 +9167,7 @@ var serviceContext = (function () { ...@@ -9075,7 +9167,7 @@ var serviceContext = (function () {
} }
} }
eventNames.forEach(item => { eventNames$1.forEach(item => {
const name = item[0].toUpperCase() + item.substr(1); const name = item[0].toUpperCase() + item.substr(1);
item = item.toLowerCase(); item = item.toLowerCase();
InnerAudioContext.prototype[`on${name}`] = function (callback) { InnerAudioContext.prototype[`on${name}`] = function (callback) {
...@@ -9140,7 +9232,7 @@ var serviceContext = (function () { ...@@ -9140,7 +9232,7 @@ var serviceContext = (function () {
createInnerAudioContext: createInnerAudioContext createInnerAudioContext: createInnerAudioContext
}); });
const eventNames$1 = [ const eventNames$2 = [
'canplay', 'canplay',
'play', 'play',
'pause', 'pause',
...@@ -9153,7 +9245,7 @@ var serviceContext = (function () { ...@@ -9153,7 +9245,7 @@ var serviceContext = (function () {
'waiting' 'waiting'
]; ];
const callbacks$4 = {}; const callbacks$4 = {};
eventNames$1.forEach(name => { eventNames$2.forEach(name => {
callbacks$4[name] = []; callbacks$4[name] = [];
}); });
...@@ -9267,7 +9359,7 @@ var serviceContext = (function () { ...@@ -9267,7 +9359,7 @@ var serviceContext = (function () {
} }
} }
eventNames$1.forEach(item => { eventNames$2.forEach(item => {
const name = item[0].toUpperCase() + item.substr(1); const name = item[0].toUpperCase() + item.substr(1);
BackgroundAudioManager.prototype[`on${name}`] = function (callback) { BackgroundAudioManager.prototype[`on${name}`] = function (callback) {
callbacks$4[item].push(callback); callbacks$4[item].push(callback);
...@@ -10996,6 +11088,23 @@ var serviceContext = (function () { ...@@ -10996,6 +11088,23 @@ var serviceContext = (function () {
const STORAGE_DATA_TYPE = '__TYPE'; const STORAGE_DATA_TYPE = '__TYPE';
const STORAGE_KEYS = 'uni-storage-keys'; const STORAGE_KEYS = 'uni-storage-keys';
function parseValue (value) {
const types = ['object', 'string', 'number', 'boolean', 'undefined'];
try {
const object = typeof value === 'string' ? JSON.parse(value) : value;
const type = object.type;
if (types.indexOf(type) >= 0) {
const keys = Object.keys(object);
// eslint-disable-next-line valid-typeof
if (keys.length === 2 && 'data' in object && typeof object.data === type) {
return object.data
} else if (keys.length === 1) {
return ''
}
}
} catch (error) { }
}
function setStorage$1 ({ function setStorage$1 ({
key, key,
data data
...@@ -11006,6 +11115,11 @@ var serviceContext = (function () { ...@@ -11006,6 +11115,11 @@ var serviceContext = (function () {
data: data data: data
}); });
try { try {
if (type === 'string' && parseValue(value) !== undefined) {
localStorage.setItem(key + STORAGE_DATA_TYPE, type);
} else {
localStorage.removeItem(key + STORAGE_DATA_TYPE);
}
localStorage.setItem(key, value); localStorage.setItem(key, value);
} catch (error) { } catch (error) {
return { return {
...@@ -11035,22 +11149,26 @@ var serviceContext = (function () { ...@@ -11035,22 +11149,26 @@ var serviceContext = (function () {
} }
} }
let data = value; let data = value;
const typeOrigin = localStorage.getItem(key + STORAGE_DATA_TYPE) || '';
const type = typeOrigin.toLowerCase();
if (type !== 'string' || (typeOrigin === 'String' && value === '{"type":"undefined"}')) {
try { try {
const object = JSON.parse(value); // 兼容H5和V3初期历史格式
let object = JSON.parse(value);
const result = parseValue(object);
if (result !== undefined) {
data = result;
} else if (type) {
// 兼容App端历史格式 // 兼容App端历史格式
const type = localStorage.getItem(key + STORAGE_DATA_TYPE);
if (!type) {
const keys = Object.keys(object);
if (keys.length === 2 && 'type' in object && 'data' in object) {
data = object.data;
} else if (keys.length === 1 && 'type' in object) {
data = '';
}
} else if (type !== 'String') {
data = object; data = object;
data = typeof data === 'string' ? JSON.parse(data) : data; if (typeof object === 'string') {
object = JSON.parse(object);
// eslint-disable-next-line valid-typeof
data = typeof object === (type === 'null' ? 'object' : type) ? object : data;
}
} }
} catch (error) { } } catch (error) { }
}
return { return {
data, data,
errMsg: 'getStorage:ok' errMsg: 'getStorage:ok'
......
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 { ...@@ -69,3 +69,5 @@ export {
export * from './ui/tab-bar' export * from './ui/tab-bar'
export * from './ui/request-component-info' 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.
先完成此消息的编辑!
想要评论请 注册