提交 b255a85b 编写于 作者: X xiaoyucoding

Revert "feat: mp-baidu api protocols"

This reverts commit b5dcf0e3.
上级 a6b4a1bc
...@@ -112,132 +112,68 @@ function upx2px (number, newDeviceWidth) { ...@@ -112,132 +112,68 @@ function upx2px (number, newDeviceWidth) {
return number return number
} }
// 不支持的 API 列表 var protocols = {};
const TODOS = [];
// 需要做转换的 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']; const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
function processCallback (methodName, method, returnValue) { function processCallback (method, returnValue) {
return function (res) { return function (res) {
return method(processReturnValue(methodName, res, returnValue)) return method(processReturnValue(res, returnValue))
} }
} }
function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { function processArgs (fromArgs, argsOption = {}, returnValue = {}) {
if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 if (isPlainObject(fromArgs)) { // 一般 api 的参数解析
const toArgs = {}; const toArgs = {};
if (isFn(argsOption)) {
argsOption = argsOption(fromArgs, toArgs) || {};
}
Object.keys(fromArgs).forEach(key => { Object.keys(fromArgs).forEach(key => {
if (hasOwn(argsOption, key)) { if (hasOwn(argsOption, key)) {
let keyOption = argsOption[key]; let keyOption = argsOption[key];
if (isFn(keyOption)) { if (isFn(keyOption)) {
keyOption = keyOption(fromArgs[key], fromArgs, toArgs); keyOption = keyOption(fromArgs[key], fromArgs);
} }
if (!keyOption) { // 不支持的参数 if (!keyOption) { // 不支持的参数
console.warn(`百度小程序 ${methodName}暂不支持${key}`); console.warn(`${百度小程序} ${name}暂不支持${key}`);
} else if (isStr(keyOption)) { // 重写参数 key } else if (isStr(keyOption)) { // 重写参数 key
toArgs[keyOption] = fromArgs[key]; toArgs[keyOption] = fromArgs[key];
} else if (isPlainObject(keyOption)) { // {name:newName,value:value}可重新指定参数 key:value } else if (isPlainObject(keyOption)) { // {name:newName,value:value}可重新指定参数 key:value
toArgs[keyOption.name ? keyOption.name : key] = keyOption.value; toArgs[keyOption.name ? keyOption.name : key] = keyOption.value;
} }
} else if (CALLBACKS.includes(key)) { } else if (CALLBACKS.includes(key)) {
toArgs[key] = processCallback(methodName, fromArgs[key], returnValue); toArgs[key] = processCallback(fromArgs[key], returnValue);
} else { } else {
toArgs[key] = fromArgs[key]; toArgs[key] = fromArgs[key];
} }
}); });
return toArgs return toArgs
} else if (isFn(fromArgs)) { } else if (isFn(fromArgs)) {
fromArgs = processCallback(methodName, fromArgs, returnValue); fromArgs = processCallback(fromArgs, returnValue);
} }
return fromArgs return fromArgs
} }
function processReturnValue (methodName, res, returnValue) { function processReturnValue (res, returnValue) {
if (isFn(protocols.returnValue)) { // 处理通用 returnValue return processArgs(res, returnValue)
res = protocols.returnValue(methodName, res);
}
return processArgs(methodName, res, returnValue)
} }
function wrapper (methodName, method) { function wrapper (name, method) {
if (hasOwn(protocols, methodName)) { if (hasOwn(protocols, name)) {
const protocol = protocols[methodName]; const protocol = protocols[name];
if (!protocol) { // 暂不支持的 api if (!protocol) { // 暂不支持的 api
return function () { return function () {
console.error(`百度小程序 暂不支持${methodName}`); throw new Error(`${百度小程序}暂不支持${name}`)
} }
} }
return function (arg1, arg2) { // 目前 api 最多两个参数 return function (arg1, arg2) { // 目前 api 最多两个参数
let options = protocol; let options = protocol;
if (isFn(protocol)) { if (isFn(protocol)) {
options = protocol(arg1); options = protocol(arg1);
} }
arg1 = processArgs(methodName, arg1, options.args, options.returnValue); arg1 = processArgs(arg1, options.args, options.returnValue);
const returnValue = swan[options.name || methodName](arg1, arg2); const returnValue = swan[options.name || name](arg1, arg2);
if (isSyncApi(methodName)) { // 同步 api if (isSyncApi(name)) { // 同步 api
return processReturnValue(methodName, returnValue, options.returnValue) return processReturnValue(returnValue, options.returnValue)
} }
return returnValue return returnValue
} }
...@@ -247,7 +183,7 @@ function wrapper (methodName, method) { ...@@ -247,7 +183,7 @@ function wrapper (methodName, method) {
const todoApis = Object.create(null); const todoApis = Object.create(null);
const TODOS$1 = [ const TODOS = [
'subscribePush', 'subscribePush',
'unsubscribePush', 'unsubscribePush',
'onPush', 'onPush',
...@@ -268,7 +204,7 @@ function createTodoApi (name) { ...@@ -268,7 +204,7 @@ function createTodoApi (name) {
} }
} }
TODOS$1.forEach(function (name) { TODOS.forEach(function (name) {
todoApis[name] = createTodoApi(name); todoApis[name] = createTodoApi(name);
}); });
...@@ -329,7 +265,7 @@ if (typeof Proxy !== 'undefined') { ...@@ -329,7 +265,7 @@ if (typeof Proxy !== 'undefined') {
if (todoApis[name]) { if (todoApis[name]) {
return promisify(name, todoApis[name]) return promisify(name, todoApis[name])
} }
if (!hasOwn(swan, name) && !hasOwn(protocols, name)) { if (!swan.hasOwnProperty(name)) {
return return
} }
return promisify(name, wrapper(name, swan[name])) return promisify(name, wrapper(name, swan[name]))
...@@ -351,7 +287,7 @@ if (typeof Proxy !== 'undefined') { ...@@ -351,7 +287,7 @@ if (typeof Proxy !== 'undefined') {
}); });
Object.keys(swan).forEach(name => { Object.keys(swan).forEach(name => {
if (hasOwn(swan, name) || hasOwn(protocols, name)) { if (swan.hasOwnProperty(name)) {
uni$1[name] = promisify(name, wrapper(name, swan[name])); uni$1[name] = promisify(name, wrapper(name, swan[name]));
} }
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册