const _toString = Object.prototype.toString; const hasOwnProperty = Object.prototype.hasOwnProperty; function isFn (fn) { return typeof fn === 'function' } function isStr (str) { return typeof str === 'string' } function isPlainObject (obj) { return _toString.call(obj) === '[object Object]' } function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/; const CALLBACK_API_RE = /^on/; function isSyncApi (name) { return SYNC_API_RE.test(name) } function isCallbackApi (name) { return CALLBACK_API_RE.test(name) } function handlePromise (promise) { return promise.then(data => { return [null, data] }) .catch(err => [err]) } function shouldPromise (name) { if (isSyncApi(name)) { return false } if (isCallbackApi(name)) { return false } return true } function promisify (name, api) { if (!shouldPromise(name)) { return api } return function promiseApi (options = {}, ...params) { if (isFn(options.success) || isFn(options.fail) || isFn(options.complete)) { return api(options, ...params) } return handlePromise(new Promise((resolve, reject) => { api(Object.assign({}, options, { success: resolve, fail: reject }), ...params); /* eslint-disable no-extend-native */ Promise.prototype.finally = function (callback) { const promise = this.constructor; return this.then( value => promise.resolve(callback()).then(() => value), reason => promise.resolve(callback()).then(() => { throw reason }) ) }; })) } } const EPS = 1e-4; const BASE_DEVICE_WIDTH = 750; let isIOS = false; let deviceWidth = 0; let deviceDPR = 0; function checkDeviceWidth () { const { platform, pixelRatio, windowWidth } = uni.getSystemInfoSync(); deviceWidth = windowWidth; deviceDPR = pixelRatio; isIOS = platform === 'ios'; } function upx2px (number, newDeviceWidth) { if (deviceWidth === 0) { checkDeviceWidth(); } number = Number(number); if (number === 0) { return 0 } number = (number / BASE_DEVICE_WIDTH) * (newDeviceWidth || deviceWidth); number = Math.floor(number + EPS); if (number === 0) { if (deviceDPR === 1 || !isIOS) { return 1 } else { return 0.5 } } return number } // 不支持的 API 列表 const TODOS = [ 'hideKeyboard' ]; // 需要做转换的 API 列表 const protocols = { request: { args (fromArgs) { // TODO // data 不支持 ArrayBuffer // method 不支持 TRACE, CONNECT // dataType 可取值为 string/json return fromArgs } }, connectSocket: { args: { method: false } }, previewImage: { args: { indicator: false, loop: false } }, getRecorderManager: { returnValue: { onFrameRecorded: false // TODO start 方法的参数有差异,暂时没有提供配置处理。 } }, getBackgroundAudioManager: { returnValue: { buffered: false, webUrl: false, protocol: false, onPrev: false, onNext: false } }, createInnerAudioContext: { returnValue: { buffered: false } }, createVideoContext: { returnValue: { playbackRate: false } }, scanCode: { onlyFromCamera: false, scanType: false } }; TODOS.forEach(todoApi => { protocols[todoApi] = false; }); const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; function processCallback (methodName, method, returnValue) { return function (res) { return method(processReturnValue(methodName, res, returnValue)) } } function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 const toArgs = {}; if (isFn(argsOption)) { argsOption = argsOption(fromArgs, toArgs) || {}; } Object.keys(fromArgs).forEach(key => { if (hasOwn(argsOption, key)) { let keyOption = argsOption[key]; if (isFn(keyOption)) { keyOption = keyOption(fromArgs[key], fromArgs, toArgs); } if (!keyOption) { // 不支持的参数 console.warn(`百度小程序 ${methodName}暂不支持${key}`); } else if (isStr(keyOption)) { // 重写参数 key toArgs[keyOption] = fromArgs[key]; } else if (isPlainObject(keyOption)) { // {name:newName,value:value}可重新指定参数 key:value toArgs[keyOption.name ? keyOption.name : key] = keyOption.value; } } else if (CALLBACKS.includes(key)) { toArgs[key] = processCallback(methodName, fromArgs[key], returnValue); } else { toArgs[key] = fromArgs[key]; } }); return toArgs } else if (isFn(fromArgs)) { fromArgs = processCallback(methodName, fromArgs, returnValue); } return fromArgs } function processReturnValue (methodName, res, returnValue) { if (isFn(protocols.returnValue)) { // 处理通用 returnValue res = protocols.returnValue(methodName, res); } return processArgs(methodName, res, returnValue) } function wrapper (methodName, method) { if (hasOwn(protocols, methodName)) { const protocol = protocols[methodName]; if (!protocol) { // 暂不支持的 api return function () { console.error(`百度小程序 暂不支持${methodName}`); } } return function (arg1, arg2) { // 目前 api 最多两个参数 let options = protocol; if (isFn(protocol)) { options = protocol(arg1); } arg1 = processArgs(methodName, arg1, options.args, options.returnValue); const returnValue = swan[options.name || methodName](arg1, arg2); if (isSyncApi(methodName)) { // 同步 api return processReturnValue(methodName, returnValue, options.returnValue) } return returnValue } } return method } const todoApis = Object.create(null); const TODOS$1 = [ 'subscribePush', 'unsubscribePush', 'onPush', 'offPush', 'share' ]; function createTodoApi (name) { return function todoApi ({ fail, complete }) { const res = { errMsg: `${name}:fail:暂不支持 ${name} 方法` }; isFn(fail) && fail(res); isFn(complete) && complete(res); } } TODOS$1.forEach(function (name) { todoApis[name] = createTodoApi(name); }); var providers = { oauth: ['baidu'], share: ['baidu'], payment: ['baidu'], push: ['baidu'] }; function getProvider ({ service, success, fail, complete }) { let res = false; if (providers[service]) { res = { errMsg: 'getProvider:ok', service, provider: providers[service] }; isFn(success) && success(res); } else { res = { errMsg: 'getProvider:fail:服务[' + service + ']不存在' }; isFn(fail) && fail(res); } isFn(complete) && complete(res); } var extraApi = /*#__PURE__*/Object.freeze({ getProvider: getProvider }); var api = /*#__PURE__*/Object.freeze({ }); let uni$1 = {}; if (typeof Proxy !== 'undefined') { uni$1 = new Proxy({}, { get (target, name) { if (name === 'upx2px') { return upx2px } if (api[name]) { return promisify(name, api[name]) } if (extraApi[name]) { return promisify(name, extraApi[name]) } if (todoApis[name]) { return promisify(name, todoApis[name]) } if (!hasOwn(swan, name) && !hasOwn(protocols, name)) { return } return promisify(name, wrapper(name, swan[name])) } }); } else { uni$1.upx2px = upx2px; Object.keys(todoApis).forEach(name => { uni$1[name] = promisify(name, todoApis[name]); }); Object.keys(extraApi).forEach(name => { uni$1[name] = promisify(name, todoApis[name]); }); Object.keys(api).forEach(name => { uni$1[name] = promisify(name, api[name]); }); Object.keys(swan).forEach(name => { if (hasOwn(swan, name) || hasOwn(protocols, name)) { uni$1[name] = promisify(name, wrapper(name, swan[name])); } }); } var uni$2 = uni$1; export default uni$2;