This commit is contained in:
Van0313
2025-06-17 22:04:31 +08:00
parent e0f9952773
commit 2ebcc4915d
9 changed files with 242 additions and 213 deletions

View File

@@ -0,0 +1,30 @@
package cn.van.business.controller.jd.erp;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.van.business.erp.request.AuthorizeListQueryRequest;
import cn.van.business.erp.request.ERPAccount;
import cn.van.business.erp.request.ProductListQueryRequest;
import com.alibaba.fastjson2.JSONObject;
/**
* @author Leo
* @version 1.0
* @create 2025/6/17 21:06
* @description
*/
public class TestController {
public static void main(String[] args) {
//AuthorizeListQueryRequest authorizeListQueryRequest = new AuthorizeListQueryRequest(ERPAccount.ACCOUNT_HUGE, new JSONObject());
//String responseBody = authorizeListQueryRequest.getResponseBody();
//System.out.println(responseBody);
System.out.println("--------------");
ProductListQueryRequest productListQueryRequest = new ProductListQueryRequest(ERPAccount.ACCOUNT_HUGE);
JSONObject jsonObject = new JSONObject();
jsonObject.put("page_no", 1);
jsonObject.put("page_size", 1);
productListQueryRequest.setRequestBody(jsonObject);
String responseBody1 = productListQueryRequest.getResponseBody();
System.out.println(responseBody1);
}
}

View File

@@ -0,0 +1,7 @@
package cn.van.business.erp.request;
public class AuthorizeListQueryRequest extends ERPRequestBase {
public AuthorizeListQueryRequest(ERPAccount erpAccount) {
super("https://open.goofish.pro/api/open/user/authorize/list", erpAccount);
}
}

View File

@@ -1,31 +1,31 @@
//package cn.van.business.erp.request; package cn.van.business.erp.request;
//
///** /**
// * @author Leo * @author Leo
// * @version 1.0 * @version 1.0
// * @create 2025/4/10 15:20 * @create 2025/4/10 15:20
// * @descriptionERP账户枚举类 * @descriptionERP账户枚举类
// */ */
//public enum ERPAccount { public enum ERPAccount {
// // 胡歌 // 胡歌
// ACCOUNT_HUGE("1016208368633221", "waLiRMgFcixLbcLjUSSwo370Hp1nBcBu"), ACCOUNT_HUGE("1016208368633221", "waLiRMgFcixLbcLjUSSwo370Hp1nBcBu"),
// // 刘强东 // 刘强东
// ACCOUNT_LQD("anotherApiKey", "anotherApiSecret"); ACCOUNT_LQD("anotherApiKey", "anotherApiSecret");
//
// private final String apiKey; private final String apiKey;
// private final String apiKeySecret; private final String apiKeySecret;
//
// ERPAccount(String apiKey, String apiKeySecret) { ERPAccount(String apiKey, String apiKeySecret) {
// this.apiKey = apiKey; this.apiKey = apiKey;
// this.apiKeySecret = apiKeySecret; this.apiKeySecret = apiKeySecret;
// } }
//
// public String getApiKey() { public String getApiKey() {
// return apiKey; return apiKey;
// } }
//
// public String getApiKeySecret() { public String getApiKeySecret() {
// return apiKeySecret; return apiKeySecret;
// } }
//} }
//

View File

@@ -1,20 +0,0 @@
//package cn.van.business.erp.request;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//
//public class ProductQueryRequest extends ERPRequestBase {
// public ProductQueryRequest(String url, ERPAccount erpAccount, ApifoxModel requestBody) {
// super(url, erpAccount, requestBody);
// }
//
// @Override
// public String getRequestBodyAsString() {
// try {
// ObjectMapper objectMapper = new ObjectMapper();
// return objectMapper.writeValueAsString(requestBody);
// } catch (JsonProcessingException e) {
// throw new RuntimeException("Failed to convert request body to JSON", e);
// }
// }
//}

View File

