gxb-js-sdk
Version:
GXB JS-SDK is a web development kit based on BlockCity for web developers.
73 lines (70 loc) • 2.31 kB
JavaScript
let methods = {};
let methodID = 0;
window.nativeCallback = function (mId) {
var args = Array.prototype.slice.call(arguments, 1);
typeof methods[mId] === 'function' && methods[mId].apply(this, args);
};
export default {
callBridge: function (schema, method, params = {}, callback) {
var mId = methodID++;
var url_params = '';
methods[mId] = function () {
typeof callback === 'function' && callback.apply(this, arguments);
if (methods[mId]) {
try {
delete methods[mId];
} catch (err) {
methods[mId] = null;
}
}
};
params._mId = mId;
for (var k in params) {
url_params += ((url_params.indexOf('=') != -1) ? '&' : '') + k + '=' + encodeURI(params[k]);
}
var url = schema + '://' + method + '?' + url_params;
var iFrame;
iFrame = document.createElement('iframe');
iFrame.setAttribute('src', url);
iFrame.setAttribute('style', 'display:none;');
iFrame.setAttribute('height', '0px');
iFrame.setAttribute('width', '0px');
iFrame.setAttribute('frameborder', '0');
document.body.appendChild(iFrame);
iFrame.parentNode.removeChild(iFrame);
iFrame = null;
},
callNative: function (method, params, callback) {
this.callBridge('native', method, params, callback);
},
exit: function () {
this.callNative('exit');
},
openExplorer: function (url) {
this.callNative('openurl', {url: url});
},
qrScan: function (callback) {
if (!callback) {
callback = function () {
};
}
this.callNative('qrscan', {
}, function (result) {
callback && callback(result);
});
},
shareConfig: function (config = {}, callback) {
if (!callback) {
callback = function () {
};
}
this.callNative('shareConfig', {
title: config.title || '',
shareDes: config.shareDes || '',
url: config.url || '',
thumbUrl: config.thumbUrl || ''
}, function (result) {
callback && callback(result);
});
}
};