cy
2022-11-18 08013d9f35bdfaa462916716f5c77b438af874fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.integrated.zyyt.util;
 
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.integrated.zyyt.ZyytConstant;
import com.integrated.zyyt.enetity.TokenResult;
import com.integrated.zyyt.enetity.ZyytDataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
 
/**
 * @ClassName ZyytGetData
 * @Author cy
 * @Date 2022/11/11
 * @Description
 * @Version 1.0
 **/
@Component
@Slf4j
public class ZyytUtil {
 
    /**
     * 获取批次号
     *
     * @return
     */
    public static String getBatchNo(LocalDate date) {
        if (date == null) {
            date = LocalDate.now();
        }
//        return batchNoPrefix + DateUtil.format(date, "yyyyMMddHHmmss");
        return ZyytConstant.batchNoPrefix + date;
    }
 
 
    /**
     * 获取授权
     *
     * @param url        请求地址
     * @param eciphrtext false
     * @param client     为数据服务平台API授权中的授权账号;
     * @param secret     secret 为数据服务平台API授权中的登陆密钥。
     * @return
     */
    public static TokenResult getAuth(String url, boolean eciphrtext, String client, String secret, String sourceSysKey) {
        HashMap params = new HashMap();
        params.put("ciphertext", eciphrtext);
        params.put("client", client);
        params.put("secret", secret);
 
        String httpResult = HttpRequest.post(url)
                .form(params)
                .header("X-EOS-SourceSysKey", sourceSysKey)
                .execute()
                .body();
        log.info("url =》》{}", url);
        log.info("params =》》{}", params);
        log.info("result =》》{}", httpResult);
        try {
            return JSONObject.parseObject(httpResult, TokenResult.class);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }
 
    /**
     * 获取数据
     *
     * @param url
     * @param bearToken
     * @param sourceSysKey
     * @param params
     * @return
     */
    public static <T> ZyytDataResult<T> getData(String url, String bearToken, String sourceSysKey, JSONObject params, int pageNum, int pageSize, Class<T> tClass) {
        HashMap header = new HashMap();
        header.put("Authorization", "Bearer " + bearToken);
        header.put("X-EOS-SourceSysKey", sourceSysKey);
 
        String trueUrl = url + "?pageNumber=" + pageNum + "&pageSize=" + pageSize;
        log.info("url =》》{}", trueUrl);
        log.info("header=>>bearToken》》{}\r\nSourceSysKey=>>{}", bearToken, sourceSysKey);
        log.info("params =》》{}", params);
        String httpResult = HttpRequest.post(trueUrl)
                .addHeaders(header)
                .contentType("application/json")
                .body(params.toJSONString())
                .timeout(1000 * 50)
                .execute()
                .body();
        try {
            JSONObject respJson = JSONObject.parseObject(httpResult);
            log.info("开始解析结果4");
            Long contentSize = respJson.getLong("contentSize");
            if (contentSize != null && contentSize.compareTo(0L) > 0) {
                JSONArray dataArray = respJson.getJSONArray("content");
                log.info("开始解析结果5");
                List<T> content = dataArray.toJavaList(tClass);
                respJson.remove("content");
                log.info("开始解析结果6");
                ZyytDataResult<T> zyytDataResult = respJson.toJavaObject(ZyytDataResult.class);
                zyytDataResult.setContent(content);
                return zyytDataResult;
            } else {
                log.info("result =》》{}", httpResult);
                return respJson.toJavaObject(ZyytDataResult.class);
            }
        } catch (Exception e) {
            log.error("getData 异常");
            e.printStackTrace();
            return null;
        }
    }
 
 
}