diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/common/ApiEntity.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/common/ApiEntity.java new file mode 100644 index 0000000..6c07a27 --- /dev/null +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/common/ApiEntity.java @@ -0,0 +1,105 @@ +package shkd.sys.sys.common; + +import com.alibaba.fastjson.JSONObject; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.util.ArrayList; +import java.util.Map; + +/** + * @Description + * @Author Tao + * @Date 2024/11/14 + */ +public class ApiEntity { + private String URL;//链接 + private String method;//请求方式 + private Map queryParams;//请求参数 + private Map headers;//请求头参数 + private JSONObject requestBody;//请求体 + + public String getURL() { + return URL; + } + + public void setURL(String URL) { + this.URL = URL; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public Map getQueryParams() { + return queryParams; + } + + public void setQueryParams(Map queryParams) { + this.queryParams = queryParams; + } + + public Map getHeaders() { + return headers; + } + + public void setHeaders(Map headers) { + this.headers = headers; + } + + public JSONObject getRequestBody() { + return requestBody; + } + + public void setRequestBody(JSONObject requestBody) { + this.requestBody = requestBody; + } + + public ApiEntity(String URL, String method, Map queryParams, Map headers, JSONObject requestBody) { + this.URL = URL; + this.method = method; + this.queryParams = queryParams; + this.headers = headers; + this.requestBody = requestBody; + } + + public ApiEntity() { + } + + public static JSONObject getResponseBody(ApiEntity apiEntity) { + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders httpHeaders = new HttpHeaders(); + Map heads = apiEntity.getHeaders(); + if (heads != null) { + for (String key : heads.keySet()) { + httpHeaders.add(key, heads.get(key).toString()); + } + } + + HttpEntity objectHttpEntity = new HttpEntity<>(httpHeaders); + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(apiEntity.getURL()); + Map params = apiEntity.getQueryParams(); + if (params != null) { + for (String key : params.keySet()) { + uriComponentsBuilder.queryParam(key, params.get(key)); + } + } + + HttpMethod method; + if (apiEntity.getMethod().equals("POST")) { + method = HttpMethod.POST; + } else { + method = HttpMethod.GET; + } + ResponseEntity exchange = restTemplate.exchange(uriComponentsBuilder.build().toString(), method, objectHttpEntity, String.class); + return JSONObject.parseObject(exchange.getBody()); + } +} diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java new file mode 100644 index 0000000..1bf02ff --- /dev/null +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java @@ -0,0 +1,156 @@ +package shkd.sys.sys.mservice; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.logging.Log; +import kd.bos.logging.LogFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import shkd.sys.sys.common.ApiEntity; + +import javax.net.ssl.SSLContext; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; + +/** + * @Description + * @Author Tao + * @Date 2024/11/11 + */ +public class BIPService { + private static final Log logger = LogFactory.getLog(BIPService.class); + + public static String getBIPToken() { + String access_token = null; + long currentTimeMillis = System.currentTimeMillis(); + try { + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders httpHeaders = new HttpHeaders(); + HttpEntity objectHttpEntity = new HttpEntity<>(httpHeaders); + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://biptest.ctny.com.cn/iuap-api-auth/open-auth/selfAppAuth/getAccessToken") + .queryParam("appKey", "22564a240d3140d0b15582aca71a748c") + .queryParam("timestamp", currentTimeMillis) + .queryParam("signature", generateSignature("appKey22564a240d3140d0b15582aca71a748ctimestamp" + currentTimeMillis)); + ResponseEntity exchange = restTemplate.exchange(builder.build().toString(), HttpMethod.GET, objectHttpEntity, String.class); + JSONObject jsonObject = JSON.parseObject(exchange.getBody()); + if ("00000".equals(jsonObject.getString("code"))){ + JSONObject data = jsonObject.getJSONObject("data"); + access_token = data.getString("access_token"); + logger.info("getBIPToken → access_token:{}", access_token); + return access_token; + } + } catch (Exception e) { + e.printStackTrace(); + } + return access_token; + } + + private static RestTemplate createRestTemplateIgnoringSsl() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { + SSLContext sslContext = new SSLContextBuilder() + .loadTrustMaterial(null, (chain, authType) -> true) + .build(); + + SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); + CloseableHttpClient httpClient = HttpClients.custom() + .setSSLSocketFactory(socketFactory) + .build(); + + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); + return new RestTemplate(factory); + } + + private static String generateSignature(String toSign) throws Exception { + // 计算 HmacSHA256 签名 + byte[] hmacData = computeHmacSha256(toSign); + + // Base64 编码 + String base64Encoded = Base64.getEncoder().encodeToString(hmacData); + + // URL 编码 + return URLEncoder.encode(base64Encoded, StandardCharsets.UTF_8.toString()); + } + + private static byte[] computeHmacSha256(String data) throws Exception { + SecretKeySpec secretKey = new SecretKeySpec("79f0b7daa428c1abd9272565dbc65486ab565c80".getBytes(StandardCharsets.UTF_8), "HmacSHA256"); + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(secretKey); + return mac.doFinal(data.getBytes(StandardCharsets.UTF_8)); + } + + /** + * 组装推送BIP付款单JSON + * + * @return + */ + public static JSONObject paymentSlipsJson() { + ApiEntity apiEntity = new ApiEntity(); + + JSONObject root = new JSONObject(); + + // 创建"data"节点 + JSONObject data = new JSONObject(); + + // 设置"data"节点的属性 + data.put("accentity_code", "SIG1040100"); + data.put("vouchdate", "2022-04-20"); + data.put("tradetype_code", "cmp_fund_payment_other"); + data.put("exchangeRateType_code", "01"); + data.put("exchRate", 1); + data.put("currency_code", "CNY"); + data.put("_status", "Insert"); + + // 创建"FundPayment_b"列表 + JSONArray fundPaymentList = new JSONArray(); + + // 创建"FundPayment_b"列表中的第一个对象 + JSONObject fundPayment = new JSONObject(); + fundPayment.put("quickType_code", "6"); + fundPayment.put("settlestatus", "1"); + fundPayment.put("oriSum", 5000); + fundPayment.put("caobject", 2); + fundPayment.put("oppositeobjectname", "11510107MB1526717D"); + fundPayment.put("_status", "Insert"); + + // 将"FundPayment_b"对象添加到列表中 + fundPaymentList.add(fundPayment); + // 将"FundPayment_b"列表添加到"data"节点中 + data.put("FundPayment_b", fundPaymentList); + // 将"data"节点添加到根JSON对象中 + root.put("data", data); + + apiEntity.setURL(""); + apiEntity.setRequestBody(root); + apiEntity.setMethod("POST"); + + Map queryParams = new HashMap<>(); + queryParams.put("access_token", getBIPToken()); + apiEntity.setQueryParams(queryParams); + + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + apiEntity.setHeaders(headers); + + return ApiEntity.getResponseBody(apiEntity); + } +} diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/api/PayBillApiSavePlugin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/api/PayBillApiSavePlugin.java index c73829c..10b2399 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/api/PayBillApiSavePlugin.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/api/PayBillApiSavePlugin.java @@ -65,7 +65,9 @@ public class PayBillApiSavePlugin implements ApiSavePlugin, ApiSerializerPlugin Object payeebankObj = map.get("payeebank"); + logger.info("payeebankObj:{}", payeebankObj); Map payeebankMap = (Map) payeebankObj; + logger.info("payeebankMap:{}", payeebankMap); bankNumber = (String) payeebankMap.get("number");