| | |
| | | package com.gs.xiaomi.service; |
| | | |
| | | import cn.hutool.crypto.SmUtil; |
| | | import cn.hutool.crypto.digest.DigestUtil; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.gs.xiaomi.config.DataAcquisitionConfiguration; |
| | |
| | | * @param request BCS101请求参数 |
| | | * @return API响应 |
| | | * @throws IOException 网络异常 |
| | | * 接口返回 |
| | | * Error: ERROR_IN_MODULECHAIN;ERROR_IN_MODULECHAIN, Sender Channel 'CC_MI_OEM_HTTP_OUT' (ID: a58369adbaa03aafb76d2c02c2ae7cd7): Catching exception calling messaging system |
| | | * 我是需要X5协议的, 我看你没有为我添加 |
| | | * <p> |
| | | * HTTP访问需要Baisc Auth(该账户为条码集成专用账户,密码切勿输入错误!): |
| | | * 账户: RFCBCSXIAOBU |
| | | * 密码: L5X8!pdL |
| | | * 如何使用: |
| | | * 使用http 调用时需要根据账户密码增加http headers参数Authorization |
| | | * 对应的值为Basic bas64(账户:密码) |
| | | * <p> |
| | | * X5协议中使用的(国密算法) |
| | | * 对应appid和对应appkey请使用小米条码平台分配的对应的测试机信息: |
| | | * APP ID:bcs_fty_177301 |
| | | * APP Key:IJBVGMJXG4ZTAMLSMFXGI33NONQWY5DG |
| | | * <p> |
| | | * HTTP HEADER参数: |
| | | * Authorization Basic XXXXX |
| | | * 同时请添加HTTP HEADER参数: |
| | | * Content-Type application/x-www-form-urlencoded |
| | | * body选择raw, |
| | | * 数据格式为data=url编码(base64编码)的数据 |
| | | * </p> |
| | | */ |
| | | public String getBCS101Data(BCS101Request request) throws IOException { |
| | | // 直接将请求对象转换为JSON字符串 |
| | | String jsonData = objectMapper.writeValueAsString(request); |
| | | |
| | | // Base64编码 |
| | | String base64Data = X5StringUtils.encodeBase64(jsonData); |
| | | |
| | | // URL编码 |
| | | String urlEncodedData = URLEncoder.encode(base64Data, String.valueOf(StandardCharsets.UTF_8)); |
| | | |
| | | // 构建form表单数据 - 按照注释要求:data=url编码(base64编码)的数据 |
| | | String formData = "data=" + urlEncodedData; |
| | | |
| | | // 构建Basic Auth - 使用专用条码集成账户 |
| | | String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; |
| | | String auth = X5StringUtils.encodeBase64(credentials); |
| | | |
| | | // 构建请求体 - Content-Type: application/x-www-form-urlencoded |
| | | 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) |
| | | .build(); |
| | | |
| | | // 执行请求 |
| | | try (Response response = client.newCall(httpRequest).execute()) { |
| | | if (response.body() != null) { |
| | | String responseBody = response.body().string(); |
| | | // 记录响应信息用于调试 |
| | | System.out.println("BCS101 API响应状态: " + response.code()); |
| | | System.out.println("BCS101 API响应内容: " + responseBody); |
| | | return responseBody; |
| | | } |
| | | throw new IOException("Empty response body"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取BCS101数据 - 带X5协议支持 |
| | | * |
| | | * @param request BCS101请求参数 |
| | | * @param enableX5 是否启用X5协议国密签名 |
| | | * @return API响应 |
| | | * @throws IOException 网络异常 |
| | | */ |
| | | public String getBCS101DataWithX5(BCS101Request request, boolean enableX5) throws IOException { |
| | | // 直接将请求对象转换为JSON字符串 |
| | | String jsonData = objectMapper.writeValueAsString(request); |
| | | |
| | |
| | | // 构建form表单数据 |
| | | String formData = "data=" + urlEncodedData; |
| | | |
| | | // 生成时间戳 |
| | | String timestamp = String.valueOf(System.currentTimeMillis() / 1000); |
| | | |
| | | // 生成X5签名 |
| | | String signature = generateX5Signature( |
| | | DataAcquisitionConfiguration.BCS_APP_ID, |
| | | DataAcquisitionConfiguration.BCS_APP_Key, |
| | | timestamp, |
| | | formData |
| | | ); |
| | | |
| | | // 构建Authorization header |
| | | // 构建Basic Auth |
| | | String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; |
| | | String auth = X5StringUtils.encodeBase64(credentials); |
| | | |
| | | // 构建请求体 |
| | | RequestBody body = RequestBody.create(formData, MediaType.parse("application/x-www-form-urlencoded")); |
| | | |
| | | // 构建请求 |
| | | Request httpRequest = new Request.Builder() |
| | | Request.Builder requestBuilder = 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", DataAcquisitionConfiguration.BCS_APP_ID) |
| | | .addHeader("X-Timestamp", timestamp) |
| | | .addHeader("X-Signature", signature) |
| | | .build(); |
| | | .addHeader("Authorization", "Basic " + auth); |
| | | |
| | | // 如果启用X5协议,添加国密签名 |
| | | if (enableX5) { |
| | | String timestamp = String.valueOf(System.currentTimeMillis() / 1000); |
| | | String signature = generateX5Signature( |
| | | DataAcquisitionConfiguration.BCS_APP_ID, |
| | | DataAcquisitionConfiguration.BCS_APP_Key, |
| | | timestamp, |
| | | formData |
| | | ); |
| | | requestBuilder.addHeader("X-App-Id", DataAcquisitionConfiguration.BCS_APP_ID) |
| | | .addHeader("X-Timestamp", timestamp) |
| | | .addHeader("X-Signature", signature); |
| | | } |
| | | |
| | | Request httpRequest = requestBuilder.build(); |
| | | |
| | | // 执行请求 |
| | | try (Response response = client.newCall(httpRequest).execute()) { |
| | | if (response.body() != null) { |
| | | return response.body().string(); |
| | | String responseBody = response.body().string(); |
| | | System.out.println("BCS101 API响应状态: " + response.code()); |
| | | System.out.println("BCS101 API响应内容: " + responseBody); |
| | | return responseBody; |
| | | } |
| | | throw new IOException("Empty response body"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 生成X5协议签名 |
| | | * 生成X5协议签名 - 使用国密算法 |
| | | * |
| | | * @param appId APP ID |
| | | * @param appKey APP Key |
| | |
| | | * @return 签名字符串 |
| | | */ |
| | | private String generateX5Signature(String appId, String appKey, String timestamp, String data) { |
| | | // X5签名算法: MD5(appId + appKey + timestamp + data) |
| | | // X5协议国密算法签名: 尝试使用SM3,如果不支持则使用MD5 |
| | | String signString = appId + appKey + timestamp + data; |
| | | return DigestUtil.md5Hex(signString).toUpperCase(); |
| | | try { |
| | | // 尝试使用国密SM3算法 |
| | | return SmUtil.sm3(signString).toUpperCase(); |
| | | } catch (Exception e) { |
| | | // 如果SM3不可用,fallback到MD5 |
| | | System.out.println("SM3算法不可用,使用MD5替代: " + e.getMessage()); |
| | | return DigestUtil.md5Hex(signString).toUpperCase(); |
| | | } |
| | | } |
| | | } |