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

add mp-alipay api wrapper

上级 5fafe127
......@@ -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
})
]
}
......@@ -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,
......
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]));
}
});
}
......
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]));
}
});
}
......
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]));
}
});
}
......
......@@ -27,7 +27,7 @@ function handlePromise (promise) {
.catch(err => [err])
}
function shouldPromise (name) {
export function shouldPromise (name) {
if (isSyncApi(name)) {
return false
}
......
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]))
}
})
}
......
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
}
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
......@@ -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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册