var isFn = function isFn(v) {
|
return typeof v === 'function';
|
};
|
|
var handlePromise = function handlePromise(promise) {
|
return promise.then(function (data) {
|
return [null, data];
|
}).catch(function (err) {
|
return [err];
|
});
|
};
|
|
var REGEX = /^on|^create|Sync$|Manager$|^pause/;
|
var API_NORMAL_LIST = ['os', 'stopRecord', 'stopVoice', 'stopBackgroundAudio', 'stopPullDownRefresh', 'hideKeyboard', 'hideToast', 'hideLoading', 'showNavigationBarLoading', 'hideNavigationBarLoading', 'canIUse', 'navigateBack', 'closeSocket', 'pageScrollTo', 'drawCanvas'];
|
|
var shouldPromise = function shouldPromise(name) {
|
if (REGEX.test(name) && name !== 'createBLEConnection') {
|
return false;
|
}
|
if (~API_NORMAL_LIST.indexOf(name)) {
|
return false;
|
}
|
return true;
|
};
|
|
var promisify = function promisify(api) {
|
return function () {
|
for (var _len = arguments.length, params = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
params[_key - 1] = arguments[_key];
|
}
|
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
if (isFn(options.success) || isFn(options.fail) || isFn(options.complete)) {
|
return api.apply(undefined, [options].concat(params));
|
}
|
return handlePromise(new Promise(function (resolve, reject) {
|
api.apply(undefined, [Object.assign({}, options, {
|
success: resolve,
|
fail: reject
|
})].concat(params));
|
/* eslint-disable no-extend-native */
|
Promise.prototype.finally = function (callback) {
|
var promise = this.constructor;
|
return this.then(function (value) {
|
return promise.resolve(callback()).then(function () {
|
return value;
|
});
|
}, function (reason) {
|
return promise.resolve(callback()).then(function () {
|
throw reason;
|
});
|
});
|
};
|
}));
|
};
|
};
|
|
var UNIAPP_LAUNCH_WEBVIEW_ID = '__UNIAPP_LAUNCH_WEBVIEW_ID';
|
|
var plus = weex.requireModule('plus');
|
var storage = weex.requireModule('plusstorage');
|
var globalEvent = weex.requireModule('globalEvent');
|
|
var id = 0;
|
var callbacks = {};
|
|
var WEBVIEW_ID = '';
|
|
if (weex.config.plus_appid) {
|
WEBVIEW_ID = weex.config.plus_appid;
|
} else {
|
storage && storage.getItem && storage.getItem(UNIAPP_LAUNCH_WEBVIEW_ID, function (evt) {
|
if (evt.result === 'success' && evt.data) {
|
WEBVIEW_ID = evt.data;
|
}
|
});
|
}
|
|
globalEvent.addEventListener('plusMessage', function (e) {
|
if (e.data.type === 'UniAppJsApi') {
|
invoke(e.data.id, e.data.data);
|
} else if (e.data.type === 'onNavigationBarButtonTap') {
|
if (typeof onNavigationBarButtonTapCallback === 'function') {
|
onNavigationBarButtonTapCallback(e.data.data);
|
}
|
} else if (e.data.type === 'onNavigationBarSearchInputChanged') {
|
if (typeof onNavigationBarSearchInputChangedCallback === 'function') {
|
onNavigationBarSearchInputChangedCallback(e.data.data);
|
}
|
} else if (e.data.type === 'onNavigationBarSearchInputConfirmed') {
|
if (typeof onNavigationBarSearchInputConfirmedCallback === 'function') {
|
onNavigationBarSearchInputConfirmedCallback(e.data.data);
|
}
|
} else if (e.data.type === 'onNavigationBarSearchInputClicked') {
|
if (typeof onNavigationBarSearchInputClickedCallback === 'function') {
|
onNavigationBarSearchInputClickedCallback(e.data.data);
|
}
|
}
|
});
|
|
var createCallback = function createCallback(args, type) {
|
var callback = function callback(res) {
|
if (isFn(args)) {
|
args(res);
|
} else if (args) {
|
if (~res.errMsg.indexOf(':ok')) {
|
isFn(args.success) && args.success(res);
|
} else if (~res.errMsg.indexOf(':fail')) {
|
isFn(args.fail) && args.fail(res);
|
}
|
isFn(args.complete) && args.complete(res);
|
}
|
};
|
if (isFn(args) || args && isFn(args.callback)) {
|
callback.keepAlive = true;
|
}
|
return callback;
|
};
|
|
var invoke = function invoke(callbackId, data) {
|
var callback = callbacks[callbackId];
|
if (callback) {
|
callback(data);
|
if (!callback.keepAlive) {
|
delete callbacks[callbackId];
|
}
|
} else {
|
console.error('callback[' + callbackId + '] is undefined');
|
}
|
};
|
|
var publish = function publish(_ref) {
|
var id = _ref.id,
|
type = _ref.type,
|
params = _ref.params;
|
|
callbacks[id] = createCallback(params, type);
|
if (WEBVIEW_ID) {
|
plus.postMessage({
|
id: id,
|
type: type,
|
params: params
|
}, WEBVIEW_ID);
|
} else {
|
console.error('launch webview id is not ready');
|
}
|
};
|
|
function postMessage(data) {
|
if (WEBVIEW_ID) {
|
plus.postMessage(data, WEBVIEW_ID);
|
} else {
|
console.error('launch webview id is not ready');
|
}
|
}
|
|
var createPublish = function createPublish(name) {
|
return function (args) {
|
publish({
|
id: id++,
|
type: name,
|
params: args
|
});
|
};
|
};
|
|
var onNavigationBarButtonTapCallback = void 0;
|
var onNavigationBarSearchInputChangedCallback = void 0;
|
var onNavigationBarSearchInputConfirmedCallback = void 0;
|
var onNavigationBarSearchInputClickedCallback = void 0;
|
function onNavigationBarButtonTap(callback) {
|
onNavigationBarButtonTapCallback = callback;
|
}
|
function onNavigationBarSearchInputChanged(callback) {
|
onNavigationBarSearchInputChangedCallback = callback;
|
}
|
function onNavigationBarSearchInputConfirmed(callback) {
|
onNavigationBarSearchInputConfirmedCallback = callback;
|
}
|
function onNavigationBarSearchInputClicked(callback) {
|
onNavigationBarSearchInputClickedCallback = callback;
|
}
|
|
function requireNativePlugin(pluginName) {
|
return weex.requireModule(pluginName);
|
}
|
|
var dom = weex.requireModule('dom');
|
|
function loadFontFace(_ref) {
|
var family = _ref.family,
|
source = _ref.source,
|
desc = _ref.desc,
|
success = _ref.success,
|
fail = _ref.fail,
|
complete = _ref.complete;
|
|
dom.addRule('fontFace', {
|
fontFamily: family,
|
src: source.replace(/"/g, '\'')
|
});
|
var res = {
|
errMsg: 'loadFontFace:ok',
|
status: 'loaded'
|
};
|
isFn(success) && success(res);
|
isFn(complete) && complete(res);
|
}
|
|
var globalEvent$1 = weex.requireModule('globalEvent');
|
|
var callbacks$1 = [];
|
|
globalEvent$1.addEventListener('plusMessage', function (e) {
|
if (e.data.type === 'UniAppReady') {
|
ready.isUniAppReady = true;
|
if (callbacks$1.length) {
|
callbacks$1.forEach(function (callback) {
|
return callback();
|
});
|
callbacks$1 = [];
|
}
|
}
|
});
|
|
function ready(callback) {
|
if (typeof callback === 'function') {
|
if (this.isUniAppReady) {
|
callback();
|
} else {
|
callbacks$1.push(callback);
|
}
|
}
|
}
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
|
return typeof obj;
|
} : function (obj) {
|
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
};
|
|
var stream = weex.requireModule('stream');
|
|
// let requestTaskId = 0
|
|
var METHOD_GET = 'GET';
|
var METHOD_POST = 'POST';
|
var CONTENT_TYPE_JSON = 'application/json';
|
var CONTENT_TYPE_FORM = 'application/x-www-form-urlencoded';
|
|
var serialize = function serialize(data) {
|
var method = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : METHOD_GET;
|
var contentType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : CONTENT_TYPE_FORM;
|
|
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object') {
|
if (method.toUpperCase() === METHOD_POST && contentType.toLowerCase() === CONTENT_TYPE_JSON) {
|
return JSON.stringify(data);
|
} else {
|
return Object.keys(data).map(function (key) {
|
return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
|
}).join('&');
|
}
|
}
|
return data;
|
};
|
|
function request(_ref) {
|
var url = _ref.url,
|
data = _ref.data,
|
header = _ref.header,
|
_ref$method = _ref.method,
|
method = _ref$method === undefined ? 'GET' : _ref$method,
|
_ref$dataType = _ref.dataType,
|
dataType = _ref$dataType === undefined ? 'json' : _ref$dataType,
|
responseType = _ref.responseType,
|
success = _ref.success,
|
fail = _ref.fail,
|
complete = _ref.complete;
|
|
// requestTaskId++
|
var aborted = false;
|
|
var hasContentType = false;
|
var headers = {};
|
if (header) {
|
for (var name in header) {
|
if (!hasContentType && name.toLowerCase() === 'content-type') {
|
hasContentType = true;
|
headers['Content-Type'] = header[name];
|
} else {
|
headers[name] = header[name];
|
}
|
}
|
}
|
if (method === METHOD_GET && data) {
|
url = url + (~url.indexOf('?') ? url.substr(-1) === '&' || url.substr(-1) === '?' ? '' : '&' : '?') + serialize(data);
|
}
|
stream.fetch({
|
url: url,
|
method: method,
|
headers: headers,
|
type: dataType === 'json' ? 'json' : 'text',
|
body: method !== METHOD_GET ? serialize(data, method, headers['Content-Type']) : ''
|
}, function (_ref2) {
|
var status = _ref2.status,
|
ok = _ref2.ok,
|
statusText = _ref2.statusText,
|
data = _ref2.data,
|
headers = _ref2.headers;
|
|
var ret = {};
|
if (!status || status === -1 || aborted) {
|
ret.errMsg = 'request:fail';
|
isFn(fail) && fail(ret);
|
} else {
|
ret.data = data;
|
ret.statusCode = status;
|
ret.header = headers;
|
isFn(success) && success(ret);
|
}
|
isFn(complete) && complete(ret);
|
});
|
return {
|
abort: function abort() {
|
aborted = true;
|
}
|
};
|
}
|
|
var storage$1 = weex.requireModule('plusstorage');
|
var UNIAPP_STORAGE_DATA_TYPE = '__TYPE';
|
|
function getStorage(_ref) {
|
var key = _ref.key,
|
data = _ref.data,
|
success = _ref.success,
|
fail = _ref.fail,
|
complete = _ref.complete;
|
|
storage$1.getItem(key + UNIAPP_STORAGE_DATA_TYPE, function (ret) {
|
if (ret.result === 'success') {
|
var dataType = ret.data;
|
storage$1.getItem(key, function (res) {
|
if (res.result === 'success') {
|
var result = res.data;
|
if (dataType && result) {
|
if (dataType !== 'String') {
|
result = JSON.parse(result);
|
}
|
isFn(success) && success({
|
errMsg: 'getStorage:ok',
|
data: result
|
});
|
} else {
|
res.errMsg = 'setStorage:fail';
|
isFn(fail) && fail(res);
|
}
|
} else {
|
res.errMsg = 'setStorage:fail';
|
isFn(fail) && fail(res);
|
}
|
isFn(complete) && complete(res);
|
});
|
} else {
|
ret.errMsg = 'setStorage:fail';
|
isFn(fail) && fail(ret);
|
isFn(complete) && complete(ret);
|
}
|
});
|
}
|
function setStorage(_ref2) {
|
var key = _ref2.key,
|
data = _ref2.data,
|
success = _ref2.success,
|
fail = _ref2.fail,
|
complete = _ref2.complete;
|
|
var dataType = 'String';
|
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object') {
|
dataType = 'Object';
|
data = JSON.stringify(data);
|
}
|
storage$1.setItem(key, data, function (res) {
|
if (res.result === 'success') {
|
storage$1.setItem(key + UNIAPP_STORAGE_DATA_TYPE, dataType, function (ret) {
|
if (ret.result === 'success') {
|
isFn(success) && success({
|
errMsg: 'setStorage:ok'
|
});
|
} else {
|
ret.errMsg = 'setStorage:fail';
|
isFn(fail) && fail(ret);
|
}
|
});
|
} else {
|
res.errMsg = 'setStorage:fail';
|
isFn(fail) && fail(res);
|
}
|
isFn(complete) && complete(res);
|
});
|
}
|
|
function removeStorage(_ref3) {
|
var key = _ref3.key,
|
data = _ref3.data,
|
success = _ref3.success,
|
fail = _ref3.fail,
|
complete = _ref3.complete;
|
|
storage$1.removeItem(key, function (res) {
|
if (res.result === 'success') {
|
isFn(success) && success({
|
errMsg: 'removeStorage:ok'
|
});
|
} else {
|
res.errMsg = 'removeStorage:fail';
|
isFn(fail) && fail(res);
|
}
|
isFn(complete) && complete(res);
|
});
|
storage$1.removeItem(key + UNIAPP_STORAGE_DATA_TYPE);
|
}
|
|
function clearStorage(_ref4) {
|
var key = _ref4.key,
|
data = _ref4.data,
|
success = _ref4.success,
|
fail = _ref4.fail,
|
complete = _ref4.complete;
|
}
|
|
var clipboard = weex.requireModule('clipboard');
|
|
function getClipboardData(_ref) {
|
var success = _ref.success,
|
fail = _ref.fail,
|
complete = _ref.complete;
|
|
clipboard.getString(function (_ref2) {
|
var data = _ref2.data;
|
|
var res = {
|
errMsg: 'getClipboardData:ok',
|
data: data
|
};
|
isFn(success) && success(res);
|
isFn(complete) && complete(res);
|
});
|
}
|
|
function setClipboardData(_ref3) {
|
var data = _ref3.data,
|
success = _ref3.success,
|
fail = _ref3.fail,
|
complete = _ref3.complete;
|
|
var res = {
|
errMsg: 'setClipboardData:ok'
|
};
|
clipboard.setString(data);
|
isFn(success) && success(res);
|
isFn(complete) && complete(res);
|
}
|
|
|
|
var api = /*#__PURE__*/Object.freeze({
|
loadFontFace: loadFontFace,
|
ready: ready,
|
request: request,
|
getStorage: getStorage,
|
setStorage: setStorage,
|
removeStorage: removeStorage,
|
clearStorage: clearStorage,
|
getClipboardData: getClipboardData,
|
setClipboardData: setClipboardData
|
});
|
|
var wx = {
|
uploadFile: true,
|
downloadFile: true,
|
chooseImage: true,
|
previewImage: true,
|
getImageInfo: true,
|
saveImageToPhotosAlbum: true,
|
chooseVideo: true,
|
saveVideoToPhotosAlbum: true,
|
saveFile: true,
|
getSavedFileList: true,
|
getSavedFileInfo: true,
|
removeSavedFile: true,
|
openDocument: true,
|
setStorage: true,
|
getStorage: true,
|
getStorageInfo: true,
|
removeStorage: true,
|
clearStorage: true,
|
getLocation: true,
|
chooseLocation: true,
|
openLocation: true,
|
getSystemInfo: true,
|
getNetworkType: true,
|
makePhoneCall: true,
|
scanCode: true,
|
setScreenBrightness: true,
|
getScreenBrightness: true,
|
setKeepScreenOn: true,
|
vibrateLong: true,
|
vibrateShort: true,
|
addPhoneContact: true,
|
showToast: true,
|
showLoading: true,
|
hideToast: true,
|
hideLoading: true,
|
showModal: true,
|
showActionSheet: true,
|
setNavigationBarTitle: true,
|
setNavigationBarColor: true,
|
navigateTo: true,
|
redirectTo: true,
|
reLaunch: true,
|
switchTab: true,
|
navigateBack: true,
|
getProvider: true,
|
login: true,
|
getUserInfo: true,
|
share: true,
|
requestPayment: true,
|
subscribePush: true,
|
unsubscribePush: true,
|
onPush: true,
|
offPush: true
|
};
|
|
var uni = {};
|
|
var baseUni = {
|
os: {
|
nvue: true
|
}
|
};
|
|
if (typeof Proxy !== 'undefined') {
|
uni = new Proxy({}, {
|
get: function get(target, name) {
|
if (name === 'os') {
|
return {
|
nvue: true
|
};
|
}
|
if (name === 'postMessage') {
|
return postMessage;
|
}
|
if (name === 'requireNativePlugin') {
|
return requireNativePlugin;
|
}
|
if (name === 'onNavigationBarButtonTap') {
|
return onNavigationBarButtonTap;
|
}
|
if (name === 'onNavigationBarSearchInputChanged') {
|
return onNavigationBarSearchInputChanged;
|
}
|
if (name === 'onNavigationBarSearchInputConfirmed') {
|
return onNavigationBarSearchInputConfirmed;
|
}
|
if (name === 'onNavigationBarSearchInputClicked') {
|
return onNavigationBarSearchInputClicked;
|
}
|
var method = api[name];
|
if (!method) {
|
method = createPublish(name);
|
}
|
if (shouldPromise(name)) {
|
return promisify(method);
|
}
|
return method;
|
}
|
});
|
} else {
|
Object.keys(baseUni).forEach(function (key) {
|
uni[key] = baseUni[key];
|
});
|
|
uni.postMessage = postMessage;
|
|
uni.requireNativePlugin = requireNativePlugin;
|
|
uni.onNavigationBarButtonTap = onNavigationBarButtonTap;
|
|
uni.onNavigationBarSearchInputChanged = onNavigationBarSearchInputChanged;
|
|
uni.onNavigationBarSearchInputConfirmed = onNavigationBarSearchInputConfirmed;
|
|
uni.onNavigationBarSearchInputClicked = onNavigationBarSearchInputClicked;
|
|
Object.keys(wx).forEach(function (name) {
|
var method = api[name];
|
if (!method) {
|
method = createPublish(name);
|
}
|
if (shouldPromise(name)) {
|
uni[name] = promisify(method);
|
} else {
|
uni[name] = method;
|
}
|
});
|
}
|
|
var uni$1 = uni;
|
|
export default uni$1;
|