71 lines
2.4 KiB
Java
71 lines
2.4 KiB
Java
package com.zhgd.xmgl.util;
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
import cn.hutool.crypto.SecureUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpEntity;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.util.Map;
|
|
import java.util.function.Supplier;
|
|
|
|
/**
|
|
* 携稳Util
|
|
*
|
|
* @author 邱平毅
|
|
* @ClassName XiwonUtil
|
|
* @date 2022/11/15 14:05
|
|
* @Version 1.0
|
|
*/
|
|
@Component
|
|
@Slf4j
|
|
public class XiwonUtil {
|
|
|
|
@Autowired
|
|
RestTemplate restTemplate;
|
|
|
|
|
|
public String getToken(long requestTime, String xiwonAppId, String xiwonAppSecret) {
|
|
return SecureUtil.md5(xiwonAppId + xiwonAppSecret + requestTime);
|
|
}
|
|
|
|
/**
|
|
* @param uri 如:/unload/listRealUnloadData
|
|
* @param xiwonAppId
|
|
* @param xiwonAppSecret
|
|
* @param map
|
|
* @param returnClass
|
|
* @param supplier
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public <T> T postForm(String uri, String xiwonAppId, String xiwonAppSecret, Map<String, Object> map, Class<T> returnClass, Supplier supplier) {
|
|
HttpHeaders requestHeaders = new HttpHeaders();
|
|
requestHeaders.set("appId", xiwonAppId);
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
requestHeaders.set("requestTime", currentTimeMillis + "");
|
|
requestHeaders.set("sign", getToken(currentTimeMillis, xiwonAppId, xiwonAppSecret));
|
|
requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestHeaders);
|
|
if (supplier != null) {
|
|
supplier.get();
|
|
}
|
|
|
|
String sendUrl = "http://openapi.xiwon588.com" + uri + "?" + MapUtil.join(map, "&", "=");
|
|
log.info("postForm: url:{}, appId:{}, appSecret:{}", sendUrl, xiwonAppId, xiwonAppSecret);
|
|
return restTemplate.postForObject(sendUrl, requestEntity, returnClass);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
System.out.println("requestTime>" + currentTimeMillis);
|
|
String appId = "1690940695416";
|
|
String appSecret = "b6162078-6f1c-4f2c-8cd5-0873f45199b2";
|
|
System.out.println("sign>" + SecureUtil.md5(appId + appSecret + currentTimeMillis));
|
|
}
|
|
}
|