Skip to content

Commit

Permalink
feat(taro-h5): 增加 makePhoneCall API,close #1426
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Dec 13, 2018
1 parent 9738e56 commit 93e23f0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
18 changes: 15 additions & 3 deletions docs/native-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1324,21 +1324,33 @@ Taro.stopCompass()

#### Taro.makePhoneCall(OBJECT)

使用方式同 [`wx.makePhoneCall`](https://developers.weixin.qq.com/miniprogram/dev/api/phonecall.html#wxmakephonecallobject),支持 `Promise` 化使用。
拨打电话,支持 `Promise` 化使用。

**OBJECT 参数说明:**

| 参数 | 类型 | 必填 | 说明 |
| :-- | :-- | :-- | :-- |
| phoneNumber | String || 需要拨打的电话号码 |
| success | Function || 接口调用成功的回调函数 |
| fail | Function || 接口调用失败的回调函数 |
| complete | Function || 接口调用结束的回调函数(调用成功、失败都会执行) |

**示例代码:**

```jsx
import Taro from '@tarojs/taro'

Taro.makePhoneCall(params).then(...)
Taro.makePhoneCall({
phoneNumber: '10086'
})
.then(...)
```

> API 支持度
| API | 微信小程序 | H5 | ReactNative |
| :-: | :-: | :-: | :-: |
| Taro.makePhoneCall | ✔️ | | ✔️ |
| Taro.makePhoneCall | ✔️ | ✔️ | ✔️ |

### 扫码

Expand Down
2 changes: 1 addition & 1 deletion packages/taro-h5/src/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@

#### 拨打电话

☑️ wx.makePhoneCall
wx.makePhoneCall

#### 扫码

Expand Down
34 changes: 34 additions & 0 deletions packages/taro-h5/src/api/others/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fromByteArray, toByteArray } from 'base64-js'
import { shouleBeObject, getParameterError } from '../utils'

export function arrayBufferToBase64 (arrayBuffer) {
return fromByteArray(arrayBuffer)
Expand All @@ -7,3 +8,36 @@ export function arrayBufferToBase64 (arrayBuffer) {
export function base64ToArrayBuffer (base64) {
return toByteArray(base64)
}

export function makePhoneCall (options) {
// options must be an Object
const isObject = shouleBeObject(options)
if (!isObject.res) {
const res = { errMsg: `makePhoneCall${isObject.msg}` }
console.error(res.errMsg)
return Promise.reject(res)
}

const { phoneNumber, success, fail, complete } = options
const res = { errMsg: 'makePhoneCall:ok' }

if (typeof phoneNumber !== 'string') {
res.errMsg = getParameterError({
name: 'makePhoneCall',
para: 'phoneNumber',
correct: 'String',
wrong: phoneNumber
})
console.error(res.errMsg)
typeof fail === 'function' && fail(res)
typeof complete === 'function' && complete(res)
return Promise.reject(res)
}

window.location.href = `tel:${phoneNumber}`

typeof success === 'function' && success(res)
typeof complete === 'function' && complete(res)

return Promise.resolve(res)
}

0 comments on commit 93e23f0

Please sign in to comment.