|
@@ -0,0 +1,67 @@
|
|
|
|
|
+package space.anyi.client;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.crypto.digest.MD5;
|
|
|
|
|
+import cn.hutool.http.Header;
|
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+import space.anyi.openApi.comom.model.User;
|
|
|
|
|
+
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @ProjectName: open-api-client
|
|
|
|
|
+ * @FileName: OpenAPIClient
|
|
|
|
|
+ * @Author: 杨逸
|
|
|
|
|
+ * @Data:2025/10/18 15:18
|
|
|
|
|
+ * @Description: 客户端
|
|
|
|
|
+ */
|
|
|
|
|
+@Data
|
|
|
|
|
+public class OpenAPIClient {
|
|
|
|
|
+ private String accessKey;
|
|
|
|
|
+ private String secretKey;
|
|
|
|
|
+ private String url;
|
|
|
|
|
+
|
|
|
|
|
+ public OpenAPIClient() {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public OpenAPIClient(String accessKey, String secretKey, String url) {
|
|
|
|
|
+ this.accessKey = accessKey;
|
|
|
|
|
+ this.secretKey = secretKey;
|
|
|
|
|
+ this.url = url;
|
|
|
|
|
+ }
|
|
|
|
|
+ public String handler(String data){
|
|
|
|
|
+ User user = new User(data);
|
|
|
|
|
+ String json = JSONUtil.toJsonStr(user);
|
|
|
|
|
+ String timestamp = System.currentTimeMillis() + "";
|
|
|
|
|
+ HttpResponse httpResponse = HttpRequest.post(url)
|
|
|
|
|
+ .header("accessKey",accessKey)//accessKey
|
|
|
|
|
+ .header("timestamp", timestamp)//时间戳
|
|
|
|
|
+ .header("sign",getSign(secretKey,timestamp))//摘要签名
|
|
|
|
|
+ .header("nonce",""+(int)Math.random()*100)//随机数
|
|
|
|
|
+ .header(Header.CONTENT_TYPE, "application/json")
|
|
|
|
|
+ .charset(StandardCharsets.UTF_8)
|
|
|
|
|
+ .body(json)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ int status = httpResponse.getStatus();
|
|
|
|
|
+ System.out.println("status = " + status);
|
|
|
|
|
+ String body = httpResponse.body();
|
|
|
|
|
+ System.out.println("body = " + body);
|
|
|
|
|
+ return body;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取摘要签名
|
|
|
|
|
+ * @param secretKey
|
|
|
|
|
+ * @param timestamp
|
|
|
|
|
+ * @return {@code String }
|
|
|
|
|
+ * @description:
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/10/18 15:21:23
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ private String getSign(String secretKey,String timestamp){
|
|
|
|
|
+ return MD5.create().digestHex16(secretKey+timestamp+secretKey);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|