提交 16da3b7b 编写于 作者: fxy060608's avatar fxy060608

支持配置 context,manager 类型 api 转换

上级 3ee221ef
...@@ -19,8 +19,13 @@ function hasOwn (obj, key) { ...@@ -19,8 +19,13 @@ function hasOwn (obj, key) {
const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/; const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/;
const CONTEXT_API_RE = /^create|Manager$/;
const CALLBACK_API_RE = /^on/; const CALLBACK_API_RE = /^on/;
function isContextApi (name) {
return CONTEXT_API_RE.test(name)
}
function isSyncApi (name) { function isSyncApi (name) {
return SYNC_API_RE.test(name) return SYNC_API_RE.test(name)
} }
...@@ -112,66 +117,67 @@ function upx2px (number, newDeviceWidth) { ...@@ -112,66 +117,67 @@ function upx2px (number, newDeviceWidth) {
return number return number
} }
// 不支持的 API 列表 // 不支持的 API 列表
const TODOS = [ const TODOS = [
'hideKeyboard' 'hideKeyboard'
]; ];
// 需要做转换的 API 列表 function createTodoMethod (contextName, methodName) {
const protocols = { return function unsupported () {
request: { console.error(`百度小程序 ${contextName}暂不支持${methodName}`);
args (fromArgs) { }
// TODO }
// data 不支持 ArrayBuffer // 需要做转换的 API 列表
// method 不支持 TRACE, CONNECT const protocols = {
// dataType 可取值为 string/json request: {
return fromArgs args (fromArgs) {
} // TODO
}, // data 不支持 ArrayBuffer
connectSocket: { // method 不支持 TRACE, CONNECT
args: { // dataType 可取值为 string/json
method: false return fromArgs
} }
}, },
previewImage: { connectSocket: {
args: { args: {
indicator: false, method: false
loop: false }
} },
}, previewImage: {
getRecorderManager: { args: {
returnValue: { indicator: false,
onFrameRecorded: false loop: false
// TODO start 方法的参数有差异,暂时没有提供配置处理。 }
} },
}, getRecorderManager: {
getBackgroundAudioManager: { returnValue (fromRet) {
returnValue: { fromRet.onFrameRecorded = createTodoMethod('RecorderManager', 'onFrameRecorded');
buffered: false, }
webUrl: false, },
protocol: false, getBackgroundAudioManager: {
onPrev: false, returnValue (fromRet) {
onNext: false fromRet.onPrev = createTodoMethod('BackgroundAudioManager', 'onPrev');
} fromRet.onNext = createTodoMethod('BackgroundAudioManager', 'onNext');
}, }
createInnerAudioContext: { },
returnValue: { createInnerAudioContext: {
buffered: false returnValue: {
} buffered: false
}, }
createVideoContext: { },
returnValue: { createVideoContext: {
playbackRate: false returnValue: {
} playbackRate: false
}, }
scanCode: { },
onlyFromCamera: false, scanCode: {
scanType: false onlyFromCamera: false,
} scanType: false
}; }
};
TODOS.forEach(todoApi => {
protocols[todoApi] = false; TODOS.forEach(todoApi => {
protocols[todoApi] = false;
}); });
const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
...@@ -182,13 +188,13 @@ function processCallback (methodName, method, returnValue) { ...@@ -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 的参数解析 if (isPlainObject(fromArgs)) { // 一般 api 的参数解析
const toArgs = {}; const toArgs = keepFromArgs === true ? fromArgs : {}; // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值
if (isFn(argsOption)) { if (isFn(argsOption)) {
argsOption = argsOption(fromArgs, toArgs) || {}; argsOption = argsOption(fromArgs, toArgs) || {};
} }
Object.keys(fromArgs).forEach(key => { for (let key in fromArgs) {
if (hasOwn(argsOption, key)) { if (hasOwn(argsOption, key)) {
let keyOption = argsOption[key]; let keyOption = argsOption[key];
if (isFn(keyOption)) { if (isFn(keyOption)) {
...@@ -204,9 +210,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { ...@@ -204,9 +210,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) {
} else if (CALLBACKS.includes(key)) { } else if (CALLBACKS.includes(key)) {
toArgs[key] = processCallback(methodName, fromArgs[key], returnValue); toArgs[key] = processCallback(methodName, fromArgs[key], returnValue);
} else { } else {
toArgs[key] = fromArgs[key]; if (!keepFromArgs) {
toArgs[key] = fromArgs[key];
}
} }
}); }
return toArgs return toArgs
} else if (isFn(fromArgs)) { } else if (isFn(fromArgs)) {
fromArgs = processCallback(methodName, fromArgs, returnValue); fromArgs = processCallback(methodName, fromArgs, returnValue);
...@@ -214,11 +222,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { ...@@ -214,11 +222,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) {
return fromArgs return fromArgs
} }
function processReturnValue (methodName, res, returnValue) { function processReturnValue (methodName, res, returnValue, keepReturnValue = false) {
if (isFn(protocols.returnValue)) { // 处理通用 returnValue if (isFn(protocols.returnValue)) { // 处理通用 returnValue
res = protocols.returnValue(methodName, res); res = protocols.returnValue(methodName, res);
} }
return processArgs(methodName, res, returnValue) return processArgs(methodName, res, returnValue, {}, keepReturnValue)
} }
function wrapper (methodName, method) { function wrapper (methodName, method) {
...@@ -239,7 +247,7 @@ function wrapper (methodName, method) { ...@@ -239,7 +247,7 @@ function wrapper (methodName, method) {
const returnValue = swan[options.name || methodName](arg1, arg2); const returnValue = swan[options.name || methodName](arg1, arg2);
if (isSyncApi(methodName)) { // 同步 api if (isSyncApi(methodName)) { // 同步 api
return processReturnValue(methodName, returnValue, options.returnValue) return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName))
} }
return returnValue return returnValue
} }
......
...@@ -4,10 +4,15 @@ import { ...@@ -4,10 +4,15 @@ import {
const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/ const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/
const CONTEXT_API_RE = /^create|Manager$/
const TASK_APIS = ['request', 'downloadFile', 'uploadFile', 'connectSocket'] const TASK_APIS = ['request', 'downloadFile', 'uploadFile', 'connectSocket']
const CALLBACK_API_RE = /^on/ const CALLBACK_API_RE = /^on/
export function isContextApi (name) {
return CONTEXT_API_RE.test(name)
}
export function isSyncApi (name) { export function isSyncApi (name) {
return SYNC_API_RE.test(name) return SYNC_API_RE.test(name)
} }
......
...@@ -6,7 +6,8 @@ import { ...@@ -6,7 +6,8 @@ import {
} from 'uni-shared' } from 'uni-shared'
import { import {
isSyncApi isSyncApi,
isContextApi
} from '../helpers/promise' } from '../helpers/promise'
import protocols from 'uni-platform/service/api/protocols' import protocols from 'uni-platform/service/api/protocols'
...@@ -19,13 +20,13 @@ function processCallback (methodName, method, returnValue) { ...@@ -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 的参数解析 if (isPlainObject(fromArgs)) { // 一般 api 的参数解析
const toArgs = {} const toArgs = keepFromArgs === true ? fromArgs : {} // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值
if (isFn(argsOption)) { if (isFn(argsOption)) {
argsOption = argsOption(fromArgs, toArgs) || {} argsOption = argsOption(fromArgs, toArgs) || {}
} }
Object.keys(fromArgs).forEach(key => { for (let key in fromArgs) {
if (hasOwn(argsOption, key)) { if (hasOwn(argsOption, key)) {
let keyOption = argsOption[key] let keyOption = argsOption[key]
if (isFn(keyOption)) { if (isFn(keyOption)) {
...@@ -41,9 +42,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { ...@@ -41,9 +42,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) {
} else if (CALLBACKS.includes(key)) { } else if (CALLBACKS.includes(key)) {
toArgs[key] = processCallback(methodName, fromArgs[key], returnValue) toArgs[key] = processCallback(methodName, fromArgs[key], returnValue)
} else { } else {
toArgs[key] = fromArgs[key] if (!keepFromArgs) {
toArgs[key] = fromArgs[key]
}
} }
}) }
return toArgs return toArgs
} else if (isFn(fromArgs)) { } else if (isFn(fromArgs)) {
fromArgs = processCallback(methodName, fromArgs, returnValue) fromArgs = processCallback(methodName, fromArgs, returnValue)
...@@ -51,11 +54,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { ...@@ -51,11 +54,11 @@ function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) {
return fromArgs return fromArgs
} }
function processReturnValue (methodName, res, returnValue) { function processReturnValue (methodName, res, returnValue, keepReturnValue = false) {
if (isFn(protocols.returnValue)) { // 处理通用 returnValue if (isFn(protocols.returnValue)) { // 处理通用 returnValue
res = protocols.returnValue(methodName, res) res = protocols.returnValue(methodName, res)
} }
return processArgs(methodName, res, returnValue) return processArgs(methodName, res, returnValue, {}, keepReturnValue)
} }
export default function wrapper (methodName, method) { export default function wrapper (methodName, method) {
...@@ -76,7 +79,7 @@ export default function wrapper (methodName, method) { ...@@ -76,7 +79,7 @@ export default function wrapper (methodName, method) {
const returnValue = __GLOBAL__[options.name || methodName](arg1, arg2) const returnValue = __GLOBAL__[options.name || methodName](arg1, arg2)
if (isSyncApi(methodName)) { // 同步 api if (isSyncApi(methodName)) { // 同步 api
return processReturnValue(methodName, returnValue, options.returnValue) return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName))
} }
return returnValue return returnValue
} }
......
// 不支持的 API 列表 // 不支持的 API 列表
const TODOS = [ const TODOS = [
'hideKeyboard' 'hideKeyboard'
] ]
// 需要做转换的 API 列表 function createTodoMethod (contextName, methodName) {
const protocols = { return function unsupported () {
request: { console.error(`__PLATFORM_TITLE__ ${contextName}暂不支持${methodName}`)
args (fromArgs) { }
// TODO }
// data 不支持 ArrayBuffer // 需要做转换的 API 列表
// method 不支持 TRACE, CONNECT const protocols = {
// dataType 可取值为 string/json request: {
return fromArgs args (fromArgs) {
} // TODO
}, // data 不支持 ArrayBuffer
connectSocket: { // method 不支持 TRACE, CONNECT
args: { // dataType 可取值为 string/json
method: false return fromArgs
} }
}, },
previewImage: { connectSocket: {
args: { args: {
indicator: false, method: false
loop: false }
} },
}, previewImage: {
getRecorderManager: { args: {
returnValue: { indicator: false,
onFrameRecorded: false loop: false
// TODO start 方法的参数有差异,暂时没有提供配置处理。 }
} },
}, getRecorderManager: {
getBackgroundAudioManager: { returnValue (fromRet) {
returnValue: { fromRet.onFrameRecorded = createTodoMethod('RecorderManager', 'onFrameRecorded')
buffered: false, }
webUrl: false, },
protocol: false, getBackgroundAudioManager: {
onPrev: false, returnValue (fromRet) {
onNext: false fromRet.onPrev = createTodoMethod('BackgroundAudioManager', 'onPrev')
} fromRet.onNext = createTodoMethod('BackgroundAudioManager', 'onNext')
}, }
createInnerAudioContext: { },
returnValue: { createInnerAudioContext: {
buffered: false returnValue: {
} buffered: false
}, }
createVideoContext: { },
returnValue: { createVideoContext: {
playbackRate: false returnValue: {
} playbackRate: false
}, }
scanCode: { },
onlyFromCamera: false, scanCode: {
scanType: false onlyFromCamera: false,
} scanType: false
} }
}
TODOS.forEach(todoApi => {
protocols[todoApi] = false TODOS.forEach(todoApi => {
}) protocols[todoApi] = false
})
export default protocols export default protocols
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册