xiangpei
2024-08-29 3e9a6da99aae968123ee7bca352fc08becd7f1f2
ycl-server/src/main/java/com/ycl/feign/HkApiUtil.java
@@ -4,15 +4,19 @@
import com.alibaba.fastjson2.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ycl.platform.domain.param.HK.BaseParam;
import com.ycl.platform.domain.param.HK.FaceDetectParam;
import com.ycl.utils.DateUtils;
import constant.ApiConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
/**
 * @author:xp
@@ -21,7 +25,12 @@
@Slf4j
public class HkApiUtil {
    @Value("${HK.host}")
    public static String host;
    @Value("${HK.appKey}")
    public static String appKey;
    @Value("${HK.appSecret}")
    public static String appSecret;
    /**
     * 调用海康接口
     *
@@ -30,12 +39,12 @@
     * @param resultType 响应结果接收类
     * @return 调用结果
     */
    public static <T> List<T> sendAPI(String apiUrl, Object params, Class<T> resultType) {
    public static <T> List<T> sendAPI(String apiUrl, BaseParam params, Class<T> resultType) {
        // STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
        ArtemisConfig.host = "51.92.65.49"; // 平台的ip端口
        ArtemisConfig.appKey = "29555942";  // 密钥appkey
        ArtemisConfig.appSecret = "t9U7tCplCyYHzQPPL7cH";// 密钥appSecret
        ArtemisConfig.host = host; // 平台的ip端口
        ArtemisConfig.appKey = appKey;  // 密钥appkey
        ArtemisConfig.appSecret = appSecret;// 密钥appSecret
        // STEP2:设置OpenAPI接口的上下文
        final String ARTEMIS_PATH = "/artemis";
@@ -54,22 +63,33 @@
        // STEP5:组装请求参数
        String body = JSON.toJSONString(params);
        log.info("请求参数:{}",body);
        // STEP6:调用接口
        String result = null;// post请求application/json类型参数
        try {
        List<T> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);
        } catch (Exception e) {
            log.error("接口:【{}】,调用失败");
            e.printStackTrace();
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject.getString("code") == null || !ApiConstants.HKSuccessCode.equals(jsonObject.getString("code"))){
                log.error("请求失败{}",result);
                dataList = null;
                break;
            }
            List<T> resultList = HkApiUtil.getDataList(JSONObject.parseObject(result), resultType);
            if(CollectionUtils.isEmpty(resultList) || resultList.size()<ApiConstants.HKPageSize) {
                dataList.addAll(resultList);
                break;
            }else {
                dataList.addAll(resultList);
                params.setPageNo(params.getPageNo()+1);
            }
        }
        return HkApiUtil.getDataList(JSONObject.parseObject(result), resultType);
        return dataList;
    }
    //解析数据
    private static <T> List<T> getDataList(JSONObject jsonObject, Class<T> resultClass) {
        if (jsonObject != null && ApiConstants.HKSuccessCode.equals(jsonObject.getString("code"))) {
            log.info("数据格式"+jsonObject);
            JSONObject data = jsonObject.getJSONObject("data");
            if (data == null) {
                return null;