提交 b7d2be5a 编写于 作者: minghao.wu's avatar minghao.wu

feature: 增加processTextV2版本 返回所有的hint

上级 f971cca3
流水线 #2173 已通过 于阶段
in 24 second
...@@ -13,8 +13,13 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -13,8 +13,13 @@ import org.springframework.web.bind.annotation.RequestMethod;
*/ */
@FeignClient(value="10902-PICA-CLOUD-ANALYSIS") @FeignClient(value="10902-PICA-CLOUD-ANALYSIS")
public interface AnalysisFeignService { public interface AnalysisFeignService {
@RequestMapping(value = "/analysis/antispam/text", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @RequestMapping(value = "/analysis/antispam/text/v1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
PicaResponse processText(@RequestBody String content, PicaResponse processTextV1(@RequestBody String content,
@RequestHeader(value = "sysCode") String sysCode,
@RequestHeader(value = "token") String token);
@RequestMapping(value = "/analysis/antispam/text/v2", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
PicaResponse processTextV2(@RequestBody String content,
@RequestHeader(value = "sysCode") String sysCode, @RequestHeader(value = "sysCode") String sysCode,
@RequestHeader(value = "token") String token); @RequestHeader(value = "token") String token);
} }
...@@ -9,7 +9,12 @@ import com.pica.cloud.online.exam.analysis.client.service.AnalysisFeignService; ...@@ -9,7 +9,12 @@ import com.pica.cloud.online.exam.analysis.client.service.AnalysisFeignService;
*/ */
public class AnalysisFeignServiceImpl implements AnalysisFeignService { public class AnalysisFeignServiceImpl implements AnalysisFeignService {
@Override @Override
public PicaResponse processText(String content, String sysCode, String token) { public PicaResponse processTextV1(String content, String sysCode, String token) {
return null;
}
@Override
public PicaResponse processTextV2(String content, String sysCode, String token) {
return null; return null;
} }
} }
...@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.foundation.entity.PicaResponse; import com.pica.cloud.foundation.entity.PicaResponse;
import com.pica.cloud.foundation.entity.PicaResultCode; import com.pica.cloud.foundation.entity.PicaResultCode;
import com.pica.cloud.foundation.redis.RedisClient; import com.pica.cloud.foundation.redis.RedisClient;
import com.pica.cloud.foundation.utils.annotation.LoginPermission;
import com.pica.cloud.foundation.utils.entity.PicaUser; import com.pica.cloud.foundation.utils.entity.PicaUser;
import com.pica.cloud.online.exam.analysis.common.CommonUtils; import com.pica.cloud.online.exam.analysis.common.CommonUtils;
import com.pica.cloud.online.exam.analysis.common.util.ReturnUtil; import com.pica.cloud.online.exam.analysis.common.util.ReturnUtil;
...@@ -29,15 +30,52 @@ public class AntiSpamController { ...@@ -29,15 +30,52 @@ public class AntiSpamController {
@Autowired @Autowired
AntiSpamService antiSpamService; AntiSpamService antiSpamService;
@LoginPermission
@ApiOperation(value = "内容检测", response = PicaResponse.class) @ApiOperation(value = "内容检测", response = PicaResponse.class)
@RequestMapping(value = "/text", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @RequestMapping(value = "/text", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public PicaResponse processText(@RequestBody String content, public PicaResponse processText(@RequestBody String content,
@RequestHeader String sysCode, @RequestHeader String sysCode,
@RequestHeader String token) { @RequestHeader String token) {
if (token == null) { PicaUser user = CommonUtils.getUserByToken(redisClient, token);
return ReturnUtil.getPicaResponse(PicaResultCode.SYSTEM_NO_TOKEN);
if (user == null || user.getId().intValue() == 0) {
return ReturnUtil.getPicaResponse(PicaResultCode.LOGIN_FAILE);
}
StringBuilder sbContent = new StringBuilder();
/**
* type:0 不检测广告 其他: 检测广告, 默认不检测
*/
Integer type = 0;
try {
JSONObject jsonObject = JSONObject.parseObject(content);
sbContent.append(jsonObject.get("content").toString());
type = (jsonObject.get("type") != null ? Integer.parseInt(jsonObject.get("type").toString()) : 0);
} catch (Exception e) {
return ReturnUtil.getPicaResponse(PicaResultCode.PARAM_IS_INVALID);
}
return antiSpamService.processString(
user.getId(),
sysCode,
sbContent.toString(),
type != 0
);
} }
@ApiOperation(value = "内容检测", response = PicaResponse.class)
@RequestMapping(value = "/text/v1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public PicaResponse processTextV1(@RequestBody String content,
@RequestHeader String sysCode,
@RequestHeader String token) {
return processText(content, sysCode, token);
}
@ApiOperation(value = "内容检测", response = PicaResponse.class)
@RequestMapping(value = "/text/v2", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public PicaResponse processTextV2(@RequestBody String content,
@RequestHeader String sysCode,
@RequestHeader String token) {
PicaUser user = CommonUtils.getUserByToken(redisClient, token); PicaUser user = CommonUtils.getUserByToken(redisClient, token);
if (user == null || user.getId().intValue() == 0) { if (user == null || user.getId().intValue() == 0) {
...@@ -57,7 +95,7 @@ public class AntiSpamController { ...@@ -57,7 +95,7 @@ public class AntiSpamController {
return ReturnUtil.getPicaResponse(PicaResultCode.PARAM_IS_INVALID); return ReturnUtil.getPicaResponse(PicaResultCode.PARAM_IS_INVALID);
} }
return antiSpamService.processString( return antiSpamService.processStringV2(
user.getId(), user.getId(),
sysCode, sysCode,
sbContent.toString(), sbContent.toString(),
......
...@@ -17,6 +17,8 @@ public interface AntiSpamService { ...@@ -17,6 +17,8 @@ public interface AntiSpamService {
*/ */
PicaResponse processString(Integer userId, String sysCode, String content, boolean isAdFilterOpen); PicaResponse processString(Integer userId, String sysCode, String content, boolean isAdFilterOpen);
PicaResponse processStringV2(Integer userId, String sysCode, String content, boolean isAdFilterOpen);
boolean processImageContent(Integer userId, String sysCode, String content); boolean processImageContent(Integer userId, String sysCode, String content);
boolean processImage(Integer userId, String sysCode, String url); boolean processImage(Integer userId, String sysCode, String url);
......
...@@ -13,6 +13,7 @@ import com.pica.cloud.online.exam.analysis.server.mapper.AntiSpamRecordMapper; ...@@ -13,6 +13,7 @@ import com.pica.cloud.online.exam.analysis.server.mapper.AntiSpamRecordMapper;
import com.pica.cloud.online.exam.analysis.server.service.AntiSpamService; import com.pica.cloud.online.exam.analysis.server.service.AntiSpamService;
import com.pica.cloud.online.exam.analysis.server.utils.HttpClient4Utils; import com.pica.cloud.online.exam.analysis.server.utils.HttpClient4Utils;
import com.pica.cloud.online.exam.analysis.server.utils.SignatureUtils; import com.pica.cloud.online.exam.analysis.server.utils.SignatureUtils;
import io.swagger.models.auth.In;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -39,15 +40,8 @@ public class AntiSpamServiceImpl implements AntiSpamService { ...@@ -39,15 +40,8 @@ public class AntiSpamServiceImpl implements AntiSpamService {
private static HttpClient httpClient = HttpClient4Utils.createHttpClient(100, 20, 2000, 2000, 2000); private static HttpClient httpClient = HttpClient4Utils.createHttpClient(100, 20, 2000, 2000, 2000);
private static HttpClient httpClientImage = HttpClient4Utils.createHttpClient(100, 20, 10000, 2000, 2000); private static HttpClient httpClientImage = HttpClient4Utils.createHttpClient(100, 20, 10000, 2000, 2000);
@Override
public PicaResponse processString(Integer userId, String sysCode, String content, boolean isAdFilterOpen) {
/**
* 过滤图片
*/
if (!processImageContent(userId, sysCode, content)) {
return ReturnUtil.getPicaResponse("400002", "图片包含违禁内容,请修改后重新输入", "");
}
private String neteaseResult(String content, boolean isAdFilterOpen) {
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
/** /**
...@@ -83,7 +77,46 @@ public class AntiSpamServiceImpl implements AntiSpamService { ...@@ -83,7 +77,46 @@ public class AntiSpamServiceImpl implements AntiSpamService {
/** /**
* 4. 发送HTTP请求 * 4. 发送HTTP请求
*/ */
String response = HttpClient4Utils.sendPost(httpClient, properties.getNeteaseAntispamTextApiUrl(), params, Consts.UTF_8); return HttpClient4Utils.sendPost(httpClient, properties.getNeteaseAntispamTextApiUrl(), params, Consts.UTF_8);
}
private String getMessageByLabel(Integer label) {
String msg = "";
switch (label) {
case 100:
msg = "输入内容包含色情词汇,请修改后重新输入";
break;
case 200:
msg = "输入内容包含广告词汇,请修改后重新输入";
break;
case 400:
msg = "输入内容包含违禁词汇,请修改后重新输入";
break;
case 500:
msg = "输入内容包含涉政词汇,请修改后重新输入";
break;
case 600:
msg = "输入内容包含谩骂词汇,请修改后重新输入";
break;
case 700:
msg = "输入内容包含灌水词汇,请修改后重新输入";
break;
default:
break;
}
return msg;
}
@Override
public PicaResponse processString(Integer userId, String sysCode, String content, boolean isAdFilterOpen) {
/**
* 过滤图片
*/
if (!processImageContent(userId, sysCode, content)) {
return ReturnUtil.getPicaResponse("400002", "图片包含违禁内容,请修改后重新输入", "");
}
String response = neteaseResult(content, isAdFilterOpen);
logger.info("content:" + content + " response=" + response); logger.info("content:" + content + " response=" + response);
...@@ -122,12 +155,7 @@ public class AntiSpamServiceImpl implements AntiSpamService { ...@@ -122,12 +155,7 @@ public class AntiSpamServiceImpl implements AntiSpamService {
JsonObject detailsObject=lObject.getAsJsonObject("details"); JsonObject detailsObject=lObject.getAsJsonObject("details");
JsonArray hintArray=detailsObject.getAsJsonArray("hint"); JsonArray hintArray=detailsObject.getAsJsonArray("hint");
if (label == 100) msg = "输入内容包含色情词汇,请修改后重新输入"; msg = getMessageByLabel(label);
else if (label == 200) msg = "输入内容包含广告词汇,请修改后重新输入";
else if (label == 400) msg = "输入内容包含违禁词汇,请修改后重新输入";
else if (label == 500) msg = "输入内容包含涉政词汇,请修改后重新输入";
else if (label == 600) msg = "输入内容包含谩骂词汇,请修改后重新输入";
else if (label == 700) msg = "输入内容包含灌水词汇,请修改后重新输入";
for (JsonElement eleHint : hintArray) { for (JsonElement eleHint : hintArray) {
hint.append(eleHint.getAsString()).append(","); hint.append(eleHint.getAsString()).append(",");
...@@ -148,6 +176,75 @@ public class AntiSpamServiceImpl implements AntiSpamService { ...@@ -148,6 +176,75 @@ public class AntiSpamServiceImpl implements AntiSpamService {
return ReturnUtil.getPicaResponse(PicaResultCode.SUCCESS, ""); return ReturnUtil.getPicaResponse(PicaResultCode.SUCCESS, "");
} }
@Override
public PicaResponse processStringV2(Integer userId, String sysCode, String content, boolean isAdFilterOpen) {
/**
* 过滤图片
*/
if (!processImageContent(userId, sysCode, content)) {
return ReturnUtil.getPicaResponse("400002", "图片包含违禁内容,请修改后重新输入", "");
}
String response = neteaseResult(content, isAdFilterOpen);
logger.info("content:" + content + " response=" + response);
/**
* 5. 解析返回值
*/
JsonObject jObject = new JsonParser().parse(response).getAsJsonObject();
int code = jObject.get("code").getAsInt();
String msg = jObject.get("msg").getAsString();
if (code == 200) {
JsonObject resultObject = jObject.getAsJsonObject("result");
int action = resultObject.get("action").getAsInt();
JsonArray labelArray = resultObject.getAsJsonArray("labels");
/**
* action: 0 通过
* action: 1 嫌疑
* action: 2 不通过
*/
if (action == 0) {
insertRecord(userId, sysCode, content, response, (byte)action, 0, "");
} else {
/**
* 保留第一个label 记录所有的提示信息
*/
int resLabel = 0;
StringBuilder hint = new StringBuilder();
for (JsonElement labelElement : labelArray) {
JsonObject lObject = labelElement.getAsJsonObject();
int label = lObject.get("label").getAsInt();
JsonObject detailsObject=lObject.getAsJsonObject("details");
JsonArray hintArray=detailsObject.getAsJsonArray("hint");
if (resLabel == 0) {
resLabel = label;
msg = getMessageByLabel(label);
}
for (JsonElement eleHint : hintArray) {
hint.append(eleHint.getAsString()).append(",");
}
if (hint.length() > 0) {
hint.deleteCharAt(hint.length() - 1);
}
}
/**
* 保存记录
*/
insertRecord(userId, sysCode, content, response, (byte)action, resLabel, hint.toString());
return ReturnUtil.getPicaResponse(PicaResultCode.DATA_IS_WRONG.code(), msg, hint.toString());
}
} else {
return ReturnUtil.getPicaResponse(jObject.get("code").getAsString(), msg, "");
}
return ReturnUtil.getPicaResponse(PicaResultCode.SUCCESS, "");
}
@Override @Override
public boolean processImageContent(Integer userId, String sysCode, String content) { public boolean processImageContent(Integer userId, String sysCode, String content) {
List<String> imageUrlList = new ArrayList<>(); List<String> imageUrlList = new ArrayList<>();
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册