更新记录
1.0.4(2026-06-10)
修复了在vue3版本,监听函数被回收的问题
1.0.3(2026-06-05)
去掉冗余
1.0.2(2026-06-05)
持续监听修复
查看更多平台兼容性
uni-app(3.97)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | × | × | √ | √ | 5.0 | √ | 12 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × | × |
uni-app x(3.97)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| × | × | 5.0 | √ | 12 | × |
<script setup>
import {
requestPermissions,
hasPermissions,
setCallMonitorConfig,
onPhoneCallEvent,
stopMonitoring,
getStatus,
makeCall
} from '@/uni_modules/ZG-PhoneCallMonitor'
const callPhone = (phone) => {
console.log('callPhone:', phone)
// 优先使用原生直接拨号
const result = makeCall(phone)
if (!result.success) {
console.log('原生拨号失败,回退到 uni.makePhoneCall:', result.message)
// 原生不支持或失败时,回退到 uni.makePhoneCall
makePhoneCall(phone)
}
}
/**
* 开始监听
*/
const startMonitor = () => {
// 先检查权限
if (!hasPermissions()) {
uni.showModal({
title: '权限不足',
content: '请先授予电话监听权限',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
requestPermission()
}
}
})
return
}
// 配置选项(先设置配置)
setCallMonitorConfig({
enableForegroundService: true,
notificationTitle: '电话监听服务',
notificationContent: '正在后台运行,监听电话事件',
resolveContactName: true,
outgoingRetryCount: 3
})
// 注册事件回调(on 前缀 + 单 callback,UTS 自动保活)
const result = onPhoneCallEvent((event) => {
console.log('>>> 电话事件:', event.type, event.phoneNumber, event.direction, event.duration)
switch (event.type) {
case 'ringing':
addEvent('ringing', '来电响铃', `${event.phoneNumber}${event.contactName ? ' (' + event.contactName + ')' : ''}`)
callState.value = 'RINGING'
setCurrentCall({
phoneNumber: event.phoneNumber,
contactName: event.contactName,
direction: 'incoming',
state: 'RINGING',
duration: 0
})
break
case 'dialing':
addEvent('dialing', '去电拨号', `${event.phoneNumber}${event.contactName ? ' (' + event.contactName + ')' : ''}`)
callState.value = 'DIALING'
setCurrentCall({
phoneNumber: event.phoneNumber,
contactName: event.contactName,
direction: 'outgoing',
state: 'DIALING',
duration: 0
})
break
case 'answered':
addEvent('connected', '通话接通', `${event.phoneNumber} (${event.direction === 'incoming' ? '来电' : '去电'})`)
callState.value = 'OFFHOOK'
updateCurrentCall({ state: 'OFFHOOK' })
startDurationTimer()
break
case 'hangup': {
const durationText = event.duration > 0 ? `通话时长 ${event.duration}秒` : '未接通'
addEvent('disconnected', '通话挂断', `${event.phoneNumber} - ${durationText}`)
callState.value = 'IDLE'
clearCurrentCall()
clearDurationTimer()
break
}
case 'missed':
addEvent('missed', '未接来电', `${event.phoneNumber}${event.contactName ? ' (' + event.contactName + ')' : ''}`)
callState.value = 'IDLE'
clearCurrentCall()
break
case 'error':
addEvent('error', '发生错误', event.message || '未知错误')
uni.showToast({ title: event.message || '发生错误', icon: 'none' })
break
}
})
if (result.success) {
isListening.value = true
addEvent('success', '监听已启动', '开始监听电话事件')
uni.showToast({ title: '监听已启动', icon: 'success' })
} else {
addEvent('error', '启动失败', result.message)
uni.showToast({ title: result.message, icon: 'none' })
}
}
</script>

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 2
赞赏 0
下载 12436898
赞赏 1934
赞赏
京公网安备:11010802035340号