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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.config.ResponseStatus;
import com.topdraw.mq.consumer.UcEventBusIptv2ManagementUcEngine;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.mq.domain.SubscribeBean;
import com.topdraw.util.Base64Util;
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.util.Base64Utils;
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) {
        try {
            String url = BASE_URL + "/uce/taskOperation/dealTask";
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("content", JSON.toJSONString(dataSyncMsg));

            log.info("request url is ==>> {} || param is ==>> {} ", url, objectObjectHashMap);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, objectObjectHashMap, String.class);
            log.info("response ==>> {}", responseEntity);
            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("处理普通权益任务(ApiUti.dealTask)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

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

            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("content", content);

            log.info("request url is ==>> {} || param is ==>> {} ", url, objectObjectHashMap);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, objectObjectHashMap, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("处理微信取关(ApiUti.unsubscribe)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

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

            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("content", content);

            log.info("request url is ==>> {} || param is ==>> {} ", url, objectObjectHashMap);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, subscribeBean, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("处理微信关注(ApiUti.subscribe)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

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

            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);
        } catch (Exception e) {
            log.error("添加观影记录(ApiUti.addCollection)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

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

            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("删除一条观影记录(ApiUti.deleteCollection)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

    public JSONObject deleteAllCollection(String content) {
        try {
            String url = BASE_URL + "/uce/userOperation/deleteAllCollection";

            log.info("request url is ==>> {} || param is ==>> {} ", url, content);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("删除所有观影记录(ApiUti.deleteAllCollection)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

    public JSONObject saveGrowthReport(String content) {
        try {
            String url = BASE_URL + "/uce/userOperation/saveGrowthReport";
            String encode = Base64Utils.encodeToString(content.getBytes());
            log.info("request url is ==>> {} || param is ==>> {} ", url, encode);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, encode, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("删除所有观影记录(ApiUti.deleteAllCollection)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

    public JSONObject dealViewRecord(String content) {
        try {
            String url = BASE_URL + "/uce/userOperation/addCollection";
            //处理接口调用 中文不显示问题
            content = new String(Base64.getEncoder().encode(content.getBytes(StandardCharsets.UTF_8)));
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
            log.info("response ==>> {}", responseEntity);

            return getParseResponseResult(responseEntity);

        } catch (Exception e) {
            log.error("处理观影记录(ApiUti.dealViewRecord)信息时出现异常,cause ==>> {}", e.getMessage());
        }

        return null;
    }

    /**
     *
     * @param responseEntity
     * @return
     */
    private static JSONObject getParseResponseResult(ResponseEntity<String> responseEntity) {

        JSONObject resultSet = null;
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String entityBody = responseEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(entityBody);
            if (jsonObject.getInteger("businessCode").equals(ResponseStatus.OK)) {
                resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0);
            }
        }
        log.info("result ==>> {}", resultSet);
        return resultSet;
    }
}