uni-app uni.makePhoneCall 不会走fail的回调
uni-app uni.makePhoneCall 不会走fail的回调
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows 10 专业版 22H2 | 3.7.11 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
PC开发环境操作系统版本号:Windows 10 专业版 22H2
HBuilderX类型:正式
HBuilderX版本号:3.7.11
手机系统:Android
手机系统版本号:Android 13
手机厂商:小米
手机机型:xiaomi 13 ultra
页面类型:vue
vue版本:vue2
打包方式:云端
项目创建方式:HBuilderX
示例代码:
//调用拨打电话api
makePhone() {
let that = this
uni.makePhoneCall({
phoneNumber: '114',
success(e) {
that.$message.info(JSON.stringify(e) + 'makePhoneCall---seccess1111');
},
fail(e) {
that.$message.info(JSON.stringify(e) + 'makePhoneCall---fail2222');
},
complete(e) {
that.$message.info(JSON.stringify(e) + 'makePhoneCall---complete33333');
}
})
},
//拦截拨打电话api
uni.addInterceptor('makePhoneCall', {
invoke(res) {
},
success(e) {
// 没有权限
// message.info(JSON.stringify(e) + 'addInterceptor---seccess1111');
console.log(e, 'success makePhoneCall')
},
fail(e) {
// message.info(JSON.stringify(e) + 'addInterceptor---fail222');
// 没有权限
noPerTips(e, '请打开拨打电话权限')
},
complete(e) {
// message.info(JSON.stringify(e) +'addInterceptor---complete33333');
console.log(e, 'complete');
completeFun()
}
})
1 回复
在 UniApp 中,uni.makePhoneCall
是一个用于拨打电话的 API。根据 UniApp 的官方文档,uni.makePhoneCall
的 fail
回调通常不会触发,因为拨打电话的行为是由系统处理的,而不是由应用程序直接控制的。因此,即使拨打电话失败(例如,设备没有电话功能或用户取消了拨号),fail
回调也不会被调用。
可能的原因
- 系统处理:拨打电话的行为是由操作系统处理的,UniApp 无法直接捕获系统拨号失败的情况。
- 用户取消:如果用户在拨号界面取消了拨号,
fail
回调也不会触发。 - 设备不支持:如果设备不支持拨打电话(例如,某些平板电脑或模拟器),
fail
回调也不会触发。
解决方案
如果你需要处理拨打电话失败的情况,可以考虑以下方法:
-
检查设备功能:在调用
uni.makePhoneCall
之前,先检查设备是否支持拨打电话功能。可以使用uni.getSystemInfo
获取设备信息,判断设备是否支持电话功能。uni.getSystemInfo({ success(res) { if (!res.platform === 'ios' && !res.platform === 'android') { console.log('设备不支持拨打电话'); return; } uni.makePhoneCall({ phoneNumber: '123456789', success() { console.log('拨号成功'); }, fail() { console.log('拨号失败'); } }); } });
-
用户提示:在拨打电话之前,给用户一个提示,告知他们即将拨打电话,并让他们确认。
uni.showModal({ title: '拨打电话', content: '是否拨打电话 123456789?', success(res) { if (res.confirm) { uni.makePhoneCall({ phoneNumber: '123456789', success() { console.log('拨号成功'); }, fail() { console.log('拨号失败'); } }); } else if (res.cancel) { console.log('用户取消了拨号'); } } });
-
自定义错误处理:如果你需要更复杂的错误处理逻辑,可以在
uni.makePhoneCall
的success
回调中手动处理一些逻辑,例如检查拨号是否成功。uni.makePhoneCall({ phoneNumber: '123456789', success() { console.log('拨号成功'); // 这里可以添加一些自定义逻辑 }, fail() { console.log('拨号失败'); // 这里可以添加一些自定义逻辑 } });