Skip to content

Commit

Permalink
chore(h5): h5端统一openapi的resolve和success参数处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed Apr 19, 2019
1 parent c2820c5 commit ada2ed5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/taro-h5/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions packages/taro-h5/src/api/location/index.js
Original file line number Diff line number Diff line change
@@ -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 })
4 changes: 2 additions & 2 deletions packages/taro-h5/src/api/open/index.js
Original file line number Diff line number Diff line change
@@ -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) })
})
1 change: 1 addition & 0 deletions packages/taro-h5/src/api/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 2 additions & 4 deletions packages/taro-h5/src/api/system/index.js
Original file line number Diff line number Diff line change
@@ -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 }
35 changes: 27 additions & 8 deletions packages/taro-h5/src/api/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}$/
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}
})
Expand Down Expand Up @@ -220,7 +239,7 @@ export {
isFunction,
createCallbackManager,
createScroller,
processApis,
processOpenapi,
findRef,
easeInOut,
getTimingFunc
Expand Down

0 comments on commit ada2ed5

Please sign in to comment.