YiDunUrlResultJob.java 10.7 KB
package com.cnlive.shenhe.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyContent;
import com.cnlive.shenhe.entity.ShyYidun;
import com.cnlive.shenhe.enums.ImageLabels;
import com.cnlive.shenhe.enums.TextLabels;
import com.cnlive.shenhe.service.ContentService;
import com.cnlive.shenhe.service.YiDunService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.HttpClientUtils;
import com.cnlive.shenhe.utils.YiDunAntiFraudUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 易盾网站检测定时拉取检测结果
 *
 * @Author: xujincheng
 * @Date: 2020/3/23 15:16
 */
@Component
public class YiDunUrlResultJob {

    private static Logger logger = LoggerFactory.getLogger(YiDunUrlResultJob.class);

    /**
     * 网站地址URL产品密钥ID,产品标识
     */
    private final static String SECRETID = "c7f33c191020bcc9cbacb7d70417fd2f";
    /**
     * 网站地址URL产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露
     */
    private final static String SECRETKEY = "833222b4951a67a9bc63b5ed0c24d07d";
    /**
     * 网站地址URL检测结果获取接口地址
     */
    private final static String URL_RESULT = "https://as.dun.163yun.com/v1/crawler/callback/results";

    @Autowired
    YiDunService yiDunService;
    @Autowired
    ContentService contentService;


    /**
     * 定时查询易盾网站检测结果接口
     * 每分钟一次
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void yiDunUrl() {
        try {
            Map<String, String> params = new HashMap<>(5);
            // 1.设置公共参数
            params.put("secretId", SECRETID);
            params.put("version", "v1.0");
            params.put("timestamp", String.valueOf(System.currentTimeMillis()));
            params.put("nonce", String.valueOf(new Random().nextInt()));

            // 2.生成签名信息
            String signature1 = YiDunAntiFraudUtil.genSignature(SECRETKEY, params);
            params.put("signature", signature1);

            // 3.发送HTTP请求
            String s1 = HttpClientUtils.post_str(URL_RESULT, params);
            logger.info("审核云查询易盾网站检测结果:{}", s1);

            // 4.解析接口返回值
            JSONObject resultObject = JSONObject.parseObject(s1);
            int code = resultObject.getIntValue("code");
            String msg = resultObject.getString("msg");
            if (code == 200 && "ok".equals(msg)) {
                JSONArray resultJsonArray = resultObject.getJSONArray("result");
                if (resultJsonArray != null && resultJsonArray.size() > 0) {
                    for (int i = 0; i < resultJsonArray.size(); i++) {
                        StringBuilder desc = new StringBuilder("易盾网站审核");
                        ShyYidun newYiDunMsg = new ShyYidun();
                        ShyContent shyContent = new ShyContent();
                        try {
                            JSONObject jsonObject = resultJsonArray.getJSONObject(i);
                            String dataId = jsonObject.getString("dataId");
                            ShyYidun yiDunMsg = yiDunService.getYiDunMsg(dataId);
                            if (yiDunMsg == null) {
                                continue;
                            }
                            newYiDunMsg.setId(yiDunMsg.getId());
                            newYiDunMsg.setDuration(-1);
                            newYiDunMsg.setUpdate_time(new Date());
                            shyContent.setId(Integer.valueOf(yiDunMsg.getContent_id()));
                            shyContent.setUpdater("机审");
                            //检测结果, 1:正常(建议通过) 2:异常(建议拦截) 3:疑似(建议人工确认)0:无结果(检测失败)
                            int result = jsonObject.getIntValue("result");
                            newYiDunMsg.setResult_type(String.valueOf(result));
                            if (result == 1) {
                                desc = new StringBuilder("易盾网站审核通过了");
                                newYiDunMsg.setDesc_msg(desc.toString());
                                newYiDunMsg.setCheck_status(0);
                                //修改图文状态
                                contentService.callback(String.valueOf(shyContent.getId()), desc.toString(), CommonConst.AUDIT_SUCCESS, "", shyContent.getUpdater());
                                logger.info("审核云结果回调业务对接方:dataId={}", dataId);
                            } else {
                                JSONObject evidences = jsonObject.getJSONObject("evidences");
                                JSONArray texts = evidences.getJSONArray("texts");
                                if (texts != null && texts.size() > 0) {
                                    for (int j = 0; j < texts.size(); j++) {
                                        JSONObject jsonObject1 = texts.getJSONObject(j);
                                        //检测结果,0:通过,1:嫌疑,2:不通过
                                        int action = jsonObject1.getIntValue("action");
                                        StringBuilder labelMsg = new StringBuilder();
                                        if (action != 0) {
                                            JSONArray labels = jsonObject1.getJSONArray("labels");
                                            labelMsg = new StringBuilder("(");
                                            if (labels != null && labels.size() > 0) {
                                                for (Object obj : labels) {
                                                    JSONObject jsonObjectone = (JSONObject) obj;
                                                    Integer label = jsonObjectone.getInteger("label");
                                                    String name = TextLabels.getName(label);
                                                    labelMsg.append(name);

                                                }
                                            }
                                        }
                                        if (action == 1) {
                                            desc.append("文字嫌疑").append(labelMsg).append(")");
                                            break;
                                        } else if (action == 2) {
                                            desc.append("文字违规").append(labelMsg).append(")");
                                            break;
                                        }
                                    }
                                }
                                JSONArray images = evidences.getJSONArray("images");
                                if (images != null && images.size() > 0) {
                                    for (int k = 0; k < images.size(); k++) {
                                        JSONObject jsonObject1 = images.getJSONObject(k);
                                        //0:正常,1:不确定,2:确定
                                        int level = jsonObject1.getIntValue("level");
                                        StringBuilder labelMsg = new StringBuilder();
                                        if (level != 0) {
                                            JSONArray labels = jsonObject1.getJSONArray("labels");
                                            labelMsg = new StringBuilder("(");
                                            if (labels != null && labels.size() > 0) {
                                                for (Object obj : labels) {
                                                    JSONObject jsonObjectone = (JSONObject) obj;
                                                    int labelsLevel = jsonObjectone.getIntValue("level");
                                                    //分类级别  0-正常 1-不确定 2-确定
                                                    if (labelsLevel != 0) {
                                                        Integer label = jsonObjectone.getInteger("label");
                                                        String name = ImageLabels.getName(label);
                                                        labelMsg.append(name);
                                                    }
                                                }
                                            }

                                        }
                                        if (level == 1) {
                                            desc.append("第").append(i + 1).append("张图片嫌疑").append(labelMsg).append(")");
                                            break;
                                        } else if (level == 2) {
                                            desc.append("第").append(i + 1).append("张图片违规").append(labelMsg).append(")");
                                            break;
                                        }
                                    }
                                }
                                newYiDunMsg.setDesc_msg(desc.toString());
                                newYiDunMsg.setCheck_status(2);
                                //修改图文状态
                                contentService.callback(String.valueOf(shyContent.getId()), desc.toString(), CommonConst.AUDIT_REFUSE, "", shyContent.getUpdater());
                            }
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                            // 领取状态为已审
                            shyContent.setReceive_status(CommonConst.RECEIVE_AUDIT);
                            shyContent.setContent_status(CommonConst.AUDIT_REFUSE);
                            //审核类型改为人工审核
                            shyContent.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
                            //拒绝原因
                            shyContent.setShenhe_msg("易盾网站识别其它问题需排查");
                            newYiDunMsg.setDesc_msg("易盾网站识别其它问题需排查");
                            contentService.updateShyContent(shyContent);
                        }
                        yiDunService.updateYiDunMsg(newYiDunMsg);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}