@@ -1,49 +1,59 @@
//package cn.van.business.erp.request; package cn.van.business.erp.request;
//
//import com.fasterxml.jackson.core.JsonProcessingException; import cn.hutool.http.HttpRequest;
//import com.fasterxml.jackson.databind.ObjectMapper; import com.alibaba.fastjson2.JSONObject;
//
//import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
//import java.security.MessageDigest; import java.security.MessageDigest;
//import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
//
//public abstract class ERPRequestBase { public abstract class ERPRequestBase {
// protected final String url; protected String url;
// protected final ERPAccount erpAccount; protected String sign;
// protected final Object requestBody; protected ERPAccount erpAccount;
// protected JSONObject requestBody;
// public ERPRequestBase(String url, ERPAccount erpAccount, Object requestBody) {
// this.url = url; public ERPRequestBase(String url, ERPAccount erpAccount) {
// this.erpAccount = erpAccount; this.url = url;
// this.requestBody = requestBody; this.erpAccount = erpAccount;
// } }
//
// public String getUrl() { public void setRequestBody(JSONObject requestBody) {
// return url; this.requestBody = requestBody;
// } }
//
// public ERPAccount getErpAccount() { public String genMd5(String str) {
// return erpAccount; StringBuilder result = new StringBuilder();
// } try {
// MessageDigest md = MessageDigest.getInstance("MD5");
// public abstract String getRequestBodyAsString(); byte[] digest = md.digest(str.getBytes(StandardCharsets.UTF_8));
// for (byte b : digest) {
// protected String genMd5(String str) { result.append(String.format("%02x", b & 0xff));
// StringBuilder result = new StringBuilder(); }
// try { } catch (NoSuchAlgorithmException e) {
// MessageDigest md = MessageDigest.getInstance("MD5"); throw new RuntimeException(e);
// byte[] digest = md.digest(str.getBytes(StandardCharsets.UTF_8)); }
// for (byte b : digest) { return result.toString();
// result.append(String.format("%02x", b & 0xff)); }
// }
// } catch (NoSuchAlgorithmException e) { public void genSign() {
// throw new RuntimeException(e); long timestamp = System.currentTimeMillis() / 1000;
// } String jsonStr = requestBody.toJSONString();
// return result.toString(); String data = erpAccount.getApiKey() + "," + genMd5(jsonStr) + "," + timestamp + "," + erpAccount.getApiKeySecret();
// } this.sign = genMd5(data);
// }
// protected String genSign(long timestamp, String jsonStr) {
// String data = erpAccount.getApiKey() + "," + genMd5(jsonStr) + "," + timestamp + "," + erpAccount.getApiKeySecret(); public String getRequestUrl() {
// return genMd5(data); return url+"?appid="+erpAccount.getApiKey()+"&timestamp="+System.currentTimeMillis()/1000+"&sign="+sign;
// } }
//} public String getRequestBody() {
return requestBody.toJSONString();
}
public String getResponseBody() {
genSign();
HttpRequest post = HttpRequest.post(getRequestUrl());
post.body(getRequestBody());
return post.execute().body();
}
}

View File

@@ -0,0 +1,107 @@
package cn.van.business.erp.request;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HttpTest {
private static String apiKey = "1016208368633221"; // 开放平台提供的应用KEY
private static String apiKeySecret = "o9wl81dncmvby3ijpq7eur456zhgtaxs"; // 开放平台提供的应用密钥
private static String domain = "https://open.goofish.pro"; // 域名
public static void main(String[] args) {
// 获取当前时间戳
long timestamp = System.currentTimeMillis() / 1000L;
System.out.println("timestamp: " + timestamp);
// 请求体JSON字符串
String productId = "941757976162";
String jsonBody = "{\"product_id\":" + productId + "}";
// 生成签名
String sign = genSign(timestamp, jsonBody);
System.out.println("sign: " + sign);
// 拼接请求地址
String apiUrl = domain + "/api/open/product/detail?appid=" + apiKey + "&timestamp=" + timestamp + "&sign="
+ sign;
try {
// 创建URL对象
URL url = new URL(apiUrl);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 设置请求头部
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
// 启用输出流
connection.setDoOutput(true);
// 获取输出流并写入请求体
OutputStream outputStream = connection.getOutputStream();
outputStream.write(jsonBody.getBytes(StandardCharsets.UTF_8));
outputStream.close();
// 获取响应状态码
int responseCode = connection.getResponseCode();
System.out.println("API Response Code: " + responseCode);
// 读取响应内容
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
response.append(line);
}
bufferedReader.close();
// 关闭连接
connection.disconnect();
System.out.println("API Response: " + response.toString());
} catch (IOException e) {
// 在此处处理异常
e.printStackTrace();
}
}
// md5加密
private static String genMd5(String str) {
StringBuilder result = new StringBuilder();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(str.getBytes(StandardCharsets.UTF_8));
for (byte b : digest) {
result.append(String.format("%02x", b & 0xff));
}
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return result.toString();
}
// 生成签名
private static String genSign(long timestamp, String jsonStr) {
// 拼接字符串
String data = apiKey + "," + genMd5(jsonStr) + "," + timestamp + "," + apiKeySecret;
// 商务对接模式 拼接字符串
// String data = apiKey + "," + genMd5(jsonStr) + "," + timestamp + "," + seller_id + "," + apiKeySecret;
// 生成签名
return genMd5(data);
}
}

