HttpTemplate.java 6.69 KB
package com.lhcredit.common.utils.http;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.project.business.TianYC.entity.param.RequestParams;
import com.lhcredit.project.business.TianYC.entity.param.SFParams;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
@RequiredArgsConstructor
public class HttpTemplate {

    @Value("${dataCenters.key}")
    private String key;

    // 令牌信息
    @Value("${dataCenters.token}")
    private String token;

//    private final RestTemplate restTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public String httpGetWithHeaders(String url) {
        log.info("===============请求地址信息:{}", url);
//        HttpHeaders headers = new HttpHeaders();
//        headers.set("token", token);
//        HttpEntity<?> requestEntity = new HttpEntity<>(headers);
//        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
//        return response.getBody();
        return HttpRequest.get(url).header("token", token).execute().body();
    }

//    public String httpPost(String url, String name) {
//        HttpHeaders headers = new HttpHeaders();
//        headers.set("token", token);
//        return restTemplate.postForEntity(url, name, String.class).getBody();
//    }

    public JSONObject getResult(String url) {
        try {
            String string = httpGetWithHeaders(url);
            return JSONObject.parseObject(string);
        } catch (Exception e) {
            log.error("============调用接口失败{}", e.getMessage(), e);
            return getErr("调用接口失败", "5000");
        }
    }

    public JSONObject getPost(String url,JSONObject j){
        j.put("key",key);
        String result = HttpRequest.post(url)
                .header("Token", token)
                .header(Header.CONTENT_TYPE,"application/json")
                .body(j.toString())
                .timeout(20000)//超时,毫秒
                .execute().body();
        return JSONObject.parseObject(result);
    }


    // 返回异常信息
    public JSONObject getErr(String msg, String code) {
        JSONObject result = new JSONObject();
        result.put("msg", msg);
        result.put("code", code);
        return result;
    }

    // 组装url参数信息
    public String getTycUrl(String url, RequestParams requestParams) {
        url = getString(url, requestParams.getName(), requestParams.getPageNum(), requestParams.getPageSize());
        if (ObjectUtil.isNotNull(requestParams.getSearchType())) {
            url = url + "&searchType=" + requestParams.getSearchType();
        }
        if (StringUtils.isNotBlank(requestParams.getAppDateEnd())) {
            url = url + "&appDateBegin=" + requestParams.getAppDateEnd();
        }
        if (StringUtils.isNotBlank(requestParams.getTmClass())) {
            url = url + "&tmClass=" + requestParams.getTmClass();
        }
        if (StringUtils.isNotBlank(requestParams.getAppDateBegin())) {
            url = url + "&appDateBegin=" + requestParams.getAppDateBegin();
        }
        if (ObjectUtil.isNotNull(requestParams.getStatus())) {
            url = url + "&status=" + requestParams.getStatus();
        }
        if (StringUtils.isNotBlank(requestParams.getPubDateBegin())) {
            url = url + "&pubDateBegin=" + requestParams.getPubDateBegin();
        }
        if (StringUtils.isNotBlank(requestParams.getPubDateEnd())) {
            url = url + "&pubDateEnd=" + requestParams.getPubDateEnd();
        }
        if (ObjectUtil.isNotNull(requestParams.getPatentType())) {
            url = url + "&patentType=" + requestParams.getPatentType();
        }
        if (ObjectUtil.isNotNull(requestParams.getStatusCode())) {
            url = url + "&statusCode=" + requestParams.getStatusCode();
        }
        if (StringUtils.isNotBlank(requestParams.getWord())) {
            url = url + "&word=" + HttpUtils.urlEncode(requestParams.getWord());
        }
        if (StringUtils.isNotBlank(requestParams.getHumanName())) {
            url = url + "&humanName=" + HttpUtils.urlEncode(requestParams.getHumanName());
        }
        if (StringUtils.isNotBlank(requestParams.getStartTime())) {
            url = url + "&startTime=" + HttpUtils.urlEncode(requestParams.getStartTime());
        }
        if (StringUtils.isNotBlank(requestParams.getEndTime())) {
            url = url + "&endTime=" + HttpUtils.urlEncode(requestParams.getEndTime());
        }
        if (StringUtils.isNotBlank(requestParams.getTags())) {
            url = url + "&tags=" + HttpUtils.urlEncode(requestParams.getTags());
        }
        if (StringUtils.isNotBlank(requestParams.getHid())) {
            url = url + "&hid=" + HttpUtils.urlEncode(requestParams.getHid());
        }
        if (StringUtils.isNotBlank(requestParams.getCid())) {
            url = url + "&cid=" + HttpUtils.urlEncode(requestParams.getCid());
        }
        if (StringUtils.isNotBlank(requestParams.getAssistanceId())) {
            url = url + "&assistanceId=" + requestParams.getAssistanceId();
        }
        return url;
    }

    // 组装url参数信息
    public String getSFUrl(String url, SFParams sfParams) {
        url = getString(url, sfParams.getName(), sfParams.getPageNum(), sfParams.getPageSize());
        if (StringUtils.isNotBlank(sfParams.getId())) {
            url = url + "&id=" + sfParams.getId();
        }
        if (StringUtils.isNotBlank(sfParams.getQueryParam())) {
            url = url + "&queryParam=" + sfParams.getQueryParam();
        }
        if (StringUtils.isNotBlank(sfParams.getType())) {
            url = url + "&type=" + sfParams.getType();
        }

        System.out.println(url);
        return url;
    }

    private String getString(String url, String name, Integer pageNum, Integer pageSize) {
        url = url + "?key=" + key;
        if (StringUtils.isNotBlank(name)) {
            url = url + "&name=" + name;
        }
        if (ObjectUtil.isNotNull(pageNum) && ObjectUtil.isNotNull(pageSize)) {
            url = url + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
        }
        return url;
    }
}