// 不支持的 API 列表 const TODOS = [ 'saveImageToPhotosAlbum', 'getRecorderManager', 'getBackgroundAudioManager', 'createInnerAudioContext', 'chooseVideo', 'saveVideoToPhotosAlbum', 'createVideoContext', 'createCameraContext', 'createLivePlayerContext', 'openDocument', 'onMemoryWarning', 'startAccelerometer', 'startCompass', 'addPhoneContact', 'setTabBarItem', 'setTabBarStyle', 'hideTabBar', 'showTabBar', 'setTabBarBadge', 'removeTabBarBadge', 'showTabBarRedDot', 'hideTabBarRedDot', 'setBackgroundColor', 'setBackgroundTextStyle', 'startPullDownRefresh', 'createIntersectionObserver', 'authorize', 'openSetting', 'getSetting', 'chooseAddress', 'chooseInvoiceTitle', 'addTemplate', 'deleteTemplate', 'getTemplateLibraryById', 'getTemplateLibraryList', 'getTemplateList', 'sendTemplateMessage', 'getUpdateManager', 'setEnableDebug', 'getExtConfig', 'getExtConfigSync', 'onWindowResize', 'offWindowResize' ] function _handleNetworkInfo (result) { switch (result.networkType) { case 'NOTREACHABLE': result.networkType = 'none' break case 'WWAN': // TODO ? result.networkType = '3g' break default: result.networkType = result.networkType.toLowerCase() break } return {} } function _handleSystemInfo (result) { let platform = result.platform ? result.platform.toLowerCase() : 'devtools' if (!~['android', 'ios'].indexOf(platform)) { platform = 'devtools' } result.platform = platform } const protocols = { // 需要做转换的 API 列表 returnValue (methodName, res) { // 通用 returnValue 解析 if (res.error || res.errorMessage) { res.errMsg = `${methodName}:fail ${res.errorMessage || res.error}` delete res.error delete res.errorMessage } else { res.errMsg = `${methodName}:ok` } return res }, request: { name: 'httpRequest', args (fromArgs) { if (!fromArgs.header) { // 默认增加 header 参数,方便格式化 content-type fromArgs.header = {} } return { header (header = {}, toArgs) { const headers = { 'content-type': 'application/json' } Object.keys(header).forEach(key => { headers[key.toLocaleLowerCase()] = header[key] }) return { name: 'headers', value: headers } }, method: 'method', // TODO 支付宝小程序仅支持 get,post responseType: false } }, returnValue: { status: 'statusCode', headers: 'header' } }, 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' } }, showLoading: { args: { title: 'content', mask: false } }, uploadFile: { args: { name: 'fileName' } // 从测试结果看,是有返回对象的,文档上没有说明。 }, downloadFile: { returnValue: { apFilePath: 'tempFilePath' } }, connectSocket: { args: { method: false, protocols: false } // TODO 有没有返回值还需要测试下 }, chooseImage: { returnValue: { apFilePaths: 'tempFilePaths' } }, previewImage: { args (fromArgs) { // 支付宝小程序的 current 是索引值,而非图片地址。 if (fromArgs.current && Array.isArray(fromArgs.urls)) { const index = fromArgs.urls.indexOf(fromArgs.current) fromArgs.current = ~index ? index : 0 } return { indicator: false, loop: false } } }, saveFile: { args: { tempFilePath: 'apFilePath' }, returnValue: { apFilePath: 'savedFilePath' } }, getSavedFileInfo: { args: { filePath: 'apFilePath' }, returnValue (result) { if (result.fileList && result.fileList.length) { result.fileList.forEach(file => { file.filePath = file.apFilePath delete file.apFilePath }) } return {} } }, removeSavedFile: { args: { filePath: 'apFilePath' } }, getLocation: { args: { type: false, altitude: false } }, openLocation: { args: { // TODO address 参数在阿里上是必传的 } }, getNetworkType: { returnValue: _handleNetworkInfo }, onNetworkStatusChange: { returnValue: _handleNetworkInfo }, stopAccelerometer: { name: 'offAccelerometerChange' }, stopCompass: { name: 'offCompassChange' }, scanCode: { name: 'scan', args: { onlyFromCamera: 'hideAlbum', scanType: false }, returnValue: { code: 'result' } }, setClipboardData: { name: 'setClipboard', args: { data: 'text' } }, getClipboardData: { name: 'getClipboard', returnValue: { text: 'data' } }, pageScrollTo: { args: { duration: false } }, login: { name: 'getAuthCode', returnValue (result) { result.code = result.authCode } }, getUserInfo: { name: 'getAuthUserInfo', returnValue (result) { result.userInfo = { nickName: result.nickName, avatarUrl: result.avatar } } }, requestPayment: { name: 'tradePay', args: { orderInfo: 'orderStr' } }, getBLEDeviceServices: { returnValue (result) { result.services.forEach((item) => { item.uuid = item.serviceId }) } }, makePhoneCall: { args: { phoneNumber: 'number' } }, stopGyroscope: { name: 'offGyroscopeChange' }, getSystemInfo: { returnValue: _handleSystemInfo }, getSystemInfoSync: { returnValue: _handleSystemInfo }, // 文档没提到,但是实测可用。 canvasToTempFilePath: { returnValue (result) { // 真机的情况下会有 tempFilePath 这个值,因此需要主动修改。 result.tempFilePath = result.apFilePath } } } TODOS.forEach(todoApi => { protocols[todoApi] = false }) export default protocols