添加数据认证,需要登录用户是此数据所有者才有权限修改,查询,删除

--AgentVoicePrintService.java 修改方法定义
--AgentVoicePrintServiceImpl.java
 1.需要登录用户是此数据所有者才有权限修改,查询,删除
 2.修改发送注册声纹http请求和发送注销声纹的请求的错误说明和添加日志记录
This commit is contained in:
JianYu Zheng
2025-07-07 15:11:37 +08:00
parent ee7ea7fca6
commit b201c3cca8
2 changed files with 30 additions and 13 deletions
@@ -24,26 +24,29 @@ public interface AgentVoicePrintService {
/**
* 删除智能体的指的声纹
*
* @param userId 当前登录的用户id
* @param voicePrintId 声纹id
* @return 是否成功 T:成功 F:失败
*/
boolean delete(String voicePrintId);
boolean delete(Long userId, String voicePrintId);
/**
* 获取指定智能体的所有声纹数据
*
* @param userId 当前登录的用户id
* @param agentId 智能体id
* @return 声纹数据集合
*/
List<AgentVoicePrintVO> list(String agentId);
List<AgentVoicePrintVO> list(Long userId, String agentId);
/**
* 更新智能体的指的声纹数据
*
* @param userId 当前登录的用户id
* @param dto 修改的声纹的数据
* @return 是否成功 T:成功 F:失败
*/
boolean update(AgentVoicePrintUpdateDTO dto);
boolean update(Long userId, AgentVoicePrintUpdateDTO dto);
@@ -73,12 +73,14 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
}
@Override
public boolean delete(String voicePrintId) {
public boolean delete(Long userId, String voicePrintId) {
// 开启事务
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
try {
// 删除声纹
int row = baseMapper.deleteById(voicePrintId);
// 删除声纹,按照指定当前登录用户和智能体
int row = baseMapper.delete(new LambdaQueryWrapper<AgentVoicePrintEntity>()
.eq(AgentVoicePrintEntity::getId,voicePrintId)
.eq(AgentVoicePrintEntity::getCreator,userId));
if(row != 1){
status.setRollbackOnly(); // 标记事务回滚
return false;
@@ -95,9 +97,11 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
@Override
public List<AgentVoicePrintVO> list(String agentId) {
public List<AgentVoicePrintVO> list(Long userId, String agentId) {
// 按照指定当前登录用户和智能体查找数据
List<AgentVoicePrintEntity> list = baseMapper.selectList(new LambdaQueryWrapper<AgentVoicePrintEntity>()
.eq(AgentVoicePrintEntity::getAgentId, agentId));
.eq(AgentVoicePrintEntity::getAgentId, agentId)
.eq(AgentVoicePrintEntity::getCreator, userId));
return list.stream().map(entity -> {
// 遍历转换成AgentVoicePrintVO类型
return ConvertUtils.sourceToTarget(entity, AgentVoicePrintVO.class);
@@ -106,7 +110,13 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
}
@Override
public boolean update(AgentVoicePrintUpdateDTO dto) {
public boolean update(Long userId, AgentVoicePrintUpdateDTO dto) {
Long l = baseMapper.selectCount(new LambdaQueryWrapper<AgentVoicePrintEntity>()
.eq(AgentVoicePrintEntity::getAgentId, dto.getId())
.eq(AgentVoicePrintEntity::getCreator, userId));
if (l != 1) {
return false;
}
// 获取音频Id
String audioId = dto.getAudioId();
// 如果有新的音频
@@ -228,12 +238,14 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, requestEntity, String.class);
if (response.getStatusCode() != HttpStatus.OK) {
throw new RenException("声纹保存失败");
log.error("声纹注册失败,请求路径:{}", requestUrl);
throw new RenException("声纹保存失败,请求不成功");
}
// 检查响应内容
String responseBody = response.getBody();
if(responseBody == null || !responseBody.contains("true")){
throw new RenException("声纹保存失败");
log.error("声纹注册失败,请求处理失败内容:{}", responseBody == null ? "空内容" : responseBody);
throw new RenException("声纹保存失败,请求处理失败");
}
}
@@ -254,12 +266,14 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
// 发送 POST 请求
ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity, String.class);
if (response.getStatusCode() != HttpStatus.OK) {
throw new RenException("声纹保存失败");
log.error("声纹注销失败,请求路径:{}", requestUrl);
throw new RenException("声纹注销失败,请求不成功");
}
// 检查响应内容
String responseBody = response.getBody();
if(responseBody == null || !responseBody.contains("true")){
throw new RenException("声纹保存失败");
log.error("声纹注销失败,请求处理失败内容:{}", responseBody == null ? "空内容" : responseBody);
throw new RenException("声纹注销失败,请求处理失败");
}
}
}