diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java index af891dc817..72a6db9b10 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java @@ -10,12 +10,13 @@ package com.epmet.commons.tools.redis; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.exception.RenException; -import com.epmet.commons.tools.utils.ConvertUtils; -import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -import org.springframework.data.redis.core.*; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.support.atomic.RedisAtomicLong; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; @@ -49,6 +50,10 @@ public class RedisUtils { * 过期时长为1小时,单位:秒 */ public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L; + /** + * 过期时长为4小时,单位:秒 + */ + public final static long HOUR_FOUR_EXPIRE = 60 * 60 * 4L; /** * 过期时长为6小时,单位:秒 */ diff --git a/epmet-gateway/src/main/resources/bootstrap.yml b/epmet-gateway/src/main/resources/bootstrap.yml index 53ec286911..25155b37ea 100644 --- a/epmet-gateway/src/main/resources/bootstrap.yml +++ b/epmet-gateway/src/main/resources/bootstrap.yml @@ -442,6 +442,7 @@ epmet: # 内部认证url白名单(在白名单中的,就不会再校验登录了) internalAuthUrlsWhiteList: - /epmetuser/customerstaff/customerlist + - /resi/group/topic/callBackPublishTopic # 外部应用认证,使用AccessToken等头进行认证 externalOpenUrls: diff --git a/epmet-module/resi-group/resi-group-client/src/main/java/com/epmet/resi/group/dto/scanapicallback/VoiceScanCallBackContentFormDTO.java b/epmet-module/resi-group/resi-group-client/src/main/java/com/epmet/resi/group/dto/scanapicallback/VoiceScanCallBackContentFormDTO.java new file mode 100644 index 0000000000..1907cbb8cf --- /dev/null +++ b/epmet-module/resi-group/resi-group-client/src/main/java/com/epmet/resi/group/dto/scanapicallback/VoiceScanCallBackContentFormDTO.java @@ -0,0 +1,97 @@ +package com.epmet.resi.group.dto.scanapicallback; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 语音检测callBack入参 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/10 10:47 + */ +@Data +public class VoiceScanCallBackContentFormDTO implements Serializable { + + /** + * 错误码,和HTTP状态码一致。 + * 200:表示检测成功。 + * 280:表示处理中,需要继续轮询。 + * 其他:表示任务失败。 + * 更多信息,请参见公共错误码。 + * + * 说明 只有返回280表示任务还在进行,需要继续轮询该任务的检测结果。返回其他值均表示该语音检测任务已结束。如果实际上该语音检测没有结束,而是因为网络异常等原因异常结束,您可以重新提交语音异步检测任务。 + */ + private Integer code; + + /** + * 请求参数的响应信息。 + */ + private String msg; + + /** + * 检测对象对应的数据ID。 + * 说明 如果在检测请求参数中传入了dataId,则此处返回对应的dataId。 + */ + private String dataId; + + /** + * 该检测任务的ID。 + */ + private String taskId; + + /** + * 检测对象的URL。 + */ + private String url; + + /** + * 检测成功(code=200)时,返回的检测结果。该结果包含一个或多个元素,每个元素是个结构体,对应一个场景。关于每个元素的结构描述,请参见result。 + */ + private List results; + + + @Data + private static class ResultDTO{ + /** + * 检测结果的分类。取值: + * normal:正常文本 + * spam:含垃圾信息 + * ad:广告 + * politics:涉政 + * terrorism:暴恐 + * abuse:辱骂 + * porn:色情 + * flood:灌水 + * contraband:违禁 + * meaningless:无意义 + * customized:自定义(例如命中自定义关键词) + */ + private String label; + + /** + * 检测场景,和调用请求中的场景对应。唯一取值:antispam。 + */ + private String scene; + + /** + * 建议您执行的后续操作。取值: + * pass:结果正常,无需进行其余操作。 + * review:结果不确定,需要进行人工审核。 + * block:结果违规,建议直接删除或者限制公开。 + */ + private String suggestion; + + /** + * 置信度分数,取值范围:0(表示置信度最低)~100(表示置信度最高)。 + * 如果suggestion为pass,则置信度越高,表示内容正常的可能性越高;如果suggestion为review或block,则置信度越高,表示内容违规的可能性越高。 + * 注意 该值仅作为参考,强烈建议您不要在业务中使用。建议您参考suggestion和label(或者部分接口返回的sublabel)结果用于内容违规判定。 + */ + private Float rate; + //下面还有个details 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述,请参见detail。 + //暂时不解析了。 + } + + +} diff --git a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/topic/controller/ResiTopicController.java b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/topic/controller/ResiTopicController.java index 820dbc5821..18bfabad0e 100644 --- a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/topic/controller/ResiTopicController.java +++ b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/topic/controller/ResiTopicController.java @@ -1,5 +1,6 @@ package com.epmet.modules.topic.controller; +import com.alibaba.fastjson.JSON; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.Result; @@ -11,10 +12,15 @@ import com.epmet.resi.group.dto.topic.ResiTopicDTO; import com.epmet.resi.group.dto.topic.TopicInfoDTO; import com.epmet.resi.group.dto.topic.form.*; import com.epmet.resi.group.dto.topic.result.*; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @Description @@ -22,7 +28,7 @@ import java.util.List; * @Author wangc * @date 2020.03.31 13:03 */ - +@Slf4j @RestController @RequestMapping("topic") public class ResiTopicController { @@ -349,5 +355,16 @@ public class ResiTopicController { return new Result().ok(topicService.selectMyPartTopic(myPartIssueFormDTO)); } + @PostMapping("callBackPublishTopic") + public Result callBackPublishTopic(@RequestParam("checksum") String checksum, @RequestParam("content") String content, HttpServletResponse response){ + log.info("checksum:"+checksum); + log.info("content:"+ JSON.toJSONString(content,true)); + Map map=new HashMap<>(); + map.put("checksum",checksum); + map.put("content",content); + response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); + return new Result<>().ok(map); + } + } diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/VoiceSceneEnum.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/VoiceSceneEnum.java new file mode 100644 index 0000000000..777b92f0d7 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/VoiceSceneEnum.java @@ -0,0 +1,47 @@ +package com.epmet.openapi.scan.common.enu; + +import java.util.ArrayList; +import java.util.List; + +/** + * 语音异步检测场景 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 10:12 + */ +public enum VoiceSceneEnum { + ANTISPAM("antispam", "检测场景,取值:antispam"); + + private String code; + private String desc; + + VoiceSceneEnum(String code, String desc) { + this.code = code; + this.desc = desc; + } + + public static List getVoiceSceneList() { + List result = new ArrayList<>(); + VoiceSceneEnum[] values = VoiceSceneEnum.values(); + for (VoiceSceneEnum v : values) { + result.add(v.getCode()); + } + return result; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/controller/ScanController.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/controller/ScanController.java index 8a0b268e50..27965201ac 100644 --- a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/controller/ScanController.java +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/controller/ScanController.java @@ -5,13 +5,19 @@ import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.openapi.scan.service.impl.ScanService; import com.epmet.openapi.scan.support.param.ImgScanParam; import com.epmet.openapi.scan.support.param.TextScanParam; +import com.epmet.openapi.scan.support.param.VoiceAsyncScanParam; import com.epmet.openapi.scan.support.result.ImgAsyncScanResult; import com.epmet.openapi.scan.support.result.SyncScanResult; +import com.epmet.openapi.scan.support.result.VoiceAsyncScanResult; +import com.epmet.openapi.scan.support.result.VoiceAsyncScanTaskResult; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + /** * @author jianjun liu * @email liujianjun@yunzongnet.com @@ -52,4 +58,27 @@ public class ScanController { public Result ImgAsyncScan(@RequestBody ImgScanParam param) { return null;//scanService.sendASyncImgScan(param); } + + + /** + * @description 语音异步检测 + * @Date 2020/12/9 9:14 + **/ + @PostMapping("voiceAsyncScan") + public Result voiceAsyncScan(@RequestBody VoiceAsyncScanParam param){ + ValidatorUtils.validateEntity(param); + return scanService.sendVoiceAsyncScan(param); + } + + /** + * @param taskIds 要查询的异步检测任务的taskId列表。数组中的元素个数不超过100个 + * @author yinzuomei + * @description 语音异步检测结果查询 + * @Date 2020/12/9 11:16 + **/ + @PostMapping("voiceResults") + public Result> voiceResults(@RequestBody List taskIds){ + return scanService.voiceResults(taskIds); + } + } diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/redis/ScanRedis.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/redis/ScanRedis.java new file mode 100644 index 0000000000..294d40e04c --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/redis/ScanRedis.java @@ -0,0 +1,42 @@ +/** + * Copyright 2018 人人开源 https://www.renren.io + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.epmet.openapi.scan.redis; + +import com.epmet.commons.tools.redis.RedisUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * 用户居民端注册信息表 用户在居民端完善的个人信息 + * + * @author generator generator@elink-cn.com + * @since v1.0.0 2020-03-30 + */ +@Component +public class ScanRedis { + @Autowired + private RedisUtils redisUtils; + + public void test(){ + redisUtils.set("yinzuomei", "尹作梅测试用",RedisUtils.MINUTE_THIRTY_EXPIRE); + } + + public void setVoiceScanKey(String taskId, String seed) { + redisUtils.set(taskId, seed,RedisUtils.HOUR_FOUR_EXPIRE); + } +} \ No newline at end of file diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanService.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanService.java index f48937bc42..d238ef95e5 100644 --- a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanService.java +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanService.java @@ -3,7 +3,12 @@ package com.epmet.openapi.scan.service.impl; import com.epmet.commons.tools.utils.Result; import com.epmet.openapi.scan.support.param.ImgScanParam; import com.epmet.openapi.scan.support.param.TextScanParam; +import com.epmet.openapi.scan.support.param.VoiceAsyncScanParam; import com.epmet.openapi.scan.support.result.SyncScanResult; +import com.epmet.openapi.scan.support.result.VoiceAsyncScanResult; +import com.epmet.openapi.scan.support.result.VoiceAsyncScanTaskResult; + +import java.util.List; /** * desc:内容扫描接口 @@ -27,4 +32,20 @@ public interface ScanService { * @return */ public Result sendSyncImgScan(ImgScanParam imgScanParam); + + /** + * 语音异步检测 + * + * @param param + * @return com.epmet.openapi.scan.support.result.VoiceAsyncScanTaskResult + */ + Result sendVoiceAsyncScan(VoiceAsyncScanParam param); + + /** + * 语音异步检测结果查询 + * + * @param taskIds 要查询的异步检测任务的taskId列表。数组中的元素个数不超过100个 + * @return com.epmet.openapi.scan.support.result.VoiceAsyncScanResult + */ + Result> voiceResults(List taskIds); } diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java index 2a5db725e2..6ea86b824f 100644 --- a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java @@ -6,32 +6,30 @@ import com.alibaba.fastjson.JSONObject; import com.aliyuncs.AcsRequest; import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest; import com.aliyuncs.green.model.v20180509.TextScanRequest; +import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest; +import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.epmet.commons.tools.utils.Result; import com.epmet.openapi.scan.common.constant.SysConstant; -import com.epmet.openapi.scan.common.enu.ImgSceneEnum; -import com.epmet.openapi.scan.common.enu.SuggestionEnum; -import com.epmet.openapi.scan.common.enu.SysResponseEnum; -import com.epmet.openapi.scan.common.enu.TextSceneEnum; +import com.epmet.openapi.scan.common.enu.*; import com.epmet.openapi.scan.common.exception.ExecuteHttpException; import com.epmet.openapi.scan.common.util.IAcsClientUtil; -import com.epmet.openapi.scan.support.param.ImgScanParam; -import com.epmet.openapi.scan.support.param.ImgTask; -import com.epmet.openapi.scan.support.param.TextScanParam; -import com.epmet.openapi.scan.support.param.TextTask; -import com.epmet.openapi.scan.support.result.ScanTaskResult; -import com.epmet.openapi.scan.support.result.SceneDetailResult; -import com.epmet.openapi.scan.support.result.SyncScanResult; +import com.epmet.openapi.scan.redis.ScanRedis; +import com.epmet.openapi.scan.support.param.*; +import com.epmet.openapi.scan.support.result.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.io.UnsupportedEncodingException; import java.util.List; +import java.util.UUID; /** * @author jianjun liu @@ -45,7 +43,8 @@ public class ScanServiceImpl implements ScanService { private String regionId; @Value("${aliyun.green.bizType}") private String bizType; - + @Autowired + private ScanRedis scanRedis; @Override public Result sendTextScan(TextScanParam textScanParam) { //默认参数 @@ -263,4 +262,193 @@ public class ScanServiceImpl implements ScanService { throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg()); } } + + /** + * @author yinzuomei + * @description 语音异步检测 + * @Date 2020/12/9 10:02 + */ + @Override + public Result sendVoiceAsyncScan(VoiceAsyncScanParam voiceAsyncScanParam) { + //检测对象不能为空,且最多支持100个元素 + List voiceTasks = voiceAsyncScanParam.getTasks(); + if (CollectionUtils.isEmpty(voiceTasks) || voiceTasks.size() > SysConstant.MAX_TASK_SIZE) { + return new Result().error(SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getCode(), + SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getMsg().concat(SysConstant.MAX_TASK_SIZE.toString())); + } + + //默认参数 + voiceAsyncScanParam.setScenes(VoiceSceneEnum.getVoiceSceneList()); + voiceAsyncScanParam.setBizType(bizType); + // 如果是语音流检测,则修改为true。现在默认为音频文件检测false + voiceAsyncScanParam.setLive(false); + voiceAsyncScanParam.setSeed(UUID.randomUUID().toString().replace("-", "")); + + VoiceAsyncScanRequest voiceAsyncScanRequest=getVoiceAsyncScanRequest(); + + try { + voiceAsyncScanRequest.setHttpContent(JSON.toJSONString(voiceAsyncScanParam).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON); + } catch (UnsupportedEncodingException e) { + log.error("sendVoiceAsyncScan parse param exception", e); + return new Result().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg()); + } + log.info("语音异步检测入参:"+JSON.toJSONString(voiceAsyncScanParam,true)); + List taskList; + try { + taskList = executeSyncVoice(voiceAsyncScanRequest); + log.info("语音异步检测返参taskList:"+JSON.toJSONString(taskList,true)); + } catch (ExecuteHttpException e) { + log.error("sendVoiceAsyncScan execute Exception", e); + return new Result().error(e.getCode(), e.getMsg()); + } + //成功返回 + VoiceAsyncScanTaskResult resultDto=new VoiceAsyncScanTaskResult(); + resultDto.setSeed(voiceAsyncScanParam.getSeed()); + resultDto.setTaskList(taskList); + if (StringUtils.isNotBlank(voiceAsyncScanParam.getCallback())) { + // 存储seed和 任务id、dataId的关系 用于回调鉴权 + log.info("need to save seed and taskId、dataId的关系"); + taskList.forEach(task -> { + scanRedis.setVoiceScanKey(task.getTaskId(), voiceAsyncScanParam.getSeed()); + }); + } + return new Result().ok(resultDto); + } + + /** + * @param + * @author yinzuomei + * @description 构造语音异步检测请求对象 + * @Date 2020/12/9 10:44 + **/ + private VoiceAsyncScanRequest getVoiceAsyncScanRequest() { + VoiceAsyncScanRequest voiceAsyncScanRequest = new VoiceAsyncScanRequest(); + // 指定api返回格式 + voiceAsyncScanRequest.setAcceptFormat(FormatType.JSON); + // 指定请求方法 + voiceAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); + voiceAsyncScanRequest.setEncoding(SysConstant.UTF8); + voiceAsyncScanRequest.setRegionId(regionId); + voiceAsyncScanRequest.setConnectTimeout(3000); + voiceAsyncScanRequest.setReadTimeout(6000); + return voiceAsyncScanRequest; + } + + /** + * @param voiceAsyncScanRequest + * @author yinzuomei + * @description 解析返参 + * @Date 2020/12/9 10:44 + **/ + private List executeSyncVoice(AcsRequest voiceAsyncScanRequest) { + try { + HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(voiceAsyncScanRequest); + + if (httpResponse.isSuccess()) { + + JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); + //后面注释掉此日志 + log.info("VoiceAsyncScanRequest原生接口返参:" + JSON.toJSONString(scrResponse, true)); + + if (HttpStatus.SC_OK == scrResponse.getInteger(SysConstant.CODE)) { + + //获取data列表 + JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA); + List resultList = dataResults.toJavaList(VoiceAsyncScanTaskDataDTO.class); + //成功返回 + return resultList; + + } else { + + log.warn("executeSyncVoice detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE)); + throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(), + SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); + } + + } else { + log.warn("executeSyncVoice response status is not success. httpResponse:{}", JSON.toJSONString(httpResponse)); + throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getCode(), + SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); + } + + } catch (Exception e) { + log.error("executeSyncVoice exception IAcsClientUtil do action exception", e); + throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg()); + } + } + + + /** + * @param taskIds 要查询的异步检测任务的taskId列表。数组中的元素个数不超过100个 + * @author yinzuomei + * @description 语音异步检测结果查询 + * @Date 2020/12/9 11:17 + **/ + @Override + public Result> voiceResults(List taskIds) { + //检测对象不能为空,且最多支持100个元素 + if (CollectionUtils.isEmpty(taskIds) || taskIds.size() > SysConstant.MAX_TASK_SIZE) { + return new Result>().error(SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getCode(), + SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getMsg().concat(SysConstant.MAX_TASK_SIZE.toString())); + } + + VoiceAsyncScanResultsRequest request=getVoiceAsyncScanResultsRequest(); + try { + request.setHttpContent(JSON.toJSONString(taskIds).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON); + } catch (UnsupportedEncodingException e) { + log.error("getVoiceAsyncScanResult parse param exception", e); + return new Result>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg()); + } + + log.info("语音异步检测结果查询入参:"+JSON.toJSONString(taskIds,true)); + + try { + HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(request); + if (httpResponse.isSuccess()) { + + JSONObject scrResponse=JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); + //后面注释掉此返参 + log.info("VoiceAsyncScanResultsRequest原生接口返参:"+JSON.toJSONString(scrResponse, true)); + if (HttpStatus.SC_OK == scrResponse.getInteger(SysConstant.CODE)) { + //获取data列表 + JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA); + List resultList = dataResults.toJavaList(VoiceAsyncScanResult.class); + //成功返回 + return new Result>().ok(resultList); + + }else{ + + log.warn("getVoiceAsyncScanResult detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE)); + throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(), + SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); + } + + } else { + log.warn("语音异步检测结果查询预警 getVoiceAsyncScanResult response status is not success. httpResponse:{}", JSON.toJSONString(httpResponse)); + throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getCode(), + SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); + } + } catch (Exception e) { + log.error("executeSyncVoice exception IAcsClientUtil do action exception", e); + throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg()); + } + + } + + private VoiceAsyncScanResultsRequest getVoiceAsyncScanResultsRequest(){ + VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest(); + // 指定API返回格式。 + getResultsRequest.setAcceptFormat(FormatType.JSON); + // 指定请求方法。 + getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); + getResultsRequest.setEncoding(SysConstant.UTF8); + getResultsRequest.setRegionId(regionId); + /** + * 请务必设置超时时间。 + */ + getResultsRequest.setConnectTimeout(3000); + getResultsRequest.setReadTimeout(6000); + return getResultsRequest; + } + } diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceAsyncScanParam.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceAsyncScanParam.java new file mode 100644 index 0000000000..d83b8784a0 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceAsyncScanParam.java @@ -0,0 +1,63 @@ +package com.epmet.openapi.scan.support.param; + +import lombok.Data; + +import javax.validation.Valid; +import javax.validation.constraints.NotEmpty; +import java.io.Serializable; +import java.util.List; + +/** + * 音频审查 入参 + * 参考文档:https://help.aliyun.com/document_detail/89630.html?spm=a2c4g.11186623.2.12.51a32dbfW6AdqV#reference-bcf-3nk-z2b + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 9:07 + */ +@Data +public class VoiceAsyncScanParam implements Serializable { + private static final long serialVersionUID = 3408043673247901184L; + + /** + * 不必填 + * 该字段用于标识您的业务场景。您可以通过内容安全控制台创建业务场景(具体操作,请参见自定义机审标准),或者提交工单联系我们帮助您创建业务场景。 + */ + private String bizType; + + /** + * 必填 + * 检测场景,取值:antispam。 + */ + private List scenes; + + /** + * 不必填 + * 是否为语音流(例如直播流)检测。取值: + * true:表示语音流检测。 + * false(默认):表示音频文件检测。 + */ + private Boolean live; + + /** + * 不必填 + * 是否为近线检测模式。 取值: + * true:表示近线检测模式。近线检测模式下,您提交的任务不保证能够实时处理,但是可以排队并在24小时内开始检测。 + * false(默认):表示实时检测模式。对于超过了并发路数限制的检测请求会直接拒绝。 + * 说明 该参数仅适用于音频文件检测,不适用于语音流检测。 + */ + private Boolean offline; + + /** + * 异步检测结果回调地址,执行异步审查内容时 必填 + */ + private String callback; + + /** + * 随机字符串,该值用于回调通知请求中的签名,使用callback时 必填 + */ + private String seed; + + @Valid + @NotEmpty(message = "任务列表不能为空") + private List tasks; + +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceTask.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceTask.java new file mode 100644 index 0000000000..89f4333bba --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceTask.java @@ -0,0 +1,31 @@ +package com.epmet.openapi.scan.support.param; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * 语音异步检测 对象 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 10:16 + */ +@Data +public class VoiceTask implements Serializable { + /** + * 不必填 + * 要检测的数据id 非必填 + * 检测对象对应的数据ID。 + * 由大小写英文字母、数字、下划线(_)、短划线(-)、英文句号(.)组成,不超过128个字符,可以用于唯一标识您的业务数据。 + * */ + @NotBlank(message = "dataId不能为空") + private String dataId; + + /** + * 必填 + * 需要检测的音频文件或语音流的下载地址。 + */ + @NotBlank(message = "音频URL不能为空") + private String url; +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanDetailDTO.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanDetailDTO.java new file mode 100644 index 0000000000..7e74caf5a6 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanDetailDTO.java @@ -0,0 +1,47 @@ +package com.epmet.openapi.scan.support.result; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 语音异步检测结果查询结果返参 -data-result-detail详情 + * 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 11:11 + */ +@Data +public class VoiceAsyncScanDetailDTO implements Serializable { + private static final long serialVersionUID = -2664219492371705160L; + /** + * 句子开始的时间,单位:秒。 + */ + private Integer startTime; + + /** + * 句子结束的时间,单位:秒。 + */ + private Integer endTime; + + /** + * 语音转换成文本的结果。 + */ + private String text; + + /** + * 检测结果的分类。取值: + * normal:正常文本 + * spam:含垃圾信息 + * ad:广告 + * politics:涉政 + * terrorism:暴恐 + * abuse:辱骂 + * porn:色情 + * flood:灌水 + * contraband:违禁 + * meaningless:无意义 + * customized:自定义(例如命中自定义关键词) + */ + private String label; +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java new file mode 100644 index 0000000000..09fd13ec73 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java @@ -0,0 +1,51 @@ +package com.epmet.openapi.scan.support.result; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 语音异步检测结果查询结果返参 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 10:56 + */ +@Data +public class VoiceAsyncScanResult implements Serializable { + private static final long serialVersionUID = 8702129292884498023L; + /** + * 错误码,和HTTP状态码一致。 + * 200:表示检测成功。 + * 280:表示处理中,需要继续轮询。 + * 其他:表示任务失败。 + * 更多信息,请参见公共错误码。 + */ + private String code; + /** + * 错误描述信息。 + */ + private String msg; + /** + * 检测对象对应的数据ID。 + */ + private String dataId; + + /** + * 检测任务的ID + */ + private String taskId; + + /** + * 暂时没用,所以返回忽略 + */ + @JsonIgnore + private String url; + + /** + * 检测成功(code=200)时,返回的检测结果。该结果包含一个或多个元素,每个元素是个结构体,对应一个场景。关于每个元素的结构描述,请参见result。 + */ + private List results; + +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResultDTO.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResultDTO.java new file mode 100644 index 0000000000..34f3280a6f --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResultDTO.java @@ -0,0 +1,57 @@ +package com.epmet.openapi.scan.support.result; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 语音异步检测结果查询结果返参 -data-result详情 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 11:07 + */ +@Data +public class VoiceAsyncScanResultDTO implements Serializable { + private static final long serialVersionUID = 4602226363259983753L; + /** + * 检测场景,和调用请求中的场景对应。取值:antispam。 + */ + private String scene; + + /** + * 检测结果的分类。取值: + * normal:正常文本 + * spam:含垃圾信息 + * ad:广告 + * politics:涉政 + * terrorism:暴恐 + * abuse:辱骂 + * porn:色情 + * flood:灌水 + * contraband:违禁 + * meaningless:无意义 + * customized:自定义(例如命中自定义关键词) + */ + private String label; + + /** + * 建议您执行的后续操作。取值: + * pass:结果正常,无需进行其余操作。 + * review:结果不确定,需要进行人工审核。 + * block:结果违规,建议直接删除或者限制公开。 + */ + private String suggestion; + + /** + * 置信度分数,取值范围:0(表示置信度最低)~100(表示置信度最高)。 + * 如果suggestion为pass,则置信度越高,表示内容正常的可能性越高;如果suggestion为review或block,则置信度越高,表示内容违规的可能性越高。 + * 注意 该值仅作为参考,强烈建议您不要在业务中使用。建议您参考suggestion和label(或者部分接口返回的sublabel)结果用于内容违规判定。 + */ + private Float rate; + + /** + * 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述, + */ + private List details; +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskDataDTO.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskDataDTO.java new file mode 100644 index 0000000000..6c94bfa757 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskDataDTO.java @@ -0,0 +1,40 @@ +package com.epmet.openapi.scan.support.result; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; + +import java.io.Serializable; + +/** + * 描述一下 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 16:38 + */ +@Data +public class VoiceAsyncScanTaskDataDTO implements Serializable { + /** + * 错误码,和HTTP状态码一致。 + * 更多信息,请参见公共错误码。 + */ + private String code; + /** + * 错误描述信息。 + */ + private String msg; + /** + * 检测对象对应的数据ID。 + */ + private String dataId; + + /** + * 检测任务的ID + */ + private String taskId; + + /** + * 暂时没用,所以返回忽略 + */ + @JsonIgnore + private String url; +} diff --git a/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskResult.java b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskResult.java new file mode 100644 index 0000000000..44d54fcf3a --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskResult.java @@ -0,0 +1,22 @@ +package com.epmet.openapi.scan.support.result; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 音频审查返参,用于接收taskId + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/9 9:23 + */ +@Data +public class VoiceAsyncScanTaskResult implements Serializable { + private static final long serialVersionUID = 8702129292884498023L; + /** + * 随机字符串,该值用于回调通知请求中的签名。 + */ + private String seed; + private List taskList; +} diff --git a/epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanRequestSample.java b/epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanRequestSample.java new file mode 100644 index 0000000000..617e6246d1 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanRequestSample.java @@ -0,0 +1,90 @@ +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest; +import com.aliyuncs.http.FormatType; +import com.aliyuncs.http.HttpResponse; +import com.aliyuncs.profile.DefaultProfile; +import com.aliyuncs.profile.IClientProfile; + +import java.util.*; + +/** + * 提交语音异步检测任务 demo + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/8 21:51 + */ +public class VoiceAsyncScanRequestSample extends BaseSample { + + + + public static void main(String[] args) throws Exception { + + //请替换成您自己的AccessKey ID、AccessKey Secret。 + IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", + "LTAI4G6Fv6uTzQbpsayATHq4", //您自己的AccessKey ID + "QevMw1RYCwQUG3RSMPq1J6EAfmSblo");//您自己的AccessKey Secret + final IAcsClient client = new DefaultAcsClient(profile); + + VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); //class different vs common + asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。 + asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。 + asyncScanRequest.setRegionId("cn-shanghai"); + asyncScanRequest.setConnectTimeout(3000); + asyncScanRequest.setReadTimeout(6000); + + List> tasks = new ArrayList>(); + Map task1 = new LinkedHashMap(); + // 请将下面的地址修改为要检测的语音文件的地址。 + task1.put("dataId","voice1"); + task1.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/6480bd6be9f14a458162218cea84dfa5.aac"); + + Map task2 = new LinkedHashMap(); + task2.put("dataId","voice2"); + task2.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/b566d94fd7114ffb9a203e80c22ebb96.aac"); + + tasks.add(task1); + tasks.add(task2); + + JSONObject data = new JSONObject(); + + System.out.println("==========Task count:" + tasks.size()); + data.put("scenes", Arrays.asList("antispam")); + data.put("tasks", tasks); + // 如果是语音流检测,则修改为true。 + data.put("live", false); + asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON); + System.out.println("接口入参:"+JSON.toJSONString(data, true)); + + try { + HttpResponse httpResponse = client.doAction(asyncScanRequest); + + if (httpResponse.isSuccess()) { + JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); + System.out.println("接口返参:"+JSON.toJSONString(scrResponse, true)); + if (200 == scrResponse.getInteger("code")) { + JSONArray taskResults = scrResponse.getJSONArray("data"); + for (Object taskResult : taskResults) { + Integer code = ((JSONObject) taskResult).getInteger("code"); + if (200 == code) { + final String taskId = ((JSONObject) taskResult).getString("taskId"); + System.out.println("submit async task success, taskId = [" + taskId + "]"); + } else { + System.out.println("task process fail: " + code); + } + } + } else { + System.out.println("detect not success. code: " + scrResponse.getInteger("code")); + } + } else { + System.out.println("response not success. status: " + httpResponse.getStatus()); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } +} diff --git a/epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanResultsRequestSample.java b/epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanResultsRequestSample.java new file mode 100644 index 0000000000..85d268f175 --- /dev/null +++ b/epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanResultsRequestSample.java @@ -0,0 +1,113 @@ +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest; +import com.aliyuncs.http.FormatType; +import com.aliyuncs.http.HttpResponse; +import com.aliyuncs.profile.DefaultProfile; +import com.aliyuncs.profile.IClientProfile; + +import java.util.*; + +/** + * 查询异步语音检测结果。 + * + * @author yinzuomei@elink-cn.com + * @date 2020/12/8 22:08 + */ +public class VoiceAsyncScanResultsRequestSample extends BaseSample { + + + public static void main(String[] args) throws Exception { + // 请替换成您自己的AccessKey ID、AccessKey Secret。 + IClientProfile profile = DefaultProfile + .getProfile("cn-shanghai", + "LTAI4G6Fv6uTzQbpsayATHq4", + "QevMw1RYCwQUG3RSMPq1J6EAfmSblo"); + final IAcsClient client = new DefaultAcsClient(profile); + //提交语音异步检测任务后返回的taskId + pollingScanResult(client, Arrays.asList("vc_f_7WZZ01BCH0q6ZnM3g1OKGG-1tAaZ7", "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi")); + // pollingScanResult(client, "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi"); + } + + public static void pollingScanResult(IAcsClient client, List taskIdList) throws InterruptedException { + int failCount = 0; + boolean stop = false; + do { + // 设置每10秒查询一次。 + Thread.sleep(10 * 1000); + JSONObject scanResult = getScanResult(client, taskIdList); + System.out.println("接口完整返参:"+JSON.toJSONString(scanResult, true)); + if (scanResult == null || 200 != scanResult.getInteger("code")) { + failCount++; + System.out.println("请求失败,get result fail, failCount=" + failCount); + if (scanResult != null) { + System.out.println("请求失败,错误信息errorMsg:" + scanResult.getString("msg")); + } + if (failCount > 20) { + break; + } + continue; + } + + JSONArray taskResults = scanResult.getJSONArray("data"); + if (taskResults.isEmpty()) { + System.out.println("请求成功,but data is empty"); + break; + } + System.out.println("data.size=" + taskResults.size()); + for (Object taskResult : taskResults) { + JSONObject result = (JSONObject) taskResult; + Integer code = result.getInteger("code"); + String taskId = result.getString("taskId"); + if (280 == code) { + System.out.println("taskId=" + taskId + ": processing status: " + result.getString("msg")); + } else if (200 == code) { + System.out.println("taskId=" + taskId + "请求成功,返参:" + JSON.toJSONString(taskResult, true)); + stop = true; + } else { + System.out.println("taskId=" + taskId + "请求失败,返参:" + JSON.toJSONString(taskResult, true)); + stop = true; + } + } + } while (!stop); + } + + private static JSONObject getScanResult(IAcsClient client, List taskIdList) { + VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest(); + getResultsRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。 + getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。 + getResultsRequest.setEncoding("utf-8"); + getResultsRequest.setRegionId("cn-shanghai"); + + + List> tasks = new ArrayList>(); + for (String taskId : taskIdList) { + Map task1 = new LinkedHashMap(); + task1.put("taskId", taskId); + tasks.add(task1); + } + + /** + * 请务必设置超时时间。 + */ + getResultsRequest.setConnectTimeout(3000); + getResultsRequest.setReadTimeout(6000); + + try { + getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON); + + HttpResponse httpResponse = client.doAction(getResultsRequest); + if (httpResponse.isSuccess()) { + return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); + } else { + System.out.println("response not success. status: " + httpResponse.getStatus()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } +}