From 16da3b7ba1de53e51fbd48443183b124cfe84a22 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 12 Dec 2018 20:32:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AE=20context,?= =?UTF-8?q?manager=20=E7=B1=BB=E5=9E=8B=20api=20=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-mp-baidu/dist/index.js | 144 +++++++++--------- src/core/helpers/promise.js | 5 + src/core/runtime/wrapper.js | 21 +-- .../mp-baidu/service/api/protocols.js | 125 +++++++-------- 4 files changed, 156 insertions(+), 139 deletions(-) diff --git a/packages/uni-mp-baidu/dist/index.js b/packages/uni-mp-baidu/dist/index.js index 0f08319fc..e0041b116 100644 --- a/packages/uni-mp-baidu/dist/index.js +++ b/packages/uni-mp-baidu/dist/index.js @@ -19,8 +19,13 @@ function hasOwn (obj, key) { const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/; +const CONTEXT_API_RE = /^create|Manager$/; + const CALLBACK_API_RE = /^on/; +function isContextApi (name) { + return CONTEXT_API_RE.test(name) +} function isSyncApi (name) { return SYNC_API_RE.test(name) } @@ -112,66 +117,67 @@ function upx2px (number, newDeviceWidth) { 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; +// 不支持的 API 列表 +const TODOS = [ + 'hideKeyboard' +]; + +function createTodoMethod (contextName, methodName) { + return function unsupported () { + console.error(`百度小程序 ${contextName}暂不支持${methodName}`); + } +} +// 需要做转换的 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 (fromRet) { + fromRet.onFrameRecorded = createTodoMethod('RecorderManager', 'onFrameRecorded'); + } + }, + getBackgroundAudioManager: { + returnValue (fromRet) { + fromRet.onPrev = createTodoMethod('BackgroundAudioManager', 'onPrev'); + fromRet.onNext = createTodoMethod('BackgroundAudioManager', 'onNext'); + } + }, + 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']; @@ -182,13 +188,13 @@ function processCallback (methodName, method, returnValue) { } } -function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { +function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) { if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 - const toArgs = {}; + const toArgs = keepFromArgs === true ? fromArgs : {}; // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值 if (isFn(argsOption)) { argsOption = argsOption(fromArgs, toArgs) || {}; } - Object.keys(fromArgs).forEach(key => { + for (let key in fromArgs) { if (hasOwn(argsOption, key)) { let keyOption = argsOption[key]; if (isFn(keyOption)) { @@ -204,9 +210,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { } else if (CALLBACKS.includes(key)) { toArgs[key] = processCallback(methodName, fromArgs[key], returnValue); } else { - toArgs[key] = fromArgs[key]; + if (!keepFromArgs) { + toArgs[key] = fromArgs[key]; + } } - }); + } return toArgs } else if (isFn(fromArgs)) { fromArgs = processCallback(methodName, fromArgs, returnValue); @@ -214,11 +222,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { return fromArgs } -function processReturnValue (methodName, res, returnValue) { +function processReturnValue (methodName, res, returnValue, keepReturnValue = false) { if (isFn(protocols.returnValue)) { // 处理通用 returnValue res = protocols.returnValue(methodName, res); } - return processArgs(methodName, res, returnValue) + return processArgs(methodName, res, returnValue, {}, keepReturnValue) } function wrapper (methodName, method) { @@ -239,7 +247,7 @@ function wrapper (methodName, method) { const returnValue = swan[options.name || methodName](arg1, arg2); if (isSyncApi(methodName)) { // 同步 api - return processReturnValue(methodName, returnValue, options.returnValue) + return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName)) } return returnValue } diff --git a/src/core/helpers/promise.js b/src/core/helpers/promise.js index 860d74f7e..b431dae4c 100644 --- a/src/core/helpers/promise.js +++ b/src/core/helpers/promise.js @@ -4,10 +4,15 @@ import { const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/ +const CONTEXT_API_RE = /^create|Manager$/ + const TASK_APIS = ['request', 'downloadFile', 'uploadFile', 'connectSocket'] const CALLBACK_API_RE = /^on/ +export function isContextApi (name) { + return CONTEXT_API_RE.test(name) +} export function isSyncApi (name) { return SYNC_API_RE.test(name) } diff --git a/src/core/runtime/wrapper.js b/src/core/runtime/wrapper.js index d79f28a3c..8b33a4be5 100644 --- a/src/core/runtime/wrapper.js +++ b/src/core/runtime/wrapper.js @@ -6,7 +6,8 @@ import { } from 'uni-shared' import { - isSyncApi + isSyncApi, + isContextApi } from '../helpers/promise' import protocols from 'uni-platform/service/api/protocols' @@ -19,13 +20,13 @@ function processCallback (methodName, method, returnValue) { } } -function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { +function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) { if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 - const toArgs = {} + const toArgs = keepFromArgs === true ? fromArgs : {} // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值 if (isFn(argsOption)) { argsOption = argsOption(fromArgs, toArgs) || {} } - Object.keys(fromArgs).forEach(key => { + for (let key in fromArgs) { if (hasOwn(argsOption, key)) { let keyOption = argsOption[key] if (isFn(keyOption)) { @@ -41,9 +42,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { } else if (CALLBACKS.includes(key)) { toArgs[key] = processCallback(methodName, fromArgs[key], returnValue) } else { - toArgs[key] = fromArgs[key] + if (!keepFromArgs) { + toArgs[key] = fromArgs[key] + } } - }) + } return toArgs } else if (isFn(fromArgs)) { fromArgs = processCallback(methodName, fromArgs, returnValue) @@ -51,11 +54,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { return fromArgs } -function processReturnValue (methodName, res, returnValue) { +function processReturnValue (methodName, res, returnValue, keepReturnValue = false) { if (isFn(protocols.returnValue)) { // 处理通用 returnValue res = protocols.returnValue(methodName, res) } - return processArgs(methodName, res, returnValue) + return processArgs(methodName, res, returnValue, {}, keepReturnValue) } export default function wrapper (methodName, method) { @@ -76,7 +79,7 @@ export default function wrapper (methodName, method) { const returnValue = __GLOBAL__[options.name || methodName](arg1, arg2) if (isSyncApi(methodName)) { // 同步 api - return processReturnValue(methodName, returnValue, options.returnValue) + return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName)) } return returnValue } diff --git a/src/platforms/mp-baidu/service/api/protocols.js b/src/platforms/mp-baidu/service/api/protocols.js index 10707f781..bee6bd9f3 100644 --- a/src/platforms/mp-baidu/service/api/protocols.js +++ b/src/platforms/mp-baidu/service/api/protocols.js @@ -1,63 +1,64 @@ -// 不支持的 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 -}) - +// 不支持的 API 列表 +const TODOS = [ + 'hideKeyboard' +] + +function createTodoMethod (contextName, methodName) { + return function unsupported () { + console.error(`__PLATFORM_TITLE__ ${contextName}暂不支持${methodName}`) + } +} +// 需要做转换的 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 (fromRet) { + fromRet.onFrameRecorded = createTodoMethod('RecorderManager', 'onFrameRecorded') + } + }, + getBackgroundAudioManager: { + returnValue (fromRet) { + fromRet.onPrev = createTodoMethod('BackgroundAudioManager', 'onPrev') + fromRet.onNext = createTodoMethod('BackgroundAudioManager', 'onNext') + } + }, + createInnerAudioContext: { + returnValue: { + buffered: false + } + }, + createVideoContext: { + returnValue: { + playbackRate: false + } + }, + scanCode: { + onlyFromCamera: false, + scanType: false + } +} + +TODOS.forEach(todoApi => { + protocols[todoApi] = false +}) + export default protocols -- GitLab