Authored by xutao.liu

UFO二手出售瑕疵和商品描述增加敏感词拦截

package com.yohoufo.common.helper;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.config.ConfigReader;
import com.yoho.core.rest.client.ServiceCaller;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 百度 文本审核
*
* @author zhao.xc
* @date 2021/4/28 13:36
*/
@Component
public class ReviewHelper {
private final static Logger logger = LoggerFactory.getLogger(ReviewHelper.class);
@Value("${yoho.reviewed.text.license}")
private String textLicense;
@Value("${yoho.reviewed.pic.license}")
private String picLicense;
@Resource(name = "core-config-reader")
private ConfigReader configReader;
@Autowired
private ServiceCaller serviceCaller;
/**
* 调用 百度 文本 审核
*/
public boolean reviewText(String content, Integer uid){
if(StringUtils.isEmpty(content)){
return true;
}
//文本审核降级开关 todo shit zk 上定义下设置一下
boolean isDegrade = configReader.getBoolean("sns.baiduAudit.text.degrade",false);
if(isDegrade){
logger.info("baiduApi sns.baiduAudit.text.degrade is true ");
return true;
}
try{
// JSONObject extraParams = new JSONObject();
// extraParams.put("type", type); //区分昵称还是签名
// extraParams.put("text",content);
String checkContent = reSetTextStr(content);
Map<String , String> req = new HashMap<>();
req.put("content",checkContent);
req.put("uid",String.valueOf(uid));
req.put("sceneName","COMMENT_CENSOR");
req.put("license",textLicense);
req.put("businessLine","sns");
req.put("context","sns");
// req.put("extraParams",extraParams.toJSONString());
logger.warn("begin call reviewText , checkContent is {}",checkContent);
long startTime = System.currentTimeMillis();
JSONObject result = serviceCaller.call("reviewed.textAudit", req,JSONObject.class, 5);
long endTime = System.currentTimeMillis();
logger.warn("call reviewText success, use time is {}, rsp is{}",endTime-startTime, result);
//接口返回非200,直接审核失败
if(result.getInteger("code")!=200){
return false;
}
if( result.getJSONObject("data")==null){
logger.warn("call reviewText response data is null");
return false;
}
if(result.getJSONObject("data").getInteger("resultCode") != 1){
logger.warn("call reviewText response auto fail");
return false;
}
}catch (Exception e){
logger.warn(" call reviewedText error, e is ", e);
return false;
}
logger.warn("call reviewText response auto success");
return true;
}
//过滤文本中的特殊字符
private String reSetTextStr(String text){
String regEx = "[`~!@#$%^&*()+=|{}:;\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
if(Pattern.compile(regEx).matcher(text).find()){
String textWithout = Pattern.compile(regEx).matcher(text).replaceAll("").trim();
return text + " "+ textWithout;
}
return text;
}
}
... ...
package com.yohoufo.order.service.seller.imperfect;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.req.ImperfectOrderSubmitReq;
import com.yohoufo.common.helper.ReviewHelper;
import com.yohoufo.order.model.ImperfectOrderContext;
import com.yohoufo.order.model.response.OrderSubmitResp;
import com.yohoufo.order.utils.LoggerUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* Created by chao.chen on 2019/4/3.
* @version 6.9.1
... ... @@ -25,12 +31,35 @@ public class ImperfectGoodsService {
@Autowired
private ImperfectGoodsPublishExcutor imperfectGoodsPublishExcutor;
@Autowired
private ReviewHelper reviewHelper;
public OrderSubmitResp publish(ImperfectOrderSubmitReq req){
logger.info("in ImperfectGoodsService.publish req {}", req);
//todo check req and buildSellerBo context of submit
// 验证输入内容,进行百度接口敏感词拦截
checkDescContent(req);
ImperfectOrderContext ctx = imperfectPublishProcessor.buildPublishCtx(req);
OrderSubmitResp resp = imperfectGoodsPublishExcutor.publish(ctx);
return resp;
}
/**
* 验证输入内容,进行百度接口敏感词拦截
*
* @author xutao.liu@yoho.cn
* 2021/5/7 17:35
*/
private void checkDescContent(ImperfectOrderSubmitReq req) {
String content = Optional.ofNullable(req.getOtherFlawDesc()).orElse("") + Optional.ofNullable(req.getDescribeInfo()).orElse("");
if (StringUtils.isNotBlank(content)) {
boolean auditResult = reviewHelper.reviewText(content,null);
if (!auditResult) {
throw new ServiceException(ServiceError.SNS_CONTENT_IS_NOT_LEGAL);
}
}
}
}
... ...