2023-02-16 15:28:15 +08:00
|
|
|
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.beans.factory.annotation.Value;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-02 17:59:49 +08:00
|
|
|
* 携稳Util
|
|
|
|
|
*
|
2023-02-16 15:28:15 +08:00
|
|
|
* @author 邱平毅
|
|
|
|
|
* @ClassName XiwonUtil
|
|
|
|
|
* @date 2022/11/15 14:05
|
|
|
|
|
* @Version 1.0
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class XiwonUtil {
|
|
|
|
|
|
|
|
|
|
@Value("${xiwon.appId}")
|
|
|
|
|
private String appId;
|
|
|
|
|
|
|
|
|
|
@Value("${xiwon.appSecret}")
|
|
|
|
|
private String appSecret;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
RestTemplate restTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getToken(long requestTime) {
|
|
|
|
|
return SecureUtil.md5(appId + appSecret + requestTime);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 17:59:49 +08:00
|
|
|
public <T> T postForm(String url, Map<String, Object> map, Class<T> returnClass, Supplier supplier) {
|
2023-02-16 15:28:15 +08:00
|
|
|
HttpHeaders requestHeaders = new HttpHeaders();
|
|
|
|
|
requestHeaders.set("appId", appId);
|
|
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
|
requestHeaders.set("requestTime", currentTimeMillis + "");
|
|
|
|
|
requestHeaders.set("sign", getToken(currentTimeMillis));
|
|
|
|
|
requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
|
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestHeaders);
|
|
|
|
|
if (supplier != null) {
|
|
|
|
|
supplier.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return restTemplate.postForObject(url + "?" + MapUtil.join(map, "&", "="), requestEntity, returnClass);
|
|
|
|
|
}
|
|
|
|
|
}
|