From a92e3aeb50efc6ef252d0fbaa2d45b4b7a3f5c61 Mon Sep 17 00:00:00 2001 From: 啊鑫 <t2856754968@163.com> Date: 星期三, 20 八月 2025 16:06:44 +0800 Subject: [PATCH] 条码接口测试 --- src/main/java/com/gs/xiaomi/service/BCS101ApiService.java | 140 ++++++++++++++++++++++++++++++++++++++++++++++ src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java | 20 ++++++ 2 files changed, 160 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/gs/xiaomi/service/BCS101ApiService.java b/src/main/java/com/gs/xiaomi/service/BCS101ApiService.java index 0e4cfb4..f1c36eb 100644 --- a/src/main/java/com/gs/xiaomi/service/BCS101ApiService.java +++ b/src/main/java/com/gs/xiaomi/service/BCS101ApiService.java @@ -271,4 +271,144 @@ throw new IOException("Empty response body"); } } + + /** + * X5鍗忚鏍囧噯瀹炵幇鐗堟湰 + * 鍩轰簬娉ㄩ噴瑕佹眰锛歑5鍗忚涓娇鐢ㄧ殑锛堝浗瀵嗙畻娉曪級 + * APP ID锛歜cs_fty_177301, APP Key锛欼JBVGMJXG4ZTAMLSMFXGI33NONQWY5DG + */ + public String getBCS101DataX5Standard(BCS101Request request) throws IOException { + System.out.println("=== BCS101 API X5鍗忚鏍囧噯瀹炵幇 ==="); + + // 1. 鏋勫缓JSON鏁版嵁 + String jsonData = objectMapper.writeValueAsString(request); + System.out.println("1. 鍘熷JSON: " + jsonData); + + // 2. Base64缂栫爜 + String base64Data = X5StringUtils.encodeBase64(jsonData); + System.out.println("2. Base64缂栫爜: " + base64Data); + + // 3. URL缂栫爜 + String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8"); + System.out.println("3. URL缂栫爜: " + urlEncodedData); + + // 4. 鏋勫缓form鏁版嵁 + String formData = "data=" + urlEncodedData; + System.out.println("4. Form鏁版嵁: " + formData); + + // 5. X5鍗忚鍙傛暟 + String appId = DataAcquisitionConfiguration.BCS_APP_ID; + String appKey = DataAcquisitionConfiguration.BCS_APP_Key; + String timestamp = String.valueOf(System.currentTimeMillis() / 1000); + String nonce = String.valueOf(System.currentTimeMillis()); // 浣跨敤鏃堕棿鎴充綔涓簄once + + // 6. X5鍗忚绛惧悕 - 鏍囧噯绠楁硶: hash(appId + timestamp + nonce + appKey + data) + String signString = appId + timestamp + nonce + appKey + formData; + String signature; + try { + signature = SmUtil.sm3(signString); + System.out.println("5. 浣跨敤SM3绛惧悕"); + } catch (Exception e) { + signature = DigestUtil.md5Hex(signString).toUpperCase(); + System.out.println("5. 浣跨敤MD5绛惧悕 (SM3涓嶅彲鐢�)"); + } + + System.out.println("6. 绛惧悕瀛楃涓�: " + signString); + System.out.println("7. 绛惧悕鍊�: " + signature); + + // 7. Basic Auth + String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; + String auth = X5StringUtils.encodeBase64(credentials); + + // 8. 鏋勫缓璇锋眰浣� + RequestBody body = RequestBody.create(formData, MediaType.parse("application/x-www-form-urlencoded")); + + // 9. 鏋勫缓璇锋眰 - 娣诲姞X5鍗忚鏍囧噯澶撮儴 + Request httpRequest = new Request.Builder() + .url(DataAcquisitionConfiguration.BCS_101_URL) + .method("POST", body) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .addHeader("Authorization", "Basic " + auth) + .addHeader("X-App-Id", appId) + .addHeader("X-Timestamp", timestamp) + .addHeader("X-Nonce", nonce) + .addHeader("X-Signature", signature) + .build(); + + System.out.println("8. 鍙戦�乆5鏍囧噯璇锋眰..."); + + // 10. 鎵ц璇锋眰 + try (Response response = client.newCall(httpRequest).execute()) { + System.out.println("9. 鍝嶅簲鐘舵��: " + response.code()); + System.out.println("10. 鍝嶅簲澶�: " + response.headers()); + + if (response.body() != null) { + String responseBody = response.body().string(); + System.out.println("11. 鍝嶅簲鍐呭: " + responseBody); + return responseBody; + } + throw new IOException("Empty response body"); + } + } + + /** + * X5鍗忚鍙樹綋2 - 涓嶅悓鐨勭鍚嶇畻娉� + * 绛惧悕绠楁硶: hash(appKey + data + timestamp + appId) + */ + public String getBCS101DataX5Variant(BCS101Request request) throws IOException { + System.out.println("=== BCS101 API X5鍗忚鍙樹綋2 ==="); + + // 1. 鏋勫缓鏁版嵁 + String jsonData = objectMapper.writeValueAsString(request); + String base64Data = X5StringUtils.encodeBase64(jsonData); + String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8"); + String formData = "data=" + urlEncodedData; + + // 2. X5鍗忚鍙傛暟 + String appId = DataAcquisitionConfiguration.BCS_APP_ID; + String appKey = DataAcquisitionConfiguration.BCS_APP_Key; + String timestamp = String.valueOf(System.currentTimeMillis() / 1000); + + // 3. 鍙樹綋绛惧悕绠楁硶: appKey + data + timestamp + appId + String signString = appKey + formData + timestamp + appId; + String signature; + try { + signature = SmUtil.sm3(signString); + System.out.println("浣跨敤SM3绛惧悕鍙樹綋"); + } catch (Exception e) { + signature = DigestUtil.md5Hex(signString).toUpperCase(); + System.out.println("浣跨敤MD5绛惧悕鍙樹綋"); + } + + System.out.println("绛惧悕瀛楃涓�(鍙樹綋): " + signString); + System.out.println("绛惧悕鍊�(鍙樹綋): " + signature); + + // 4. Basic Auth + String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; + String auth = X5StringUtils.encodeBase64(credentials); + + // 5. 鏋勫缓璇锋眰 - 浣跨敤甯歌鐨刋5澶撮儴鍚嶇О + RequestBody body = RequestBody.create(formData, MediaType.parse("application/x-www-form-urlencoded")); + Request httpRequest = new Request.Builder() + .url(DataAcquisitionConfiguration.BCS_101_URL) + .method("POST", body) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .addHeader("Authorization", "Basic " + auth) + .addHeader("appId", appId) + .addHeader("timestamp", timestamp) + .addHeader("signature", signature) + .build(); + + System.out.println("鍙戦�乆5鍙樹綋璇锋眰..."); + + try (Response response = client.newCall(httpRequest).execute()) { + System.out.println("鍙樹綋鍝嶅簲鐘舵��: " + response.code()); + if (response.body() != null) { + String responseBody = response.body().string(); + System.out.println("鍙樹綋鍝嶅簲鍐呭: " + responseBody); + return responseBody; + } + throw new IOException("Empty response body"); + } + } } diff --git a/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java b/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java index 45cc4df..3b4e7cd 100644 --- a/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java +++ b/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java @@ -83,5 +83,25 @@ System.out.println("BCS101 API 绠�鍖栫増鏈皟鐢ㄥ紓甯�: " + e.getMessage()); e.printStackTrace(); } + + System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圶5鍗忚鏍囧噯鐗堟湰锛� ==="); + try { + // 璋冪敤X5鍗忚鏍囧噯鐗堟湰 + String x5StandardResponse = bcs101ApiService.getBCS101DataX5Standard(request); + System.out.println("BCS101 API X5Standard Response: " + x5StandardResponse); + } catch (Exception e) { + System.out.println("BCS101 API X5鏍囧噯鐗堟湰璋冪敤寮傚父: " + e.getMessage()); + e.printStackTrace(); + } + + System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圶5鍗忚鍙樹綋鐗堟湰锛� ==="); + try { + // 璋冪敤X5鍗忚鍙樹綋鐗堟湰 + String x5VariantResponse = bcs101ApiService.getBCS101DataX5Variant(request); + System.out.println("BCS101 API X5Variant Response: " + x5VariantResponse); + } catch (Exception e) { + System.out.println("BCS101 API X5鍙樹綋鐗堟湰璋冪敤寮傚父: " + e.getMessage()); + e.printStackTrace(); + } } } -- Gitblit v1.9.3