RopHttpUtil.java 5.29 KB
package cn.com.glsx.entmicro.utils;

import cn.com.glsx.entmicro.response.entmicromerchant.GroupItemResponse;
import cn.com.glsx.worktool.model.response.ScrmWorktoolGroupResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilder;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: http调用rop端服务
 * @date 2021/7/30 11:26
 */
@Slf4j
public class RopHttpUtil {

    private final static String appKey = "48e5e13229b82c1b4e6e8c96151f0637";

    private final static String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";

    private static void addBaseParam(MultiValueMap<String, String> paramsMap, String method) {
        paramsMap.add("v", "1.0.0");
        paramsMap.add("method", method);
        paramsMap.add("name", "zhouhb");
    }

    public static <T> T get(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
        addBaseParam(paramsMap, method);
        MultiValueMap<String, String> params = getSysParams();
        params.putAll(paramsMap);
        for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
            if (StringUtils.isEmpty(entry.getValue())) {
                params.remove(entry.getKey());
            }
        }
        String sign = RopUtils.sign(params.toSingleValueMap(), secret);
        params.add("sign", sign);
        RestTemplate rest = new RestTemplate();
        RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(baseurl);
        uriTemplateHandler.setDefaultUriVariables(params.toSingleValueMap());
        rest.setUriTemplateHandler(uriTemplateHandler);
        T forObject = rest.getForObject(baseurl, responseType, params.toSingleValueMap());
        return forObject;
    }

    public static <T> T post(MultiValueMap<String, String> paramsMap, Class<T> responseType, String baseurl, String method) throws Exception {
        addBaseParam(paramsMap, method);
        MultiValueMap<String, String> params = getSysParams();
        params.putAll(paramsMap);
        for (Entry<String, String> entry : params.toSingleValueMap().entrySet()) {
            if (StringUtils.isEmpty(entry.getValue())) {
                params.remove(entry.getKey());
            }
        }
        String sign = RopUtils.sign(params.toSingleValueMap(), secret);
        params.add("sign", sign);
        return new RestTemplate().postForObject(baseurl, params, responseType);
    }

    public static MultiValueMap<String, String> getSysParams() {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
        params.add("appKey", appKey);
        params.add("format", "json");
        return params;
    }

    /**
     * 获取群列表
     * @param requestUrl 请求URL
     * @return 群列表
     */
    public static List<ScrmWorktoolGroupResponse> getGroupList(String requestUrl) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            log.info("获取群列表,请求URL: {}", requestUrl);
            // 发送GET请求获取群列表
            GroupItemResponse[] groupItems = restTemplate.getForObject(requestUrl, GroupItemResponse[].class);
            if (groupItems == null || groupItems.length == 0) {
                log.warn("获取群列表为空");
                return new ArrayList<>();
            }
            List<ScrmWorktoolGroupResponse> result = new ArrayList<>();
            for (GroupItemResponse item : groupItems) {
                ScrmWorktoolGroupResponse response = new ScrmWorktoolGroupResponse();
                response.setId(null);
                response.setRootId(null);
                response.setDelFlag(0);
                response.setGroupName(item.getGroupName());
                String groupRemark = item.getGroupRemark();
                response.setGroupRemark(groupRemark);
                result.add(response);
            }
            log.info("获取群列表成功,共{}个群", result.size());
            return result;
        } catch (Exception e) {
            log.error("获取群列表异常,URL: {}", requestUrl, e);
            throw new RuntimeException("获取群列表失败: " + e.getMessage(), e);
        }
    }

    static class RopUriTemplateHandler extends DefaultUriBuilderFactory {

        public RopUriTemplateHandler(String baseUriTemplate) {
            super(baseUriTemplate);
        }

        @Override
        public URI expand(String uriTemplate, Map<String, ?> uriVars) {
            UriBuilder builder = uriString(uriTemplate);
            if (uriVars != null && !uriVars.isEmpty()) {
                MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
                for (Entry<String, ?> entry : uriVars.entrySet()) {
                    params.add(entry.getKey(), entry.getValue().toString());
                }
                builder.queryParams(params);
            }
            return builder.build(uriVars);
        }
    }
}