AntiFraudUtil.java 13.5 KB
package com.cnlive.shenhe.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;


/**
 * 数美 反欺诈工具类
 *
 * @author GuoYaqi
 */
public class AntiFraudUtil {

    //把变量配置到属性文件

    private static final String ACCESSKEY = "DU0tyaqrO5Vuiuiq4i8E"; // 商户的 accessKey
    private static final String REQUEST_URL_TEXT = "http://api.fengkongcloud.com/v2/saas/anti_fraud/text";
    private static final String REQUEST_URL_ARTICLE = "http://img-api.fengkongcloud.com/v1/saas/anti_fraud/article";
    private static final String REQUEST_URL_IMG = "http://img-api-bj.fengkongcloud.com/v2/saas/anti_fraud/imgs";
    private static final String REQUEST_URL_VIDEO = "http://video-api-bj.fengkongcloud.com/v2/saas/anti_fraud/video";
    private static final String RESPONSE_URL_VIDEO = "http://video-api-bj.fengkongcloud.com/v2/saas/anti_fraud/query_video";
    public static void main(String[] args) {
        String jsonArray = "[{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},\n" +
                "{\"imageUrl\":\"https://wjj.ys1.cnliveimg.com/769/img/2018/0129/fitnessPhotos.jpg\"},{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"},{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},\n" +
                "{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"},{\"imageUrl\":\"http://ffyl.ys2.cnliveimg.com/223/img/2018/0820/ff80808164a7163a016555c9a6ac0208/000002.jpg?imageView2/1/w/704/h/396\"},{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"},{\"imageUrl\":\"http://yweb3.cnliveimg.com/828/img/2018/0802/180802161358328_692.jpg\"}]";
        UUID uuid = UUID.randomUUID();
        String btId = uuid.toString();

//        System.out.println("文本:"+AntiFraudUtil.filterText("0000001", "王八蛋", "DYNAMIC_USER") + "---");
        System.out.println(AntiFraudUtil.filterArticle("000111","3g.huabian.com/api/hz0227/show/2225530.html",null));
        //System.out.println("图片:"+AntiFraudUtil.filterImg("0000001", jsonArray,"DYNAMIC_BUSINESS") + "---");
        //System.out.println("视频:"+AntiFraudUtil.filterVideo("http://wjj.ys1.cnliveimg.com/testVideo/test1.mp4",btId,null) + "---");
        //queryVideoResult("02b649b7-3451-403e-abd0-592302cec2a0");

    }

    /**
     * 智能文本过滤
     *
     * @param text 需要过滤的文本
     * @param tokenId 建议传UID
     * @param channel COMMENT,DYNAMIC_BUSINESS,DYNAMIC_USER,不传为COMMENT
     * @return 通过:PASS  拒绝:REJECT  可疑:REVIEW  数美异常:null
     */
    public static JSONObject filterText(String tokenId, String text, String channel) {
        try {
            JSONObject result = reqTextFilter(tokenId, text, channel);
            
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            if (result != null) {
                Integer code = result.getInteger("code");
                
                if (code != null && code == 1100) {  //请求成功
                	String riskLevel = result.getString("riskLevel");
                	
                	if ("PASS".equals(riskLevel)) {
                		return result;
                	}
                	
                	if ("REJECT".equals(riskLevel)) {
                		JSONObject detail = result.getJSONObject("detail");

                		result.put("desc",
                				String.format("(%s)", detail.getString("riskTypeDesc")));
                		return result;
                	}
                	
                	if ("REVIEW".equals(riskLevel)) {
                		return result;
                	}
                }
            }
            return null;
        } catch (Exception e) {
        	return null;
        }
    }


    /**
     * 智能图文过滤
     * @param channel COMMENT,NEWS(新闻),CULTURE_SPORTS(娱乐/体育),不传为COMMENT
     * @param contents 要检测的网页内容,url链接或文本内容
     * @param tokenId 建议传UID
     * @return 通过:PASS  拒绝:REJECT  可疑:REVIEW  数美异常:null
     */
    public static JSONObject filterArticle(String tokenId, String contents ,String channel) {
        try {
            JSONObject result = reqArticleFilter(tokenId, contents ,channel);
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            return getResult(result);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 智能图片过滤
     *
     * @param picArray    需要过滤的图片的集合
     * @param tokenId 建议传UID
     * @return 通过:PASS  拒绝:REJECT  异常:REVIEW
     */
    public static JSONObject filterImg(String tokenId, String picArray, String channel) {
        try {
            JSONArray imgArray = getJSONArray(picArray);
            JSONObject result = reqImgFilter(tokenId, imgArray, channel);
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            if (result != null) {
                System.out.println("数美图片过滤返回结果" + result);
                JSONObject jsonObj = new JSONObject();
                StringBuffer strBuffer = new StringBuffer();
                int count = 0;
                Integer code = result.getInteger("code");
                if (code != null && code == 1100) { //请求成功
                    JSONArray jsonArray = result.getJSONArray("imgs");
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject json = jsonArray.getJSONObject(i);
                            String riskLevel = json.getString("riskLevel");

                            if ("PASS".equals(riskLevel)) {
                                continue;
                            }

                            if ("REJECT".equals(riskLevel) || "REVIEW".equals(riskLevel)) {
                                JSONObject detail = json.getJSONObject("detail");
                                for (int j = 0; j < imgArray.size(); j++) {
                                    String btId = json.getString("btId");
                                    if (imgArray.getJSONObject(j).containsValue(btId)) {
                                        strBuffer.append("(第" + (j + 1) + "张-" + detail.getString("description") + ")");
                                    }
                                }
                                jsonObj.put("desc", strBuffer);
                                count++;
                                continue;
                            }
                        }
                    }
                    if (count > 0) {
                        jsonObj.put("type", "REJECT");
                    } else {
                        jsonObj.put("type", "PASS");
                    }
                }else{//数美请求结果code为失败状态
                	jsonObj.put("type", "REJECT");
                	jsonObj.put("desc","code:"+code+",message:"+result.getString("message") );
                }
                return jsonObj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 智能视频过滤
     *
     * @param url    需要过滤的视频的url
     * @param uuid    需要过滤的视频的唯一标识
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject filterVideo(String url,String uuid, String channel) {
        try {
            JSONObject result = reqVideoFilter(url, uuid, channel);
            /**
             * 接口会返回code, code=1100 时说明请求成功,根据不同的 riskLevel 风险级别进行业务处理
             * 当 code!=1100 时,如果是 1902 错误,需要检查参数配置
             * 其余情况需要根据错误码进行重试或者其它异常处理
             */
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 智能视频过滤
     *
     * @param uuid    需要查看的视频的唯一标识
     * @return 通过:PASS  拒绝:REJECT  异常:null
     */
    public static JSONObject queryVideoResult(String uuid) {
        try {
            JSONObject userData = new JSONObject();
            userData.put("accessKey", ACCESSKEY);
            userData.put("btId", uuid);
            JSONObject result = HttpClientUtils.httpPost(RESPONSE_URL_VIDEO, userData,false);
            System.out.println("响应:" + result);
            return result;
        } catch (Exception e) {
            System.out.println("...................接口请求失败:");
            e.printStackTrace();
            return null;
        }
    }



    private static JSONObject reqTextFilter(String tokenId, String text, String channel) throws Exception {
        JSONObject userData = new JSONObject();

        userData.put("accessKey", ACCESSKEY);
        userData.put("type", "SOCIAL");

        JSONObject data = new JSONObject();
        data.put("tokenId", tokenId);
        data.put("text", text);
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_TEXT, userData,false);
    }

    private static JSONObject reqImgFilter(String tokenId, JSONArray imgs, String channel) throws Exception {
        JSONObject userData = new JSONObject();
        userData.put("accessKey", ACCESSKEY);
        userData.put("type", "AD_PORN_PERSON");

        JSONObject data = new JSONObject();
        data.put("tokenId", tokenId);

        data.put("imgs", imgs);
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_IMG, userData,false);
    }

    private static JSONObject reqVideoFilter(String url,String uuid, String channel) throws Exception {
        JSONObject userData = new JSONObject();
        userData.put("accessKey", ACCESSKEY);
        userData.put("btId", uuid);
        userData.put("callback","http://apiwjjtest.cnlive.com/Daren/filterVideoResult/callbackVideoResult.action");
        userData.put("type", "AD_PORN_PERSON_VIOLENCE");


        JSONObject data = new JSONObject();
        data.put("url", url);
        data.put("addition","AUDIO");
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_VIDEO, userData,false);
    }

    private static JSONObject reqArticleFilter(String tokenId,String contents ,String channel) throws Exception {
        JSONObject userData = new JSONObject();
        userData.put("accessKey", ACCESSKEY);

        JSONObject data = new JSONObject();
        data.put("contents", contents);
        data.put("tokenId", tokenId);
        data.put("channel", StringUtils.isNotEmpty(channel) ? channel : "COMMENT");
        userData.put("data", data);

        return HttpClientUtils.httpPost(REQUEST_URL_ARTICLE, userData,false);
    }

    private static JSONObject getResult(JSONObject result) {
        if (result != null) {
            System.out.println(result);
            Integer code = result.getInteger("code");

            if (code != null && code == 1100) {  //请求成功
                String riskLevel = result.getString("riskLevel");

                if ("PASS".equals(riskLevel)) {
                    return result;
                }

                if ("REJECT".equals(riskLevel)) {
                    JSONObject detail = result.getJSONObject("detail");

                    result.put("desc", String.format("(%s)", detail.getString("description")));
                    return result;
                }

                if ("REVIEW".equals(riskLevel)) {
                    return result;
                }
            }
        }
        return null;
    }

    private static JSONArray getJSONArray(String json){
        JSONArray picJson = JSON.parseArray(json);
        JSONArray imgs = new JSONArray();
        for (int i = 0; i < picJson.size(); i++) {
            UUID uuid = UUID.randomUUID();
            String btId = uuid.toString();
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObj = picJson.getJSONObject(i);
            String img = jsonObj.getString("imageUrl");
            jsonObject.put("img",img);
            jsonObject.put("btId",btId);
            imgs.add(jsonObject);
        }
        return imgs;
    }
}