@mpxjs/api-proxy
Advanced tools
Comparing version 2.1.13 to 2.2.0
{ | ||
"name": "@mpxjs/api-proxy", | ||
"version": "2.1.13", | ||
"version": "2.2.0", | ||
"description": "convert miniprogram API at each end", | ||
@@ -30,3 +30,3 @@ "module": "src/index.js", | ||
"homepage": "https://github.com/didi/mpx#readme", | ||
"gitHead": "2667e21f5b0651e04e1b924b9d005ffccfdf9502" | ||
"gitHead": "5aca2e9ac3597f2a0e9255fbe95b45f757118dde" | ||
} |
@@ -10,3 +10,4 @@ import proxyAll from './proxy-all' | ||
exclude = [], // 转换平台时不转换的 Api | ||
fallbackMap = {} // 对于不支持的API,允许配置一个映射表,接管不存在的API | ||
fallbackMap = {}, // 对于不支持的API,允许配置一个映射表,接管不存在的API | ||
optimize = false // 内部一些实验优化的开关,外部用户慎用 | ||
} = options | ||
@@ -33,3 +34,4 @@ | ||
from, | ||
to | ||
to, | ||
optimize | ||
}) | ||
@@ -36,0 +38,0 @@ |
import { error } from '../utils' | ||
import wxToAliApi from './wxToAli' | ||
import getWxToAliApi from './wxToAli' | ||
import promisify from '../promisify' | ||
function transformApi (target, options) { | ||
const wxToAliApi = getWxToAliApi({ optimize: options.optimize }) | ||
const platformMap = { | ||
@@ -7,0 +8,0 @@ 'wx_ali': wxToAliApi, |
@@ -9,11 +9,35 @@ import { changeOpts, handleSuccess, getEnvObj, error, warn, noop } from '../utils' | ||
const wxToAliApi = { | ||
/** | ||
* 基础 | ||
*/ | ||
// optimize case avoid call api multi times with getSystemInfoSync | ||
let systemInfo | ||
getSystemInfo (options = {}) { | ||
const opts = changeOpts(options) | ||
const getWxToAliApi = ({ optimize = false }) => { | ||
return { | ||
/** | ||
* 基础 | ||
*/ | ||
handleSuccess(opts, res => { | ||
getSystemInfo (options = {}) { | ||
const opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
res.system = `${res.platform} ${res.system}` | ||
// 支付宝 windowHeight 可能为 0 | ||
if (!res.windowHeight) { | ||
res.windowHeight = Math.floor(res.screenHeight * res.windowWidth / res.screenWidth) - 50 | ||
} | ||
return res | ||
}) | ||
ALI_OBJ.getSystemInfo(opts) | ||
}, | ||
getSystemInfoSync () { | ||
if (optimize) { | ||
if (systemInfo) return systemInfo | ||
} | ||
let res = ALI_OBJ.getSystemInfoSync() || {} | ||
res.system = `${res.platform} ${res.system}` | ||
@@ -26,615 +50,602 @@ | ||
if (optimize) systemInfo = res | ||
return res | ||
}) | ||
}, | ||
ALI_OBJ.getSystemInfo(opts) | ||
}, | ||
/** | ||
* 界面 | ||
*/ | ||
getSystemInfoSync () { | ||
let res = ALI_OBJ.getSystemInfoSync() || {} | ||
showToast (options = {}) { | ||
const opts = changeOpts(options, { | ||
title: 'content', | ||
icon: 'type' | ||
}) | ||
ALI_OBJ.showToast(opts) | ||
}, | ||
res.system = `${res.platform} ${res.system}` | ||
hideToast (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.hideToast 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.hideToast(options) | ||
}, | ||
// 支付宝 windowHeight 可能为 0 | ||
if (!res.windowHeight) { | ||
res.windowHeight = Math.floor(res.screenHeight * res.windowWidth / res.screenWidth) - 50 | ||
} | ||
showModal (options = {}) { | ||
let opts | ||
return res | ||
}, | ||
if (options.showCancel === undefined || options.showCancel) { | ||
opts = changeOpts(options, { | ||
confirmText: 'confirmButtonText', | ||
cancelText: 'cancelButtonText' | ||
}) | ||
/** | ||
* 界面 | ||
*/ | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, undefined, { 'cancel': !res.confirm }) | ||
}) | ||
showToast (options = {}) { | ||
const opts = changeOpts(options, { | ||
title: 'content', | ||
icon: 'type' | ||
}) | ||
ALI_OBJ.showToast(opts) | ||
}, | ||
ALI_OBJ.confirm(opts) | ||
} else { | ||
opts = changeOpts(options, { | ||
confirmText: 'buttonText' | ||
}) | ||
hideToast (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.hideToast 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.hideToast(options) | ||
}, | ||
ALI_OBJ.alert(opts) | ||
} | ||
}, | ||
showModal (options = {}) { | ||
let opts | ||
if (options.showCancel === undefined || options.showCancel) { | ||
opts = changeOpts(options, { | ||
confirmText: 'confirmButtonText', | ||
cancelText: 'cancelButtonText' | ||
showLoading (options = {}) { | ||
const opts = changeOpts(options, { | ||
title: 'content' | ||
}) | ||
ALI_OBJ.showLoading(opts) | ||
}, | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, undefined, { 'cancel': !res.confirm }) | ||
}) | ||
hideLoading (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.hideLoading 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.hideLoading(options) | ||
}, | ||
ALI_OBJ.confirm(opts) | ||
} else { | ||
opts = changeOpts(options, { | ||
confirmText: 'buttonText' | ||
showActionSheet (options = {}) { | ||
const opts = changeOpts(options, { | ||
itemList: 'items' | ||
}) | ||
ALI_OBJ.alert(opts) | ||
} | ||
}, | ||
const cacheSuc = opts.success || noop | ||
const cacheFail = opts.fail || noop | ||
showLoading (options = {}) { | ||
const opts = changeOpts(options, { | ||
title: 'content' | ||
}) | ||
ALI_OBJ.showLoading(opts) | ||
}, | ||
hideLoading (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.hideLoading 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.hideLoading(options) | ||
}, | ||
showActionSheet (options = {}) { | ||
const opts = changeOpts(options, { | ||
itemList: 'items' | ||
}) | ||
const cacheSuc = opts.success || noop | ||
const cacheFail = opts.fail || noop | ||
opts.success = function (res) { | ||
let sucRes = changeOpts(res, { | ||
index: 'tapIndex' | ||
}) | ||
if (sucRes.tapIndex === -1) { | ||
cacheFail.call(this, { | ||
errMsg: 'showActionSheet:fail cancel' | ||
opts.success = function (res) { | ||
let sucRes = changeOpts(res, { | ||
index: 'tapIndex' | ||
}) | ||
} else { | ||
cacheSuc.call(this, sucRes) | ||
if (sucRes.tapIndex === -1) { | ||
cacheFail.call(this, { | ||
errMsg: 'showActionSheet:fail cancel' | ||
}) | ||
} else { | ||
cacheSuc.call(this, sucRes) | ||
} | ||
} | ||
} | ||
ALI_OBJ.showActionSheet(opts) | ||
}, | ||
ALI_OBJ.showActionSheet(opts) | ||
}, | ||
showNavigationBarLoading (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.showNavigationBarLoading 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.showNavigationBarLoading(options) | ||
}, | ||
showNavigationBarLoading (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.showNavigationBarLoading 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.showNavigationBarLoading(options) | ||
}, | ||
hideNavigationBarLoading (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.hideNavigationBarLoading 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.hideNavigationBarLoading(options) | ||
}, | ||
hideNavigationBarLoading (options = {}) { | ||
if (options.success || options.fail || options.complete) { | ||
warn(`${TIPS_NAME}.hideNavigationBarLoading 不支持 success/fail/complete 参数`) | ||
} | ||
ALI_OBJ.hideNavigationBarLoading(options) | ||
}, | ||
setNavigationBarTitle (options = {}) { | ||
ALI_OBJ.setNavigationBar(options) | ||
}, | ||
setNavigationBarTitle (options = {}) { | ||
ALI_OBJ.setNavigationBar(options) | ||
}, | ||
setNavigationBarColor (options = {}) { | ||
ALI_OBJ.setNavigationBar(options) | ||
}, | ||
setNavigationBarColor (options = {}) { | ||
ALI_OBJ.setNavigationBar(options) | ||
}, | ||
/** | ||
* 网络 | ||
*/ | ||
/** | ||
* 网络 | ||
*/ | ||
request (options = {}) { | ||
const opts = changeOpts(options, { | ||
header: 'headers' | ||
}) | ||
request (options = {}) { | ||
const opts = changeOpts(options, { | ||
header: 'headers' | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { | ||
headers: 'header', | ||
status: 'statusCode' | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { | ||
headers: 'header', | ||
status: 'statusCode' | ||
}) | ||
}) | ||
}) | ||
// request 在 1.11.0 以上版本才支持 | ||
// httpRequest 即将被废弃,钉钉端仍需要使用 | ||
if (ALI_OBJ.canIUse('request')) { | ||
return ALI_OBJ.request(opts) | ||
} else { | ||
return ALI_OBJ.httpRequest(opts) | ||
} | ||
}, | ||
// request 在 1.11.0 以上版本才支持 | ||
// httpRequest 即将被废弃,钉钉端仍需要使用 | ||
if (ALI_OBJ.canIUse('request')) { | ||
return ALI_OBJ.request(opts) | ||
} else { | ||
return ALI_OBJ.httpRequest(opts) | ||
} | ||
}, | ||
downloadFile (options = {}) { | ||
const opts = changeOpts(options) | ||
downloadFile (options = {}) { | ||
const opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePath: 'tempFilePath' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePath: 'tempFilePath' }) | ||
}) | ||
return ALI_OBJ.downloadFile(opts) | ||
}, | ||
return ALI_OBJ.downloadFile(opts) | ||
}, | ||
uploadFile (options = {}) { | ||
const opts = changeOpts(options, { name: 'fileName' }) | ||
uploadFile (options = {}) { | ||
const opts = changeOpts(options, { name: 'fileName' }) | ||
return ALI_OBJ.uploadFile(opts) | ||
}, | ||
return ALI_OBJ.uploadFile(opts) | ||
}, | ||
/** | ||
* 数据缓存 | ||
*/ | ||
/** | ||
* 数据缓存 | ||
*/ | ||
setStorageSync (key, data) { | ||
ALI_OBJ.setStorageSync({ | ||
key, | ||
data | ||
}) | ||
}, | ||
setStorageSync (key, data) { | ||
ALI_OBJ.setStorageSync({ | ||
key, | ||
data | ||
}) | ||
}, | ||
removeStorageSync (key) { | ||
ALI_OBJ.removeStorageSync({ | ||
key | ||
}) | ||
}, | ||
removeStorageSync (key) { | ||
ALI_OBJ.removeStorageSync({ | ||
key | ||
}) | ||
}, | ||
getStorageSync (key) { | ||
return ALI_OBJ.getStorageSync({ | ||
key | ||
}).data | ||
}, | ||
getStorageSync (key) { | ||
return ALI_OBJ.getStorageSync({ | ||
key | ||
}).data | ||
}, | ||
/** | ||
* 媒体 | ||
*/ | ||
/** | ||
* 媒体 | ||
*/ | ||
saveImageToPhotosAlbum (key) { | ||
warn(`如果想要保存在线图片链接,可以直接使用 ${TIPS_NAME}.saveImage`) | ||
}, | ||
saveImageToPhotosAlbum (key) { | ||
warn(`如果想要保存在线图片链接,可以直接使用 ${TIPS_NAME}.saveImage`) | ||
}, | ||
previewImage (options = {}) { | ||
const opts = changeOpts(options) | ||
previewImage (options = {}) { | ||
const opts = changeOpts(options) | ||
if (opts.current) { | ||
let idx = options.urls.indexOf(opts.current) | ||
opts.current = idx !== -1 ? idx : 0 | ||
} | ||
if (opts.current) { | ||
let idx = options.urls.indexOf(opts.current) | ||
opts.current = idx !== -1 ? idx : 0 | ||
} | ||
ALI_OBJ.previewImage(opts) | ||
}, | ||
ALI_OBJ.previewImage(opts) | ||
}, | ||
compressImage (options = {}) { | ||
const opts = changeOpts(options, { | ||
quality: '' | ||
}, { | ||
compressLevel: Math.round(options.quality / 100 * 4), // 支付宝图片压缩质量为 0 ~ 4,微信是 0 ~ 100 | ||
apFilePaths: [options.src] | ||
}) | ||
compressImage (options = {}) { | ||
const opts = changeOpts(options, { | ||
quality: '' | ||
}, { | ||
compressLevel: Math.round(options.quality / 100 * 4), // 支付宝图片压缩质量为 0 ~ 4,微信是 0 ~ 100 | ||
apFilePaths: [options.src] | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts( | ||
res, | ||
{ apFilePaths: '' }, | ||
{ tempFilePath: res.apFilePaths[0] } | ||
) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts( | ||
res, | ||
{ apFilePaths: '' }, | ||
{ tempFilePath: res.apFilePaths[0] } | ||
) | ||
}) | ||
ALI_OBJ.compressImage(opts) | ||
}, | ||
ALI_OBJ.compressImage(opts) | ||
}, | ||
chooseImage (options = {}) { | ||
const opts = changeOpts(options) | ||
chooseImage (options = {}) { | ||
const opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePaths: 'tempFilePaths' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePaths: 'tempFilePaths' }) | ||
}) | ||
ALI_OBJ.chooseImage(opts) | ||
}, | ||
ALI_OBJ.chooseImage(opts) | ||
}, | ||
/** | ||
* 位置 | ||
*/ | ||
/** | ||
* 位置 | ||
*/ | ||
getLocation (options = {}) { | ||
if (options.aliType === undefined && options.type) { | ||
warn(`如果要针对支付宝设置 ${TIPS_NAME}.getLocation 中的 type 参数,请使用 aliType, 取值为 0~3`) | ||
options.aliType = 0 | ||
} | ||
if (options.altitude) { | ||
error(`支付宝 ${TIPS_NAME}.getLocation 不支持获取高度信息`) | ||
} | ||
getLocation (options = {}) { | ||
if (options.aliType === undefined && options.type) { | ||
warn(`如果要针对支付宝设置 ${TIPS_NAME}.getLocation 中的 type 参数,请使用 aliType, 取值为 0~3`) | ||
options.aliType = 0 | ||
} | ||
if (options.altitude) { | ||
error(`支付宝 ${TIPS_NAME}.getLocation 不支持获取高度信息`) | ||
} | ||
const opts = changeOpts(options, { | ||
type: '', | ||
aliType: 'type' | ||
}) | ||
const opts = changeOpts(options, { | ||
type: '', | ||
aliType: 'type' | ||
}) | ||
ALI_OBJ.getLocation(opts) | ||
}, | ||
ALI_OBJ.getLocation(opts) | ||
}, | ||
/** | ||
* 文件 | ||
*/ | ||
/** | ||
* 文件 | ||
*/ | ||
saveFile (options = {}) { | ||
const opts = changeOpts(options, { | ||
tempFilePath: 'apFilePath' | ||
}) | ||
saveFile (options = {}) { | ||
const opts = changeOpts(options, { | ||
tempFilePath: 'apFilePath' | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePath: 'savedFilePath' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePath: 'savedFilePath' }) | ||
}) | ||
ALI_OBJ.saveFile(opts) | ||
}, | ||
ALI_OBJ.saveFile(opts) | ||
}, | ||
removeSavedFile (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
removeSavedFile (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePath: 'savedFilePath' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { apFilePath: 'savedFilePath' }) | ||
}) | ||
ALI_OBJ.removeSavedFile(opts) | ||
}, | ||
ALI_OBJ.removeSavedFile(opts) | ||
}, | ||
getSavedFileList (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
getSavedFileList (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
handleSuccess(opts, res => { | ||
if (res.fileList) { | ||
res.fileList.forEach(file => { | ||
let resFile = changeOpts(file, { | ||
apFilePath: 'filePath' | ||
handleSuccess(opts, res => { | ||
if (res.fileList) { | ||
res.fileList.forEach(file => { | ||
let resFile = changeOpts(file, { | ||
apFilePath: 'filePath' | ||
}) | ||
file = resFile | ||
}) | ||
file = resFile | ||
}) | ||
} | ||
return res | ||
}) | ||
} | ||
return res | ||
}) | ||
ALI_OBJ.getSavedFileList(opts) | ||
}, | ||
ALI_OBJ.getSavedFileList(opts) | ||
}, | ||
getSavedFileInfo (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
getSavedFileInfo (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
ALI_OBJ.getSavedFileInfo(opts) | ||
}, | ||
ALI_OBJ.getSavedFileInfo(opts) | ||
}, | ||
getFileInfo (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
getFileInfo (options = {}) { | ||
const opts = changeOpts(options, { | ||
filePath: 'apFilePath' | ||
}) | ||
ALI_OBJ.getFileInfo(opts) | ||
}, | ||
ALI_OBJ.getFileInfo(opts) | ||
}, | ||
/** | ||
* 设备 | ||
*/ | ||
/** | ||
* 设备 | ||
*/ | ||
addPhoneContact (options = {}) { | ||
const opts = changeOpts(options, { | ||
weChatNumber: 'alipayAccount' | ||
}) | ||
addPhoneContact (options = {}) { | ||
const opts = changeOpts(options, { | ||
weChatNumber: 'alipayAccount' | ||
}) | ||
ALI_OBJ.addPhoneContact(opts) | ||
}, | ||
ALI_OBJ.addPhoneContact(opts) | ||
}, | ||
setClipboardData (options = {}) { | ||
const opts = changeOpts(options, { | ||
data: 'text' | ||
}) | ||
setClipboardData (options = {}) { | ||
const opts = changeOpts(options, { | ||
data: 'text' | ||
}) | ||
ALI_OBJ.setClipboard(opts) | ||
}, | ||
ALI_OBJ.setClipboard(opts) | ||
}, | ||
getClipboardData (options = {}) { | ||
const opts = changeOpts(options) | ||
getClipboardData (options = {}) { | ||
const opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { text: 'data' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { text: 'data' }) | ||
}) | ||
ALI_OBJ.getClipboard(opts) | ||
}, | ||
ALI_OBJ.getClipboard(opts) | ||
}, | ||
setScreenBrightness (options = {}) { | ||
const opts = changeOpts(options, { | ||
value: 'brightness' | ||
}) | ||
setScreenBrightness (options = {}) { | ||
const opts = changeOpts(options, { | ||
value: 'brightness' | ||
}) | ||
ALI_OBJ.setScreenBrightness(opts) | ||
}, | ||
ALI_OBJ.setScreenBrightness(opts) | ||
}, | ||
getScreenBrightness (options = {}) { | ||
const opts = changeOpts(options) | ||
getScreenBrightness (options = {}) { | ||
const opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { brightness: 'value' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { brightness: 'value' }) | ||
}) | ||
ALI_OBJ.getScreenBrightness(opts) | ||
}, | ||
ALI_OBJ.getScreenBrightness(opts) | ||
}, | ||
makePhoneCall (options = {}) { | ||
const opts = changeOpts(options, { | ||
phoneNumber: 'number' | ||
}) | ||
makePhoneCall (options = {}) { | ||
const opts = changeOpts(options, { | ||
phoneNumber: 'number' | ||
}) | ||
ALI_OBJ.makePhoneCall(opts) | ||
}, | ||
ALI_OBJ.makePhoneCall(opts) | ||
}, | ||
stopAccelerometer (options = {}) { | ||
ALI_OBJ.offAccelerometerChange(options) | ||
}, | ||
stopAccelerometer (options = {}) { | ||
ALI_OBJ.offAccelerometerChange(options) | ||
}, | ||
startAccelerometer () { | ||
warn(`支付宝加速计不需要使用 ${TIPS_NAME}.startAccelerometer 开始,可以直接在 ${TIPS_NAME}.onAccelerometerChange 中监听`) | ||
}, | ||
startAccelerometer () { | ||
warn(`支付宝加速计不需要使用 ${TIPS_NAME}.startAccelerometer 开始,可以直接在 ${TIPS_NAME}.onAccelerometerChange 中监听`) | ||
}, | ||
stopCompass (options = {}) { | ||
ALI_OBJ.offCompassChange(options) | ||
}, | ||
stopCompass (options = {}) { | ||
ALI_OBJ.offCompassChange(options) | ||
}, | ||
startCompass () { | ||
warn(`支付宝罗盘不需要使用 ${TIPS_NAME}.startCompass 开始,可以直接在 ${TIPS_NAME}.onCompassChange 中监听`) | ||
}, | ||
startCompass () { | ||
warn(`支付宝罗盘不需要使用 ${TIPS_NAME}.startCompass 开始,可以直接在 ${TIPS_NAME}.onCompassChange 中监听`) | ||
}, | ||
stopGyroscope (options = {}) { | ||
ALI_OBJ.offGyroscopeChange(options) | ||
}, | ||
stopGyroscope (options = {}) { | ||
ALI_OBJ.offGyroscopeChange(options) | ||
}, | ||
startGyroscope () { | ||
warn(`支付宝陀螺仪不需要使用 ${TIPS_NAME}.startGyroscope 开始,可以直接在 ${TIPS_NAME}.onGyroscopeChange 中监听`) | ||
}, | ||
startGyroscope () { | ||
warn(`支付宝陀螺仪不需要使用 ${TIPS_NAME}.startGyroscope 开始,可以直接在 ${TIPS_NAME}.onGyroscopeChange 中监听`) | ||
}, | ||
scanCode (options = {}) { | ||
const opts = changeOpts(options, { | ||
onlyFromCamera: 'hideAlbum', | ||
scanType: 'type' | ||
}) | ||
scanCode (options = {}) { | ||
const opts = changeOpts(options, { | ||
onlyFromCamera: 'hideAlbum', | ||
scanType: 'type' | ||
}) | ||
if (opts.type) { | ||
switch (opts.type) { | ||
case 'barCode': | ||
opts.type = 'bar' | ||
break | ||
case 'qrCode': | ||
opts.type = 'qr' | ||
break | ||
default: | ||
error(`${TIPS_NAME}.scanCode 只支持扫描条形码和二维码,请将 type 设置为 barCode/qrCode`) | ||
opts.type = 'qr' | ||
break | ||
if (opts.type) { | ||
switch (opts.type) { | ||
case 'barCode': | ||
opts.type = 'bar' | ||
break | ||
case 'qrCode': | ||
opts.type = 'qr' | ||
break | ||
default: | ||
error(`${TIPS_NAME}.scanCode 只支持扫描条形码和二维码,请将 type 设置为 barCode/qrCode`) | ||
opts.type = 'qr' | ||
break | ||
} | ||
} | ||
} | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { code: 'result' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { code: 'result' }) | ||
}) | ||
ALI_OBJ.scan(opts) | ||
}, | ||
ALI_OBJ.scan(opts) | ||
}, | ||
/** | ||
* 开放接口 | ||
*/ | ||
/** | ||
* 开放接口 | ||
*/ | ||
login (options = {}) { | ||
const opts = changeOpts(options) | ||
login (options = {}) { | ||
const opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { authCode: 'code' }) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, { authCode: 'code' }) | ||
}) | ||
ALI_OBJ.getAuthCode(opts) | ||
}, | ||
ALI_OBJ.getAuthCode(opts) | ||
}, | ||
checkSession () { | ||
warn(`支付宝不支持 ${TIPS_NAME}.checkSession 检查登录过期`) | ||
}, | ||
checkSession () { | ||
warn(`支付宝不支持 ${TIPS_NAME}.checkSession 检查登录过期`) | ||
}, | ||
getUserInfo (options = {}) { | ||
if (options.withCredentials === true) { | ||
warn(`支付宝不支持在 ${TIPS_NAME}.getUserInfo 使用 withCredentials 参数中获取等敏感信息`) | ||
} | ||
if (options.lang) { | ||
warn(`支付宝不支持在 ${TIPS_NAME}.getUserInfo 中使用 lang 参数`) | ||
} | ||
getUserInfo (options = {}) { | ||
if (options.withCredentials === true) { | ||
warn(`支付宝不支持在 ${TIPS_NAME}.getUserInfo 使用 withCredentials 参数中获取等敏感信息`) | ||
} | ||
if (options.lang) { | ||
warn(`支付宝不支持在 ${TIPS_NAME}.getUserInfo 中使用 lang 参数`) | ||
} | ||
let opts = changeOpts(options) | ||
let opts = changeOpts(options) | ||
handleSuccess(opts, res => { | ||
let userInfo = changeOpts(res, { avatar: 'avatarUrl' }, { gender: 0 }) | ||
const params = ['country', 'province', 'city', 'language'] | ||
handleSuccess(opts, res => { | ||
let userInfo = changeOpts(res, { avatar: 'avatarUrl' }, { gender: 0 }) | ||
const params = ['country', 'province', 'city', 'language'] | ||
params.forEach(key => { | ||
Object.defineProperty(userInfo, key, { | ||
get () { | ||
warn(`支付宝 ${TIPS_NAME}.getUserInfo 不能获取 ${key}`) | ||
return '' | ||
} | ||
params.forEach(key => { | ||
Object.defineProperty(userInfo, key, { | ||
get () { | ||
warn(`支付宝 ${TIPS_NAME}.getUserInfo 不能获取 ${key}`) | ||
return '' | ||
} | ||
}) | ||
}) | ||
return { | ||
userInfo | ||
} | ||
}) | ||
return { | ||
userInfo | ||
ALI_OBJ.getAuthUserInfo(opts) | ||
}, | ||
requestPayment (options = {}) { | ||
if (!options.tradeNO) { | ||
error(`请在支付函数 ${TIPS_NAME}.requestPayment 中添加 tradeNO 参数用于支付宝支付`) | ||
} | ||
}) | ||
ALI_OBJ.getAuthUserInfo(opts) | ||
}, | ||
let opts = changeOpts(options, { | ||
timeStamp: '', | ||
nonceStr: '', | ||
package: '', | ||
signType: '', | ||
paySign: '' | ||
}) | ||
requestPayment (options = {}) { | ||
if (!options.tradeNO) { | ||
error(`请在支付函数 ${TIPS_NAME}.requestPayment 中添加 tradeNO 参数用于支付宝支付`) | ||
} | ||
const cacheSuc = opts.success || noop | ||
const cacheFail = opts.fail || noop | ||
let opts = changeOpts(options, { | ||
timeStamp: '', | ||
nonceStr: '', | ||
package: '', | ||
signType: '', | ||
paySign: '' | ||
}) | ||
// 抹平用微信的 complete | ||
if (typeof opts.complete === 'function') { | ||
const cacheComplete = opts.complete | ||
opts.complete = function (res) { | ||
if (+res.resultCode === 9000) { | ||
res.errMsg = 'requestPayment:ok' | ||
cacheComplete.call(this, res) | ||
} | ||
} | ||
} | ||
const cacheSuc = opts.success || noop | ||
const cacheFail = opts.fail || noop | ||
// 抹平用微信的 complete | ||
if (typeof opts.complete === 'function') { | ||
const cacheComplete = opts.complete | ||
opts.complete = function (res) { | ||
opts.success = function (res) { | ||
if (+res.resultCode === 9000) { | ||
res.errMsg = 'requestPayment:ok' | ||
cacheComplete.call(this, res) | ||
cacheSuc.call(this, res) | ||
} else { | ||
cacheFail.call(this, res) | ||
} | ||
} | ||
} | ||
opts.success = function (res) { | ||
if (+res.resultCode === 9000) { | ||
cacheSuc.call(this, res) | ||
} else { | ||
cacheFail.call(this, res) | ||
} | ||
} | ||
ALI_OBJ.tradePay(opts) | ||
}, | ||
ALI_OBJ.tradePay(opts) | ||
}, | ||
/** | ||
* 画布 | ||
*/ | ||
/** | ||
* 画布 | ||
*/ | ||
createCanvasContext (canvasId = {}) { | ||
let ctx = ALI_OBJ.createCanvasContext(canvasId) | ||
createCanvasContext (canvasId = {}) { | ||
let ctx = ALI_OBJ.createCanvasContext(canvasId) | ||
CANVAS_MAP[canvasId] = ctx | ||
CANVAS_MAP[canvasId] = ctx | ||
return ctx | ||
}, | ||
return ctx | ||
}, | ||
canvasToTempFilePath (options = {}) { | ||
if (!CANVAS_MAP[options.canvasId]) { | ||
error(`支付宝调用 ${TIPS_NAME}.toTempFilePath 方法之前需要先调用 ${TIPS_NAME}.createCanvasContext 创建 context`) | ||
return | ||
} | ||
canvasToTempFilePath (options = {}) { | ||
if (!CANVAS_MAP[options.canvasId]) { | ||
error(`支付宝调用 ${TIPS_NAME}.toTempFilePath 方法之前需要先调用 ${TIPS_NAME}.createCanvasContext 创建 context`) | ||
return | ||
} | ||
const opts = changeOpts(options, { canvasId: '' }) | ||
const ctx = CANVAS_MAP[options.canvasId] | ||
const opts = changeOpts(options, { canvasId: '' }) | ||
const ctx = CANVAS_MAP[options.canvasId] | ||
handleSuccess(opts, res => { | ||
return changeOpts( | ||
res, | ||
{ apFilePath: 'tempFilePath' }, | ||
{ errMsg: 'canvasToTempFilePath:ok' } | ||
) | ||
}) | ||
handleSuccess(opts, res => { | ||
return changeOpts( | ||
res, | ||
{ apFilePath: 'tempFilePath' }, | ||
{ errMsg: 'canvasToTempFilePath:ok' } | ||
) | ||
}) | ||
ctx.toTempFilePath(opts) | ||
}, | ||
ctx.toTempFilePath(opts) | ||
}, | ||
canvasPutImageData (options = {}) { | ||
if (!CANVAS_MAP[options.canvasId]) { | ||
error(`支付宝调用 ${TIPS_NAME}.putImageData 方法之前需要先调用 ${TIPS_NAME}.createCanvasContext 创建 context`) | ||
return | ||
} | ||
canvasPutImageData (options = {}) { | ||
if (!CANVAS_MAP[options.canvasId]) { | ||
error(`支付宝调用 ${TIPS_NAME}.putImageData 方法之前需要先调用 ${TIPS_NAME}.createCanvasContext 创建 context`) | ||
return | ||
} | ||
const opts = changeOpts(options, { canvasId: '' }) | ||
const ctx = CANVAS_MAP[options.canvasId] | ||
const opts = changeOpts(options, { canvasId: '' }) | ||
const ctx = CANVAS_MAP[options.canvasId] | ||
// success 里面的 this 指向参数 options | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, undefined, { errMsg: 'canvasPutImageData:ok' } | ||
) | ||
}, options) | ||
// success 里面的 this 指向参数 options | ||
handleSuccess(opts, res => { | ||
return changeOpts(res, undefined, { errMsg: 'canvasPutImageData:ok' } | ||
) | ||
}, options) | ||
ctx.putImageData(opts) | ||
}, | ||
ctx.putImageData(opts) | ||
}, | ||
canvasGetImageData (options = {}) { | ||
if (!CANVAS_MAP[options.canvasId]) { | ||
error(`支付宝调用 ${TIPS_NAME}.getImageData 方法之前需要先调用 ${TIPS_NAME}.createCanvasContext 创建 context`) | ||
return | ||
} | ||
canvasGetImageData (options = {}) { | ||
if (!CANVAS_MAP[options.canvasId]) { | ||
error(`支付宝调用 ${TIPS_NAME}.getImageData 方法之前需要先调用 ${TIPS_NAME}.createCanvasContext 创建 context`) | ||
return | ||
} | ||
const opts = changeOpts(options, { canvasId: '' }) | ||
const ctx = CANVAS_MAP[options.canvasId] | ||
const opts = changeOpts(options, { canvasId: '' }) | ||
const ctx = CANVAS_MAP[options.canvasId] | ||
// success 里面的 this 指向参数 options | ||
handleSuccess(opts, undefined, options) | ||
// success 里面的 this 指向参数 options | ||
handleSuccess(opts, undefined, options) | ||
ctx.getImageData(opts) | ||
}, | ||
ctx.getImageData(opts) | ||
}, | ||
/** | ||
* WXML | ||
*/ | ||
/** | ||
* WXML | ||
*/ | ||
createSelectorQuery (options = {}) { | ||
const selectorQuery = ALI_OBJ.createSelectorQuery(options) | ||
const proxyMethods = ['boundingClientRect', 'scrollOffset'] | ||
const cbs = [] | ||
proxyMethods.forEach((name) => { | ||
const originalMethod = selectorQuery[name] | ||
selectorQuery[name] = function (cb = noop) { | ||
cbs.push(cb) | ||
return originalMethod.call(this) | ||
} | ||
}) | ||
createSelectorQuery (options = {}) { | ||
const selectorQuery = ALI_OBJ.createSelectorQuery(options) | ||
const proxyMethods = ['boundingClientRect', 'scrollOffset'] | ||
const cbs = [] | ||
proxyMethods.forEach((name) => { | ||
const originalMethod = selectorQuery[name] | ||
selectorQuery[name] = function (cb = noop) { | ||
cbs.push(cb) | ||
return originalMethod.call(this) | ||
const originalExec = selectorQuery.exec | ||
selectorQuery.exec = function (originalCb = noop) { | ||
const cb = function (results) { | ||
results.forEach((item, index) => { | ||
cbs[index](item) | ||
}) | ||
originalCb(results) | ||
} | ||
return originalExec.call(this, cb) | ||
} | ||
}) | ||
const originalExec = selectorQuery.exec | ||
selectorQuery.exec = function (originalCb = noop) { | ||
const cb = function (results) { | ||
results.forEach((item, index) => { | ||
cbs[index](item) | ||
}) | ||
originalCb(results) | ||
selectorQuery.in = function () { | ||
return this | ||
} | ||
return originalExec.call(this, cb) | ||
} | ||
selectorQuery.in = function () { | ||
return this | ||
return selectorQuery | ||
} | ||
return selectorQuery | ||
} | ||
} | ||
export default wxToAliApi | ||
export default getWxToAliApi |
767
36478
9