diff --git a/packages/uni-mp-alipay/dist/index.js b/packages/uni-mp-alipay/dist/index.js index fa3247819fb1989cfbdc897f51cc7218b0f70f10..8916f8377a8c4a52906c6ecf45fe5bef9b9ff66e 100644 --- a/packages/uni-mp-alipay/dist/index.js +++ b/packages/uni-mp-alipay/dist/index.js @@ -163,8 +163,8 @@ const protocols = { // 需要做转换的 API 列表 res.errMsg = `${methodName}:fail ${res.errorMessage || res.error}`; delete res.error; delete res.errorMessage; - } else { - res.errMsg = `${methodName}:ok`; + } else { + res.errMsg = `${methodName}:ok`; } return res }, @@ -385,6 +385,27 @@ const protocols = { // 需要做转换的 API 列表 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' + } } }; diff --git a/packages/uni-mp-alipay/package.json b/packages/uni-mp-alipay/package.json index 9a7261aaae832dcf3d39be851d4d3d50247169c7..bbd08088fe8dd63443b6703e5082af7f6d7aa335 100644 --- a/packages/uni-mp-alipay/package.json +++ b/packages/uni-mp-alipay/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-mp-alipay", - "version": "0.0.4", + "version": "0.0.5", "description": "uni-app mp-alipay", "main": "dist/index.js", "scripts": { diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index 8b0be351cd2fb2a4088ff399003c8c2f2bc8b583..b681c8613075abadaca7542e6d529aca6ebc30ee 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/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) } @@ -116,64 +121,72 @@ var protocols = {}; const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; -function processCallback (method, returnValue) { +function processCallback (methodName, method, returnValue) { return function (res) { - return method(processReturnValue(res, returnValue)) + return method(processReturnValue(methodName, res, returnValue)) } } -function processArgs (fromArgs, argsOption = {}, returnValue = {}) { +function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) { if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 - const toArgs = {}; - Object.keys(fromArgs).forEach(key => { + const toArgs = keepFromArgs === true ? fromArgs : {}; // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值 + if (isFn(argsOption)) { + argsOption = argsOption(fromArgs, toArgs) || {}; + } + for (let key in fromArgs) { if (hasOwn(argsOption, key)) { let keyOption = argsOption[key]; if (isFn(keyOption)) { - keyOption = keyOption(fromArgs[key], fromArgs); + keyOption = keyOption(fromArgs[key], fromArgs, toArgs); } if (!keyOption) { // 不支持的参数 - console.warn(`${微信小程序} ${name}暂不支持${key}`); + 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(fromArgs[key], returnValue); + 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(fromArgs, returnValue); + fromArgs = processCallback(methodName, fromArgs, returnValue); } return fromArgs } -function processReturnValue (res, returnValue) { - return processArgs(res, returnValue) +function processReturnValue (methodName, res, returnValue, keepReturnValue = false) { + if (isFn(protocols.returnValue)) { // 处理通用 returnValue + res = protocols.returnValue(methodName, res); + } + return processArgs(methodName, res, returnValue, {}, keepReturnValue) } -function wrapper (name, method) { - if (hasOwn(protocols, name)) { - const protocol = protocols[name]; +function wrapper (methodName, method) { + if (hasOwn(protocols, methodName)) { + const protocol = protocols[methodName]; if (!protocol) { // 暂不支持的 api return function () { - throw new Error(`${微信小程序}暂不支持${name}`) + console.error(`微信小程序 暂不支持${methodName}`); } } - return function (arg1, arg2) { // 目前 api 最多两个参数 + return function (arg1, arg2) { // 目前 api 最多两个参数 let options = protocol; if (isFn(protocol)) { options = protocol(arg1); } - arg1 = processArgs(arg1, options.args, options.returnValue); + arg1 = processArgs(methodName, arg1, options.args, options.returnValue); - const returnValue = wx[options.name || name](arg1, arg2); - if (isSyncApi(name)) { // 同步 api - return processReturnValue(returnValue, options.returnValue) + const returnValue = wx[options.name || methodName](arg1, arg2); + if (isSyncApi(methodName)) { // 同步 api + return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName)) } return returnValue } @@ -265,7 +278,7 @@ if (typeof Proxy !== 'undefined') { if (todoApis[name]) { return promisify(name, todoApis[name]) } - if (!wx.hasOwnProperty(name)) { + if (!hasOwn(wx, name) && !hasOwn(protocols, name)) { return } return promisify(name, wrapper(name, wx[name])) @@ -287,7 +300,7 @@ if (typeof Proxy !== 'undefined') { }); Object.keys(wx).forEach(name => { - if (wx.hasOwnProperty(name)) { + if (hasOwn(wx, name) || hasOwn(protocols, name)) { uni$1[name] = promisify(name, wrapper(name, wx[name])); } }); diff --git a/packages/uni-mp-weixin/package.json b/packages/uni-mp-weixin/package.json index 75c85ac9d68a9ff070a4f445920bb2cd3ace8ab4..43a4cd2da36f466f5231200f2e9b39622907be83 100644 --- a/packages/uni-mp-weixin/package.json +++ b/packages/uni-mp-weixin/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-mp-weixin", - "version": "0.0.2", + "version": "0.0.3", "description": "uni-app mp-weixin", "main": "dist/index.js", "scripts": {