fix:无人船

This commit is contained in:
2026-04-07 22:21:37 +08:00
parent 982e4364d5
commit a338cf7d02
112 changed files with 8255 additions and 611 deletions
+6
View File
@@ -122,5 +122,11 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
@@ -29,4 +29,9 @@ public interface ServiceNameConstants {
* 消息中心服务
*/
String MSG_SERVICE = "msg-center";
/**
* 优惠券中心服务
*/
String COUPON_SERVICE = "coupon-center";
}
@@ -0,0 +1,80 @@
package com.central.common.utils.http;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
* @author lishilin
* @see
* @since 1.0
*/
@Component
public class HeaderUtils {
// @Value("${jetlinks.clientId}")
private String xClientId = "WFMfEGS8iHrEPRBH" ;
// @Value("${jetlinks.secureKey}")
private String secureKey = "ihat3zHC6T7xPZYFAEnG3tExQiFANd2n" ;
public Map<String, String> createHeadersOfParams(Map<String, Object> params) {
//时间戳
String xTimestamp = String.valueOf(System.currentTimeMillis());
//openApi客户端id
// String xClientId = "rN7cpmQb884W4hcH";
//密钥
// String secureKey = "hBYtiNxQrA8FZbtetGhCZJPi";
//将参数按ASCII排序后拼接为k1=v1&k2=v2的格式
String paramString = new TreeMap<>(params).entrySet()
.stream()
.map(e -> e.getKey().concat("=").concat(String.valueOf(e.getValue())))
.collect(Collectors.joining("&"));
System.out.println(paramString);
//param+X-Timestamp+SecureKey通过MD5加密
MessageDigest digest = DigestUtils.getMd5Digest();
System.out.println(paramString + xTimestamp + secureKey);
digest.update(paramString.getBytes());
digest.update(xTimestamp.getBytes());
digest.update(secureKey.getBytes());
Map<String, String> headers = new HashMap<>();
headers.put("X-Sign", Hex.encodeHexString(digest.digest()));
headers.put("X-Client-Id", xClientId);
headers.put("X-Timestamp", xTimestamp);
return headers;
}
public Map<String, String> createHeadersOfJsonString(String jsonString) {
//时间戳
String xTimestamp = String.valueOf(System.currentTimeMillis());
//openApi客户端id
// String xClientId = "rN7cpmQb884W4hcH";
//密钥
// String secureKey = "hBYtiNxQrA8FZbtetGhCZJPi";
//param+X-Timestamp+SecureKey通过MD5加密
MessageDigest digest = DigestUtils.getMd5Digest();
System.out.println(jsonString + xTimestamp + secureKey);
digest.update(jsonString.getBytes());
digest.update(xTimestamp.getBytes());
digest.update(secureKey.getBytes());
Map<String, String> headers = new HashMap<>(5);
headers.put("X-Sign", Hex.encodeHexString(digest.digest()));
headers.put("X-Client-Id", xClientId);
headers.put("X-Timestamp", xTimestamp);
return headers;
}
}
@@ -0,0 +1,39 @@
package com.central.common.utils.http;
import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
/**
* @author wang
* @see
* @since 1.0
*/
public interface HttpRequest extends Closeable {
Response get() throws IOException;
Response post() throws IOException;
Response put() throws IOException;
Response delete() throws IOException;
Response patch() throws IOException;
HttpRequest encode(String encode);
HttpRequest contentType(String type);
HttpRequest param(String name, String value);
HttpRequest params(Map<String, Object> params);
HttpRequest header(String name, String value);
HttpRequest headers(Map<String, String> headers);
HttpRequest requestBody(String body);
HttpRequest resultAsJsonString();
}
@@ -0,0 +1,13 @@
package com.central.common.utils.http;
import java.io.IOException;
/**
* @author wang
* @see
* @since 1.0
*/
public interface Response {
byte[] asBytes() throws IOException;
}
@@ -0,0 +1,185 @@
package com.central.common.utils.http;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author wang
* @see
* @since 1.0
*/
public class SimpleHttpRequest implements HttpRequest {
private Map<String, Object> params = new LinkedHashMap<>();
private Map<String, String> headers = new LinkedHashMap<>();
private String url;
private String requestBody;
private String contentType;
private String encode = "utf-8";
protected HttpClient httpClient;
public SimpleHttpRequest(String url) {
this.url = url;
createHttpClient();
}
public SimpleHttpRequest(String url, HttpClient client) {
this.url = url;
this.httpClient = client;
}
protected void createHttpClient() {
if (httpClient == null) {
HttpClientBuilder builder = HttpClientBuilder.create();
httpClient = builder.build();
}
}
@Override
public Response get() throws IOException {
String param = EntityUtils.toString(createUrlEncodedFormEntity());
String tmpUrl = url + (url.contains("?") ? "&" : "?") + param;
HttpGet get = new HttpGet(tmpUrl);
HttpResponse response = execute(get);
return getResultValue(response);
}
@Override
public Response post() throws IOException {
HttpPost post = new HttpPost(url);
if (requestBody != null)
post.setEntity(new StringEntity(requestBody, ContentType.create(contentType,encode)));
else {
post.setEntity(createUrlEncodedFormEntity());
}
HttpResponse response = execute(post);
return getResultValue(response);
}
@Override
public Response put() throws IOException {
HttpPut put = new HttpPut(url);
if (requestBody != null)
put.setEntity(new StringEntity(requestBody, ContentType.create(contentType, encode)));
else {
put.setEntity(createUrlEncodedFormEntity());
}
HttpResponse response = execute(put);
return getResultValue(response);
}
protected UrlEncodedFormEntity createUrlEncodedFormEntity() throws UnsupportedEncodingException {
List<NameValuePair> nameValuePair = params.entrySet()
.stream().map(stringStringEntry ->
new BasicNameValuePair(stringStringEntry.getKey(), String.valueOf(stringStringEntry.getValue())))
.collect(Collectors.toList());
return new UrlEncodedFormEntity(nameValuePair, encode);
}
@Override
public Response delete() throws IOException {
HttpDelete delete = new HttpDelete(url);
HttpResponse response = execute(delete);
return getResultValue(response);
}
@Override
public Response patch() throws IOException {
HttpPatch delete = new HttpPatch(url);
if (requestBody != null)
delete.setEntity(new StringEntity(requestBody, ContentType.create(contentType)));
else {
delete.setEntity(createUrlEncodedFormEntity());
}
return getResultValue(execute(delete));
}
@Override
public void close() throws IOException {
if (httpClient != null && httpClient instanceof CloseableHttpClient) {
((CloseableHttpClient) httpClient).close();
}
}
@Override
public HttpRequest resultAsJsonString() {
header("Accept", "application/json");
return this;
}
@Override
public HttpRequest requestBody(String body) {
contentType("application/json");
this.requestBody = body;
return this;
}
@Override
public HttpRequest encode(String encode) {
this.encode = encode;
return this;
}
@Override
public HttpRequest contentType(String type) {
this.contentType = type;
return this;
}
@Override
public HttpRequest param(String name, String value) {
this.params.put(name, value);
return this;
}
@Override
public HttpRequest params(Map<String, Object> params) {
this.params.putAll(params);
return this;
}
@Override
public HttpRequest header(String name, String value) {
this.headers.put(name, value);
return this;
}
@Override
public HttpRequest headers(Map<String, String> header) {
this.headers.putAll(header);
return this;
}
private Response getResultValue(HttpResponse res) throws IOException {
return new SimpleResponse(res);
}
private HttpResponse execute(HttpRequestBase request) throws IOException {
putHeader(request);
return httpClient.execute(request);
}
private void putHeader(HttpUriRequest request) {
headers.forEach(request::setHeader);
}
}
@@ -0,0 +1,26 @@
package com.central.common.utils.http;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* @author wang
* @see
* @since 1.0
*/
public class SimpleResponse implements Response {
HttpResponse response;
public SimpleResponse(HttpResponse response) {
this.response = response;
}
@Override
public byte[] asBytes() throws IOException {
return EntityUtils.toByteArray(response.getEntity());
}
}