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 } const TODOS = [ 'hideTabBar', 'hideTabBarRedDot', 'removeTabBarBadge', 'setTabBarBadge', 'setTabBarItem', 'setTabBarStyle', 'showTabBar', 'showTabBarRedDot' ]; const protocols = { returnValue (methodName, res) { // 通用 returnValue 解析 if (res.error || res.errorMessage) { res.errMsg = `${methodName}:fail ${res.errorMessage || res.error}`; delete res.error; delete res.errorMessage; } return res }, setNavigationBarColor: { name: 'setNavigationBar', args: { frontColor: false, animation: false } }, setNavigationBarTitle: { name: 'setNavigationBar' }, showModal ({ showCancel = true } = {}) { if (showCancel) { return { name: 'confirm', args: { cancelColor: false, confirmColor: false, cancelText: 'cancelButtonText', confirmText: 'confirmButtonText' }, returnValue (fromRes, toRes) { toRes.confirm = fromRes.confirm; toRes.cancel = !fromRes.confirm; } } } return { name: 'alert', args: { confirmColor: false, confirmText: 'buttonText' }, returnValue (fromRes, toRes) { toRes.confirm = true; toRes.cancel = false; } } }, showToast ({ icon = 'success' } = {}) { const args = { title: 'content', icon: 'type', duration: false, image: false, mask: false }; if (icon === 'loading') { return { name: 'showLoading', args } } return { name: 'showToast', args } }, showActionSheet: { name: 'showActionSheet', args: { itemList: 'items', itemColor: false }, returnValue: { index: 'tapIndex' } } }; 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); } 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 () { throw new 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 = my[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: ['alipay'], share: ['alipay'], payment: ['alipay'], push: ['alipay'] }; 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(my, name) && !hasOwn(protocols, name)) { return } return promisify(name, wrapper(name, my[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(my).forEach(name => { if (hasOwn(my, name) || hasOwn(protocols, name)) { uni$1[name] = promisify(name, wrapper(name, my[name])); } }); } var uni$2 = uni$1; export default uni$2;