View File

@@ -0,0 +1,7 @@
package cn.van.business.erp.request;
public class ProductListQueryRequest extends ERPRequestBase {
public ProductListQueryRequest(ERPAccount erpAccount) {
super("https://open.goofish.pro/api/open/product/list", erpAccount);
}
}

View File

@@ -1,111 +0,0 @@
//package cn.van.business.erp.request;
//
///**
// * @author Leo
// * @version 1.0
// * @create 2025/4/9 21:30
// * @description
// */
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.io.OutputStream;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.nio.charset.StandardCharsets;
//import java.security.MessageDigest;
//import java.security.NoSuchAlgorithmException;
//
//public class HttpTest {
// private static long apiKey = 203413189371893L; // 开放平台提供的应用KEY
// private static String apiKeySecret = "o9wl81dncmvby3ijpq7eur456zhgtaxs"; // 开放平台提供的应用密钥
// private static String domain = "https://open.goofish.pro"; // 域名
//
// public static void main(String[] args) {
// // 获取当前时间戳
// long timestamp = System.currentTimeMillis() / 1000L;
//
// // 请求体JSON字符串
// String productId = "220656347074629";
// String jsonBody = "{\"product_id\":" + productId + "}";
//
// // 生成签名
// String sign = genSign(timestamp, jsonBody);
//
// // 拼接请求地址
// String apiUrl = domain + "/api/open/product/detail?appid=" + apiKey + "&timestamp=" + timestamp + "&sign="
// + sign;
//
// try {
// // 创建URL对象
// URL url = new URL(apiUrl);
//
// // 打开连接
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
// // 设置请求方法为POST
// connection.setRequestMethod("POST");
//
// // 设置请求头部
// connection.setRequestProperty("Content-Type", "application/json");
// connection.setRequestProperty("Accept", "application/json");
//
// // 启用输出流
// connection.setDoOutput(true);
//
// // 获取输出流并写入请求体
// OutputStream outputStream = connection.getOutputStream();
// outputStream.write(jsonBody.getBytes(StandardCharsets.UTF_8));
// outputStream.close();
//
// // 获取响应状态码
// int responseCode = connection.getResponseCode();
// System.out.println("API Response Code: " + responseCode);
//
// // 读取响应内容
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// String line;
// StringBuilder response = new StringBuilder();
// while ((line = bufferedReader.readLine()) != null) {
// response.append(line);
// }
// bufferedReader.close();
//
// // 关闭连接
// connection.disconnect();
//
// System.out.println("API Response: " + response.toString());
//
// } catch (IOException e) {
// // 在此处处理异常
// e.printStackTrace();
// }
// }
//
// // md5加密
// private static String genMd5(String str) {
// StringBuilder result = new StringBuilder();
// try {
// MessageDigest md = MessageDigest.getInstance("MD5");
// byte[] digest = md.digest(str.getBytes(StandardCharsets.UTF_8));
// for (byte b : digest) {
// result.append(String.format("%02x", b & 0xff));
// }
// } catch (NoSuchAlgorithmException e) {
// throw new RuntimeException(e);
// }
// return result.toString();
// }
//
// // 生成签名
// private static String genSign(long timestamp, String jsonStr) {
// // 拼接字符串
// String data = apiKey + "," + genMd5(jsonStr) + "," + timestamp + "," + apiKeySecret;
//
// // 商务对接模式 拼接字符串
// // String data = apiKey + "," + genMd5(jsonStr) + "," + timestamp + "," + seller_id + "," + apiKeySecret;
//
// // 生成签名
// return genMd5(data);
// }
//}

View File

@@ -14,12 +14,11 @@ import java.util.Date;
@Component @Component
public class JwtUtils { public class JwtUtils {
private final String secret = "7b78f5cc9735091442361c78b863607d"; // 应配置在 application.yml 中 private final String secret = "7b4c86d11fe2b64a5ce4d7d83de881b4afa76bf4a8bbc13ee50676c20b14c0835dda8a78a5a795b0f2201a26f758301b56ffbc52d021e158f1365d8be22fa6d7"; // 应配置在 application.yml 中
private final long expiration = 86400000 * 7; // 24小时 private final long expiration = 86400000 * 7; // 24小时
/** /**
* 生成一个新的 JWT Token * 生成一个新的 JWT Token
*
* @param username 用户名 * @param username 用户名
* @param roles 用户角色(可选) * @param roles 用户角色(可选)
* @return 返回生成的 JWT Token 字符串 * @return 返回生成的 JWT Token 字符串