uni.makePhoneCall 可兼容微信小程序、H5、移动端(安卓、IOS),但是在移动端(安卓)上,如果拒绝授权电话,则会出现点击号码,既不报错,也不弹出打电话的bug。当然,如果只是简单调用makePhoneCall ,也就不值得我去记录了
封装了一个电话处理函数,具体看注释
export const makeCall = (phone, callBack) => {
// 获取当前运行平台是否 android
if (getPlatform() === 'android') {
//拨打前先验证是否授权
plus.android.requestPermissions(
['android.permission.CALL_PHONE'],
function (resultObj) {
var result = 0
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i]
console.log('已获取的权限:' + grantedPermission)
result = 1
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i]
console.log('拒绝本次申请的权限:' + deniedPresentPermission)
result = 0
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i]
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission)
result = -1
}
console.log(result)
if (result == 1) {
uni.makePhoneCall({
phoneNumber: phone, //电话号码
success(ress) {
console.log('拨打电话成功', ress)
},
fail(err) {
console.log('拨打电话失败', 'err')
},
})
} else {
//权限为拒绝的时候,需要做的处理
callBack()
}
},
function (error) {
console.log('申请权限错误:' + error.code + ' = ' + error.message)
}
)
} else {
uni.makePhoneCall({
phoneNumber: phone,
})
}
}