From 8f685967d3913457ba54ced829736ac3c10071f2 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 11 Jul 2025 11:49:53 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BF=AE=E5=A4=8D=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E5=A3=B0=E7=BA=B9=E6=B3=A8=E5=86=8Cbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/dto/IdentifyVoicePrintResponse.java | 3 + .../impl/AgentVoicePrintServiceImpl.java | 133 ++++++++++-------- .../src/components/VoicePrintDialog.vue | 4 +- 3 files changed, 77 insertions(+), 63 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/dto/IdentifyVoicePrintResponse.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/dto/IdentifyVoicePrintResponse.java index 3680ae49..14ce57c2 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/dto/IdentifyVoicePrintResponse.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/dto/IdentifyVoicePrintResponse.java @@ -1,5 +1,7 @@ package xiaozhi.modules.agent.dto; +import com.fasterxml.jackson.annotation.JsonProperty; + import lombok.Data; /** @@ -10,6 +12,7 @@ public class IdentifyVoicePrintResponse { /** * 最匹配的声纹id */ + @JsonProperty("speaker_id") private String speakerId; /** * 声纹的分数 diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentVoicePrintServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentVoicePrintServiceImpl.java index 928079ff..672578dc 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentVoicePrintServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentVoicePrintServiceImpl.java @@ -1,17 +1,29 @@ package xiaozhi.modules.agent.service.impl; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.stream.Collectors; + import org.apache.commons.lang3.StringUtils; import org.springframework.core.io.ByteArrayResource; -import org.springframework.http.*; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.support.TransactionTemplate; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; import xiaozhi.common.constant.Constant; import xiaozhi.common.exception.RenException; import xiaozhi.common.utils.ConvertUtils; @@ -27,12 +39,6 @@ import xiaozhi.modules.agent.service.AgentVoicePrintService; import xiaozhi.modules.agent.vo.AgentVoicePrintVO; import xiaozhi.modules.sys.service.SysParamsService; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.List; -import java.util.stream.Collectors; - /** * @author zjy */ @@ -48,17 +54,19 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl RECOGNITION){ - throw new RenException("此声音已经注册为声纹过了"); + if (response != null && response.getScore() > RECOGNITION) { + // 根据识别出的声纹ID查询对应的用户信息 + AgentVoicePrintEntity existingVoicePrint = baseMapper.selectById(response.getSpeakerId()); + String existingUserName = existingVoicePrint != null ? existingVoicePrint.getSourceName() : "未知用户"; + throw new RenException("此声音声纹对应的人(" + existingUserName + ")已经注册,请选择其他声音注册"); } AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class); // 开启事务 @@ -74,12 +82,12 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl() - .eq(AgentVoicePrintEntity::getId,voicePrintId) - .eq(AgentVoicePrintEntity::getCreator,userId)); - if(row != 1){ + .eq(AgentVoicePrintEntity::getId, voicePrintId) + .eq(AgentVoicePrintEntity::getCreator, userId)); + if (row != 1) { status.setRollbackOnly(); // 标记事务回滚 return false; } @@ -105,14 +113,12 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl list(Long userId, String agentId) { // 按照指定当前登录用户和智能体查找数据 @@ -121,17 +127,17 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl { // 遍历转换成AgentVoicePrintVO类型 - return ConvertUtils.sourceToTarget(entity, AgentVoicePrintVO.class); + return ConvertUtils.sourceToTarget(entity, AgentVoicePrintVO.class); }).toList(); } @Override public boolean update(Long userId, AgentVoicePrintUpdateDTO dto) { - AgentVoicePrintEntity agentVoicePrintEntity = - baseMapper.selectOne(new LambdaQueryWrapper() - .eq(AgentVoicePrintEntity::getId, dto.getId()) - .eq(AgentVoicePrintEntity::getCreator, userId)); + AgentVoicePrintEntity agentVoicePrintEntity = baseMapper + .selectOne(new LambdaQueryWrapper() + .eq(AgentVoicePrintEntity::getId, dto.getId()) + .eq(AgentVoicePrintEntity::getCreator, userId)); if (agentVoicePrintEntity == null) { return false; } @@ -142,15 +148,18 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl RECOGNITION){ + if (response != null && response.getScore() > RECOGNITION) { // 判断返回的id如果不是要修改的声纹id,说明这个声纹id,现在要注册的声音已经存在且不是原来的声纹,不允许修改 - if(!response.getSpeakerId().equals(dto.getId())){ - throw new RenException("此次修改不允许,此声音已经注册为声纹了"); + if (!response.getSpeakerId().equals(dto.getId())) { + // 根据识别出的声纹ID查询对应的用户信息 + AgentVoicePrintEntity existingVoicePrint = baseMapper.selectById(response.getSpeakerId()); + String existingUserName = existingVoicePrint != null ? existingVoicePrint.getSourceName() : "未知用户"; + throw new RenException("此次修改不允许,此声音已经注册为声纹了(" + existingUserName + ")"); } } } else { @@ -161,11 +170,11 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl body = new LinkedMultiValueMap<>(); body.add("speaker_id", id); @@ -285,7 +294,7 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl> requestEntity = new HttpEntity<>(headers); // 发送 POST 请求 - ResponseEntity response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity, String.class); + ResponseEntity response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity, + String.class); if (response.getStatusCode() != HttpStatus.OK) { log.error("声纹注销失败,请求路径:{}", requestUrl); throw new RenException("声纹注销失败,请求不成功"); } // 检查响应内容 String responseBody = response.getBody(); - if(responseBody == null || !responseBody.contains("true")){ + if (responseBody == null || !responseBody.contains("true")) { log.error("声纹注销失败,请求处理失败内容:{}", responseBody == null ? "空内容" : responseBody); throw new RenException("声纹注销失败,请求处理失败"); } } - /** - * 发送识别声纹http请求 - * @param agentId 智能体id + * 发送识别声纹http请求 + * + * @param agentId 智能体id * @param resource 声纹音频资源 * @return 返回识别数据 */ private IdentifyVoicePrintResponse identifyVoicePrint(String agentId, ByteArrayResource resource) { // 获取该智能体所有注册的声纹 - List agentVoicePrintList = baseMapper.selectList(new LambdaQueryWrapper() - .select(AgentVoicePrintEntity::getId) - .eq(AgentVoicePrintEntity::getAgentId,agentId)); + List agentVoicePrintList = baseMapper + .selectList(new LambdaQueryWrapper() + .select(AgentVoicePrintEntity::getId) + .eq(AgentVoicePrintEntity::getAgentId, agentId)); // 声纹数量为0,说明还没注册过声纹不需要发生识别请求 - if(agentVoicePrintList.isEmpty()) { - return null; + if (agentVoicePrintList.isEmpty()) { + return null; } // 处理声纹接口地址,获取前缀 URI uri = getVoicePrintURI(); String baseUrl = getBaseUrl(uri); - String requestUrl = baseUrl + "/voiceprint/identify"; + String requestUrl = baseUrl + "/voiceprint/identify"; // 创建请求体 MultiValueMap body = new LinkedMultiValueMap<>(); - - //创建speaker_id参数 + // 创建speaker_id参数 String speakerIds = agentVoicePrintList.stream() .map(AgentVoicePrintEntity::getId) .collect(Collectors.joining(",")); @@ -367,7 +378,7 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl - +