From b4deeaa43d5be670320b5f0379844e358d8916e6 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 10 Dec 2018 15:12:25 +0800 Subject: [PATCH] add mp-alipay api wrapper --- build/rollup.config.js | 22 +- package.json | 9 +- packages/uni-mp-alipay/dist/index.js | 205 +++++++++++++++++- packages/uni-mp-baidu/dist/index.js | 96 +++++++- packages/uni-mp-weixin/dist/index.js | 96 +++++++- src/core/helpers/promise.js | 2 +- src/core/runtime/{base.js => extra.js} | 0 src/core/runtime/index.js | 24 +- src/core/runtime/wrapper.js | 85 ++++++++ .../mp-alipay/service/api/protocols.js | 98 +++++++++ .../mp-baidu/service/api/protocols.js | 1 + .../mp-weixin/service/api/protocols.js | 1 + src/shared/util.js | 7 +- 13 files changed, 605 insertions(+), 41 deletions(-) rename src/core/runtime/{base.js => extra.js} (100%) create mode 100644 src/core/runtime/wrapper.js create mode 100644 src/platforms/mp-alipay/service/api/protocols.js create mode 100644 src/platforms/mp-baidu/service/api/protocols.js create mode 100644 src/platforms/mp-weixin/service/api/protocols.js diff --git a/build/rollup.config.js b/build/rollup.config.js index a4f660320..7ae516753 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -2,11 +2,22 @@ const path = require('path') const alias = require('rollup-plugin-alias') const replace = require('rollup-plugin-replace') -const GLOBAL = { - 'mp-weixin': 'wx', - 'mp-alipay': 'my', - 'mp-baidu': 'swan' +const PLATFORMS = { + 'mp-weixin': { + prefix: 'wx', + title: '微信小程序' + }, + 'mp-alipay': { + prefix: 'my', + title: '支付宝小程序' + }, + 'mp-baidu': { + prefix: 'swan', + title: '百度小程序' + } } + +const platform = PLATFORMS[process.env.UNI_PLATFORM] module.exports = { input: 'src/core/runtime/index.js', output: { @@ -19,7 +30,8 @@ module.exports = { 'uni-platform': path.resolve(__dirname, '../src/platforms/' + process.env.UNI_PLATFORM) }), replace({ - __GLOBAL__: GLOBAL[process.env.UNI_PLATFORM] + __GLOBAL__: platform.prefix, + __PLATFORM_TITLE__: platform.title }) ] } diff --git a/package.json b/package.json index bac880df8..47df25bf7 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,9 @@ "lint": "eslint --fix --config package.json --ext .js --ext .vue --ignore-path .eslintignore build src", "dev:h5": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=true UNI_PLATFORM=h5 node build/build.js", "build:h5": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=false UNI_PLATFORM=h5 node build/build.js", - "build:mp-weixin": "npm run lint && cross-env UNI_PLATFORM=mp-weixin rollup -c build/rollup.config.js", - "build:mp-baidu": "npm run lint && cross-env UNI_PLATFORM=mp-baidu rollup -c build/rollup.config.js", - "build:mp-alipay": "npm run lint && cross-env UNI_PLATFORM=mp-alipay rollup -c build/rollup.config.js", + "build:mp-weixin": "npm run lint && cross-env UNI_PLATFORM=mp-weixin rollup -c build/rollup.config.js", + "build:mp-baidu": "npm run lint && cross-env UNI_PLATFORM=mp-baidu rollup -c build/rollup.config.js", + "build:mp-alipay": "npm run lint && cross-env UNI_PLATFORM=mp-alipay rollup -c build/rollup.config.js", "test:unit": "cross-env NODE_ENV=test UNI_PLATFORM=h5 mocha-webpack --require tests/unit/setup.js --webpack-config build/webpack.config.test.js tests/unit/**/*.spec.js" }, "dependencies": {}, @@ -64,7 +64,8 @@ "UniServiceJSBridge": true, "__PLATFORM__": true, "__VERSION__": true, - "__GLOBAL__": true + "__GLOBAL__": true, + "__PLATFORM_TITLE__": true }, "rules": { "no-tabs": 0, diff --git a/packages/uni-mp-alipay/dist/index.js b/packages/uni-mp-alipay/dist/index.js index eda5d20f8..87df9f297 100644 --- a/packages/uni-mp-alipay/dist/index.js +++ b/packages/uni-mp-alipay/dist/index.js @@ -1,5 +1,20 @@ +const _toString = Object.prototype.toString; +const hasOwnProperty = Object.prototype.hasOwnProperty; + function isFn (fn) { return typeof fn === 'function' +} + +function isStr (str) { + return typeof str === 'string' +} + +function isPlainObject (obj) { + return _toString.call(obj) === '[object Object]' +} + +function hasOwn (obj, key) { + return hasOwnProperty.call(obj, key) } const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/; @@ -97,9 +112,179 @@ function upx2px (number, newDeviceWidth) { return number } +const TODOS = [ + 'hideTabBar', + 'hideTabBarRedDot', + 'removeTabBarBadge', + 'setTabBarBadge', + 'setTabBarItem', + 'setTabBarStyle', + 'showTabBar', + 'showTabBarRedDot' +]; + +const protocols = { + returnValue (methodName, res) { // 通用 returnValue 解析 + if (res.error || res.errorMessage) { + res.errMsg = `${methodName}:fail ${res.errorMessage || res.error}`; + delete res.error; + delete res.errorMessage; + } + return res + }, + 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' + } + } +}; + +TODOS.forEach(todoApi => { + protocols[todoApi] = false; +}); + +const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; + +function processCallback (methodName, method, returnValue) { + return function (res) { + return method(processReturnValue(methodName, res, returnValue)) + } +} + +function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { + if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 + const toArgs = {}; + if (isFn(argsOption)) { + argsOption = argsOption(fromArgs, toArgs) || {}; + } + Object.keys(fromArgs).forEach(key => { + if (hasOwn(argsOption, key)) { + let keyOption = argsOption[key]; + if (isFn(keyOption)) { + keyOption = keyOption(fromArgs[key], fromArgs); + } + if (!keyOption) { // 不支持的参数 + 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(methodName, fromArgs[key], returnValue); + } else { + toArgs[key] = fromArgs[key]; + } + }); + return toArgs + } else if (isFn(fromArgs)) { + fromArgs = processCallback(methodName, fromArgs, returnValue); + } + return fromArgs +} + +function processReturnValue (methodName, res, returnValue) { + if (isFn(protocols.returnValue)) { // 处理通用 returnValue + res = protocols.returnValue(methodName, res); + } + return processArgs(methodName, res, returnValue) +} + +function wrapper (methodName, method) { + if (hasOwn(protocols, methodName)) { + const protocol = protocols[methodName]; + if (!protocol) { // 暂不支持的 api + return function () { + throw new Error(`支付宝小程序 暂不支持${methodName}`) + } + } + return function (arg1, arg2) { // 目前 api 最多两个参数 + let options = protocol; + if (isFn(protocol)) { + options = protocol(arg1); + } + + arg1 = processArgs(methodName, arg1, options.args, options.returnValue); + + const returnValue = my[options.name || methodName](arg1, arg2); + if (isSyncApi(methodName)) { // 同步 api + return processReturnValue(methodName, returnValue, options.returnValue) + } + return returnValue + } + } + return method +} + const todoApis = Object.create(null); -const TODOS = [ +const TODOS$1 = [ 'subscribePush', 'unsubscribePush', 'onPush', @@ -120,7 +305,7 @@ function createTodoApi (name) { } } -TODOS.forEach(function (name) { +TODOS$1.forEach(function (name) { todoApis[name] = createTodoApi(name); }); @@ -154,7 +339,7 @@ function getProvider ({ isFn(complete) && complete(res); } -var baseApi = /*#__PURE__*/Object.freeze({ +var extraApi = /*#__PURE__*/Object.freeze({ getProvider: getProvider }); @@ -175,16 +360,16 @@ if (typeof Proxy !== 'undefined') { if (api[name]) { return promisify(name, api[name]) } - if (baseApi[name]) { - return promisify(name, baseApi[name]) + if (extraApi[name]) { + return promisify(name, extraApi[name]) } if (todoApis[name]) { return promisify(name, todoApis[name]) } - if (!my.hasOwnProperty(name)) { + if (!hasOwn(my, name) && !hasOwn(protocols, name)) { return } - return promisify(name, my[name]) + return promisify(name, wrapper(name, my[name])) } }); } else { @@ -194,7 +379,7 @@ if (typeof Proxy !== 'undefined') { uni$1[name] = promisify(name, todoApis[name]); }); - Object.keys(baseApi).forEach(name => { + Object.keys(extraApi).forEach(name => { uni$1[name] = promisify(name, todoApis[name]); }); @@ -203,8 +388,8 @@ if (typeof Proxy !== 'undefined') { }); Object.keys(my).forEach(name => { - if (my.hasOwnProperty(name)) { - uni$1[name] = promisify(name, my[name]); + if (hasOwn(my, name) || hasOwn(protocols, name)) { + uni$1[name] = promisify(name, wrapper(name, my[name])); } }); } diff --git a/packages/uni-mp-baidu/dist/index.js b/packages/uni-mp-baidu/dist/index.js index 96d58af03..99a5765b7 100644 --- a/packages/uni-mp-baidu/dist/index.js +++ b/packages/uni-mp-baidu/dist/index.js @@ -1,5 +1,20 @@ +const _toString = Object.prototype.toString; +const hasOwnProperty = Object.prototype.hasOwnProperty; + function isFn (fn) { return typeof fn === 'function' +} + +function isStr (str) { + return typeof str === 'string' +} + +function isPlainObject (obj) { + return _toString.call(obj) === '[object Object]' +} + +function hasOwn (obj, key) { + return hasOwnProperty.call(obj, key) } const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/; @@ -97,6 +112,75 @@ function upx2px (number, newDeviceWidth) { return number } +var protocols = {}; + +const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; + +function processCallback (method, returnValue) { + return function (res) { + return method(processReturnValue(res, returnValue)) + } +} + +function processArgs (fromArgs, argsOption = {}, returnValue = {}) { + if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 + const toArgs = {}; + Object.keys(fromArgs).forEach(key => { + if (hasOwn(argsOption, key)) { + let keyOption = argsOption[key]; + if (isFn(keyOption)) { + keyOption = keyOption(fromArgs[key], fromArgs); + } + if (!keyOption) { // 不支持的参数 + console.warn(`${百度小程序} ${name}暂不支持${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); + } else { + toArgs[key] = fromArgs[key]; + } + }); + return toArgs + } else if (isFn(fromArgs)) { + fromArgs = processCallback(fromArgs, returnValue); + } + return fromArgs +} + +function processReturnValue (res, returnValue) { + return processArgs(res, returnValue) +} + +function wrapper (name, method) { + if (hasOwn(protocols, name)) { + const protocol = protocols[name]; + if (!protocol) { // 暂不支持的 api + return function () { + throw new Error(`${百度小程序}暂不支持${name}`) + } + } + return function (arg1, arg2) { // 目前 api 最多两个参数 + let options = protocol; + if (isFn(protocol)) { + options = protocol(arg1); + } + + arg1 = processArgs(arg1, options.args, options.returnValue); + + const returnValue = swan[options.name || name](arg1, arg2); + if (isSyncApi(name)) { // 同步 api + return processReturnValue(returnValue, options.returnValue) + } + return returnValue + } + } + return method +} + const todoApis = Object.create(null); const TODOS = [ @@ -154,7 +238,7 @@ function getProvider ({ isFn(complete) && complete(res); } -var baseApi = /*#__PURE__*/Object.freeze({ +var extraApi = /*#__PURE__*/Object.freeze({ getProvider: getProvider }); @@ -175,8 +259,8 @@ if (typeof Proxy !== 'undefined') { if (api[name]) { return promisify(name, api[name]) } - if (baseApi[name]) { - return promisify(name, baseApi[name]) + if (extraApi[name]) { + return promisify(name, extraApi[name]) } if (todoApis[name]) { return promisify(name, todoApis[name]) @@ -184,7 +268,7 @@ if (typeof Proxy !== 'undefined') { if (!swan.hasOwnProperty(name)) { return } - return promisify(name, swan[name]) + return promisify(name, wrapper(name, swan[name])) } }); } else { @@ -194,7 +278,7 @@ if (typeof Proxy !== 'undefined') { uni$1[name] = promisify(name, todoApis[name]); }); - Object.keys(baseApi).forEach(name => { + Object.keys(extraApi).forEach(name => { uni$1[name] = promisify(name, todoApis[name]); }); @@ -204,7 +288,7 @@ if (typeof Proxy !== 'undefined') { Object.keys(swan).forEach(name => { if (swan.hasOwnProperty(name)) { - uni$1[name] = promisify(name, swan[name]); + uni$1[name] = promisify(name, wrapper(name, swan[name])); } }); } diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index 47ba28e96..8b0be351c 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -1,5 +1,20 @@ +const _toString = Object.prototype.toString; +const hasOwnProperty = Object.prototype.hasOwnProperty; + function isFn (fn) { return typeof fn === 'function' +} + +function isStr (str) { + return typeof str === 'string' +} + +function isPlainObject (obj) { + return _toString.call(obj) === '[object Object]' +} + +function hasOwn (obj, key) { + return hasOwnProperty.call(obj, key) } const SYNC_API_RE = /hideKeyboard|upx2px|canIUse|^create|Sync$|Manager$/; @@ -97,6 +112,75 @@ function upx2px (number, newDeviceWidth) { return number } +var protocols = {}; + +const CALLBACKS = ['success', 'fail', 'cancel', 'complete']; + +function processCallback (method, returnValue) { + return function (res) { + return method(processReturnValue(res, returnValue)) + } +} + +function processArgs (fromArgs, argsOption = {}, returnValue = {}) { + if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 + const toArgs = {}; + Object.keys(fromArgs).forEach(key => { + if (hasOwn(argsOption, key)) { + let keyOption = argsOption[key]; + if (isFn(keyOption)) { + keyOption = keyOption(fromArgs[key], fromArgs); + } + if (!keyOption) { // 不支持的参数 + console.warn(`${微信小程序} ${name}暂不支持${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); + } else { + toArgs[key] = fromArgs[key]; + } + }); + return toArgs + } else if (isFn(fromArgs)) { + fromArgs = processCallback(fromArgs, returnValue); + } + return fromArgs +} + +function processReturnValue (res, returnValue) { + return processArgs(res, returnValue) +} + +function wrapper (name, method) { + if (hasOwn(protocols, name)) { + const protocol = protocols[name]; + if (!protocol) { // 暂不支持的 api + return function () { + throw new Error(`${微信小程序}暂不支持${name}`) + } + } + return function (arg1, arg2) { // 目前 api 最多两个参数 + let options = protocol; + if (isFn(protocol)) { + options = protocol(arg1); + } + + arg1 = processArgs(arg1, options.args, options.returnValue); + + const returnValue = wx[options.name || name](arg1, arg2); + if (isSyncApi(name)) { // 同步 api + return processReturnValue(returnValue, options.returnValue) + } + return returnValue + } + } + return method +} + const todoApis = Object.create(null); const TODOS = [ @@ -154,7 +238,7 @@ function getProvider ({ isFn(complete) && complete(res); } -var baseApi = /*#__PURE__*/Object.freeze({ +var extraApi = /*#__PURE__*/Object.freeze({ getProvider: getProvider }); @@ -175,8 +259,8 @@ if (typeof Proxy !== 'undefined') { if (api[name]) { return promisify(name, api[name]) } - if (baseApi[name]) { - return promisify(name, baseApi[name]) + if (extraApi[name]) { + return promisify(name, extraApi[name]) } if (todoApis[name]) { return promisify(name, todoApis[name]) @@ -184,7 +268,7 @@ if (typeof Proxy !== 'undefined') { if (!wx.hasOwnProperty(name)) { return } - return promisify(name, wx[name]) + return promisify(name, wrapper(name, wx[name])) } }); } else { @@ -194,7 +278,7 @@ if (typeof Proxy !== 'undefined') { uni$1[name] = promisify(name, todoApis[name]); }); - Object.keys(baseApi).forEach(name => { + Object.keys(extraApi).forEach(name => { uni$1[name] = promisify(name, todoApis[name]); }); @@ -204,7 +288,7 @@ if (typeof Proxy !== 'undefined') { Object.keys(wx).forEach(name => { if (wx.hasOwnProperty(name)) { - uni$1[name] = promisify(name, wx[name]); + uni$1[name] = promisify(name, wrapper(name, wx[name])); } }); } diff --git a/src/core/helpers/promise.js b/src/core/helpers/promise.js index 897fea454..860d74f7e 100644 --- a/src/core/helpers/promise.js +++ b/src/core/helpers/promise.js @@ -27,7 +27,7 @@ function handlePromise (promise) { .catch(err => [err]) } -function shouldPromise (name) { +export function shouldPromise (name) { if (isSyncApi(name)) { return false } diff --git a/src/core/runtime/base.js b/src/core/runtime/extra.js similarity index 100% rename from src/core/runtime/base.js rename to src/core/runtime/extra.js diff --git a/src/core/runtime/index.js b/src/core/runtime/index.js index aa0d77d44..6c8fbb142 100644 --- a/src/core/runtime/index.js +++ b/src/core/runtime/index.js @@ -1,3 +1,7 @@ +import { + hasOwn +} from 'uni-shared' + import { promisify } from '../helpers/promise' @@ -6,12 +10,16 @@ import { upx2px } from '../service/api/upx2px' +import wrapper from './wrapper' + import todoApi from './todo' -import * as baseApi from './base' +import * as extraApi from './extra' import * as api from 'uni-platform/service/api/index.js' +import protocols from 'uni-platform/service/api/protocols' + let uni = {} if (typeof Proxy !== 'undefined') { @@ -23,16 +31,16 @@ if (typeof Proxy !== 'undefined') { if (api[name]) { return promisify(name, api[name]) } - if (baseApi[name]) { - return promisify(name, baseApi[name]) + if (extraApi[name]) { + return promisify(name, extraApi[name]) } if (todoApi[name]) { return promisify(name, todoApi[name]) } - if (!__GLOBAL__.hasOwnProperty(name)) { + if (!hasOwn(__GLOBAL__, name) && !hasOwn(protocols, name)) { return } - return promisify(name, __GLOBAL__[name]) + return promisify(name, wrapper(name, __GLOBAL__[name])) } }) } else { @@ -42,7 +50,7 @@ if (typeof Proxy !== 'undefined') { uni[name] = promisify(name, todoApi[name]) }) - Object.keys(baseApi).forEach(name => { + Object.keys(extraApi).forEach(name => { uni[name] = promisify(name, todoApi[name]) }) @@ -51,8 +59,8 @@ if (typeof Proxy !== 'undefined') { }) Object.keys(__GLOBAL__).forEach(name => { - if (__GLOBAL__.hasOwnProperty(name)) { - uni[name] = promisify(name, __GLOBAL__[name]) + if (hasOwn(__GLOBAL__, name) || hasOwn(protocols, name)) { + uni[name] = promisify(name, wrapper(name, __GLOBAL__[name])) } }) } diff --git a/src/core/runtime/wrapper.js b/src/core/runtime/wrapper.js new file mode 100644 index 000000000..03f20fec3 --- /dev/null +++ b/src/core/runtime/wrapper.js @@ -0,0 +1,85 @@ +import { + isFn, + isStr, + hasOwn, + isPlainObject +} from 'uni-shared' + +import { + isSyncApi +} from '../helpers/promise' + +import protocols from 'uni-platform/service/api/protocols' + +const CALLBACKS = ['success', 'fail', 'cancel', 'complete'] + +function processCallback (methodName, method, returnValue) { + return function (res) { + return method(processReturnValue(methodName, res, returnValue)) + } +} + +function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}) { + if (isPlainObject(fromArgs)) { // 一般 api 的参数解析 + const toArgs = {} + if (isFn(argsOption)) { + argsOption = argsOption(fromArgs, toArgs) || {} + } + Object.keys(fromArgs).forEach(key => { + if (hasOwn(argsOption, key)) { + let keyOption = argsOption[key] + if (isFn(keyOption)) { + keyOption = keyOption(fromArgs[key], fromArgs) + } + if (!keyOption) { // 不支持的参数 + console.warn(`__PLATFORM_TITLE__ ${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(methodName, fromArgs[key], returnValue) + } else { + toArgs[key] = fromArgs[key] + } + }) + return toArgs + } else if (isFn(fromArgs)) { + fromArgs = processCallback(methodName, fromArgs, returnValue) + } + return fromArgs +} + +function processReturnValue (methodName, res, returnValue) { + if (isFn(protocols.returnValue)) { // 处理通用 returnValue + res = protocols.returnValue(methodName, res) + } + return processArgs(methodName, res, returnValue) +} + +export default function wrapper (methodName, method) { + if (hasOwn(protocols, methodName)) { + const protocol = protocols[methodName] + if (!protocol) { // 暂不支持的 api + return function () { + throw new Error(`__PLATFORM_TITLE__ 暂不支持${methodName}`) + } + } + return function (arg1, arg2) { // 目前 api 最多两个参数 + let options = protocol + if (isFn(protocol)) { + options = protocol(arg1) + } + + arg1 = processArgs(methodName, arg1, options.args, options.returnValue) + + const returnValue = __GLOBAL__[options.name || methodName](arg1, arg2) + if (isSyncApi(methodName)) { // 同步 api + return processReturnValue(methodName, returnValue, options.returnValue) + } + return returnValue + } + } + return method +} diff --git a/src/platforms/mp-alipay/service/api/protocols.js b/src/platforms/mp-alipay/service/api/protocols.js new file mode 100644 index 000000000..452fa9d29 --- /dev/null +++ b/src/platforms/mp-alipay/service/api/protocols.js @@ -0,0 +1,98 @@ +const TODOS = [ + 'hideTabBar', + 'hideTabBarRedDot', + 'removeTabBarBadge', + 'setTabBarBadge', + 'setTabBarItem', + 'setTabBarStyle', + 'showTabBar', + 'showTabBarRedDot' +] + +const protocols = { + returnValue (methodName, res) { // 通用 returnValue 解析 + if (res.error || res.errorMessage) { + res.errMsg = `${methodName}:fail ${res.errorMessage || res.error}` + delete res.error + delete res.errorMessage + } + return res + }, + 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' + } + } +} + +TODOS.forEach(todoApi => { + protocols[todoApi] = false +}) + +export default protocols diff --git a/src/platforms/mp-baidu/service/api/protocols.js b/src/platforms/mp-baidu/service/api/protocols.js new file mode 100644 index 000000000..b1c6ea436 --- /dev/null +++ b/src/platforms/mp-baidu/service/api/protocols.js @@ -0,0 +1 @@ +export default {} diff --git a/src/platforms/mp-weixin/service/api/protocols.js b/src/platforms/mp-weixin/service/api/protocols.js new file mode 100644 index 000000000..b1c6ea436 --- /dev/null +++ b/src/platforms/mp-weixin/service/api/protocols.js @@ -0,0 +1 @@ +export default {} diff --git a/src/shared/util.js b/src/shared/util.js index d6672499f..887821385 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -4,6 +4,11 @@ const hasOwnProperty = Object.prototype.hasOwnProperty export function isFn (fn) { return typeof fn === 'function' } + +export function isStr (str) { + return typeof str === 'string' +} + export function isPlainObject (obj) { return _toString.call(obj) === '[object Object]' } @@ -26,7 +31,7 @@ export function setProperties (item, props, propsData) { }) } -export function getLen (str = '') { +export function getLen (str = '') { /* eslint-disable no-control-regex */ return ('' + str).replace(/[^\x00-\xff]/g, '**').length } -- GitLab