diff --git a/packages/taro-h5/src/api/index.js b/packages/taro-h5/src/api/index.js index 0c059875ab55..08e69eec26a4 100644 --- a/packages/taro-h5/src/api/index.js +++ b/packages/taro-h5/src/api/index.js @@ -10,6 +10,7 @@ export * from './createSelectorQuery' export * from './fileTransfer' export * from './imageUtils' export * from './interactive' +export * from './location' export * from './navigationBar' export * from './others' export * from './pullDownRefresh' diff --git a/packages/taro-h5/src/api/location/index.js b/packages/taro-h5/src/api/location/index.js new file mode 100644 index 000000000000..4185e349f5f9 --- /dev/null +++ b/packages/taro-h5/src/api/location/index.js @@ -0,0 +1,5 @@ +import { processOpenapi } from '../utils/index' + +// export { default as chooseLocation } from './chooseLocation' +export const getLocation = processOpenapi('getLocation') +export const openLocation = processOpenapi('openLocation', { scale: 18 }) diff --git a/packages/taro-h5/src/api/open/index.js b/packages/taro-h5/src/api/open/index.js index 106c54dbca7e..c0fae6779f4b 100644 --- a/packages/taro-h5/src/api/open/index.js +++ b/packages/taro-h5/src/api/open/index.js @@ -1,5 +1,5 @@ -import { processApis } from '../utils' +import { processOpenapi } from '../utils' -export const requestPayment = processApis('chooseWXPay', undefined, undefined, options => { +export const requestPayment = processOpenapi('chooseWXPay', undefined, undefined, options => { return Object.assign(options, { timestamp: Number.parseInt(options.timeStamp, 10) }) }) diff --git a/packages/taro-h5/src/api/request/index.js b/packages/taro-h5/src/api/request/index.js index 25cee7880cde..8d8c88872d1b 100644 --- a/packages/taro-h5/src/api/request/index.js +++ b/packages/taro-h5/src/api/request/index.js @@ -106,5 +106,6 @@ function taroInterceptor (chain) { const link = new Link(taroInterceptor) +/** @type {TaroH5.request} */ export const request = link.request.bind(link) export const addInterceptor = link.addInterceptor.bind(link) diff --git a/packages/taro-h5/src/api/system/index.js b/packages/taro-h5/src/api/system/index.js index f8b33e020d87..ad7fdbac38f4 100644 --- a/packages/taro-h5/src/api/system/index.js +++ b/packages/taro-h5/src/api/system/index.js @@ -1,11 +1,9 @@ import { getSystemInfo, getSystemInfoSync } from './info' import { getNetworkType, onNetworkStatusChange } from './network' -import { processApis } from '../utils' +import { processOpenapi } from '../utils' -export const scanCode = processApis('scanQRCode', { needResult: 1 }, res => ({ +export const scanCode = processOpenapi('scanQRCode', { needResult: 1 }, res => ({ errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg, result: res.resultStr })) -export const getLocation = processApis('getLocation') -export const openLocation = processApis('openLocation', { scale: 18 }) export { getSystemInfo, getSystemInfoSync, getNetworkType, onNetworkStatusChange } diff --git a/packages/taro-h5/src/api/utils/index.js b/packages/taro-h5/src/api/utils/index.js index bc25a686c3ec..4616c1315373 100644 --- a/packages/taro-h5/src/api/utils/index.js +++ b/packages/taro-h5/src/api/utils/index.js @@ -64,15 +64,33 @@ function serializeParams (params) { } function temporarilyNotSupport (apiName) { - return () => console.error(`暂时不支持 API ${apiName}`) + return () => { + const errMsg = `暂时不支持 API ${apiName}` + console.error(errMsg) + return Promise.reject({ + errMsg + }) + } } function weixinCorpSupport (apiName) { - return () => console.error(`h5端仅在微信公众号中支持 API ${apiName}`) + return () => { + const errMsg = `h5端仅在微信公众号中支持 API ${apiName}` + console.error(errMsg) + return Promise.reject({ + errMsg + }) + } } function permanentlyNotSupport (apiName) { - return () => console.error(`不支持 API ${apiName}`) + return () => { + const errMsg = `不支持 API ${apiName}` + console.error(errMsg) + return Promise.reject({ + errMsg + }) + } } const VALID_COLOR_REG = /^#\d{6}$/ @@ -163,7 +181,7 @@ const createScroller = () => { return { listen, unlisten, getPos, isReachBottom } } -function processApis (apiName, defaultOptions, formatResult = res => res, formatParams = options => options) { +function processOpenapi (apiName, defaultOptions, formatResult = res => res, formatParams = options => options) { if (!window.wx) { return weixinCorpSupport(apiName) } @@ -172,12 +190,13 @@ function processApis (apiName, defaultOptions, formatResult = res => res, format let obj = Object.assign({}, defaultOptions, options) const p = new Promise((resolve, reject) => { ;['fail', 'success', 'complete'].forEach(k => { - obj[k] = res => { + obj[k] = oriRes => { + const res = formatResult(oriRes) options[k] && options[k](res) if (k === 'success') { - resolve(formatResult(res)) + resolve(res) } else if (k === 'fail') { - reject(formatResult(res)) + reject(res) } } }) @@ -220,7 +239,7 @@ export { isFunction, createCallbackManager, createScroller, - processApis, + processOpenapi, findRef, easeInOut, getTimingFunc