RestTemplateClient.java 7.78 KB
package com.topdraw.resttemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.business.module.member.address.domain.MemberAddress;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.mq.domain.SubscribeBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;

@Slf4j
@Component
public class RestTemplateClient {

    private static RestTemplate restTemplate;

    private static String BASE_URL;

    @Autowired
    private Environment environment;

    @PostConstruct
    private void init() {
        BASE_URL = environment.getProperty("api.baseUrl");
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 设置连接超时
        factory.setConnectTimeout(5000);
        // 设置读取超时
        factory.setReadTimeout(8000);
        restTemplate = new RestTemplate(factory);
    }

    public JSONObject dealTask(DataSyncMsg dataSyncMsg) {
        String url = BASE_URL + "/uce/taskOperation/dealTask";
        log.info("request uc : url is " + url + ", dataSyncMsg is " + dataSyncMsg);
        String content = JSON.toJSONString(dataSyncMsg);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("content", content);
        log.info("===>>>" + content);
            restTemplate.postForEntity(url, objectObjectHashMap, String.class);
       /* ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, objectObjectHashMap, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String entityBody = responseEntity.getBody();444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444被44444444444444 444444 44444 44444 44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
            JSONObject jsonObject = JSONObject.parseObject(entityBody);
            if (jsonObject.getInteger("businessCode").equals(200)) {
                resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0);
            }
        }
        log.info("uc response: " + resultSet.toJSONString());
        return resultSet;*/
        return null;
    }

    public JSONObject getMemberInfo(Long memberId) {
        JSONObject resultSet = null;
        String url = BASE_URL + "/uce/member/findById/" + memberId;
        log.info("request uc : url is " + url + ", memberId is " + memberId);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String entityBody = responseEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(entityBody);
            if (jsonObject.getInteger("businessCode").equals(200)) {
                resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0);
            }
        }
        log.info("uc response: " + resultSet.toJSONString());
        return resultSet;
    }

    public String createMemberAddress(MemberAddress member) {
        String url = BASE_URL + "/uce/memberAddress/create";
        log.info("request uc : url is " + url + ", memberId is " + JSONObject.toJSONString(member));
        restTemplate.postForEntity(url, member, String.class);
       /* ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, member, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }

    public String unsubscribe(SubscribeBean subscribeBean) {
        String url = BASE_URL + "/uce/userOperation/unsubscribe";
        String content = JSON.toJSONString(subscribeBean);

        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("content", content);
        restTemplate.postForEntity(url, objectObjectHashMap, String.class);
        /*ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, subscribeBean, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }

    public String subscribe(SubscribeBean subscribeBean) {
        String url = BASE_URL + "/uce/userOperation/subscribe";
        String content = JSON.toJSONString(subscribeBean);

        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("content", content);
        log.info("objectObjectHashMap ===>> [{}]",objectObjectHashMap);
        restTemplate.postForEntity(url, objectObjectHashMap, String.class);
        /*ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, subscribeBean, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }

    public String sendQrCodeMessage(String content) {
        String url = BASE_URL + "/uce/userOperation/sendQrCodeMessage";
        restTemplate.postForEntity(url, content, String.class);
       /* ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }

    public String addCollection(String content) {
        String url = BASE_URL + "/uce/userOperation/addCollection";
        //处理接口调用 中文不显示问题
        content = new String(Base64.getEncoder().encode(content.getBytes(StandardCharsets.UTF_8)));

        restTemplate.postForEntity(url, content, String.class);
       /* ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }

    public String deleteCollection(String content) {
        String url = BASE_URL + "/uce/userOperation/deleteCollection";
        restTemplate.postForEntity(url, content, String.class);
       /* ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }

    public String deleteAllCollection(String content) {
        String url = BASE_URL + "/uce/userOperation/deleteAllCollection";
        restTemplate.postForEntity(url, content, String.class);
        /*ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
        String entityBody = "";
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            entityBody = responseEntity.getBody();
        }
        log.info("uc response: " + entityBody);*/
        return null;
    }
}