提交内容:定时任务

备注:定时推送单据至BIP
提交人:邹江涛
This commit is contained in:
zoujiangtao 2024-12-18 10:51:28 +08:00
parent 8372e0d2b7
commit f5933260b2
3 changed files with 79 additions and 108 deletions

View File

@ -10,16 +10,12 @@ import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
@ -30,7 +26,6 @@ import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
/**
* @Description
@ -47,97 +42,61 @@ public class ApiService {
public static String getBIPToken(String domainName) {
String token = null;
try {
// 自定义 TrustManager
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// 安装所有信任的管理器
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// 自定义 HostnameVerifier
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
logger.info("Verifying hostname: " + hostname);
return true; // 始终返回 true绕过验证
}
};
// 设置自定义 HostnameVerifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
long currentTimeMillis = System.currentTimeMillis();
String signature = generateSignature("appKey" + APP_KEY + "timestamp" + currentTimeMillis);
String timestamp = Long.toString(System.currentTimeMillis());
String signature = generateSignature("appKey" + APP_KEY + "timestamp" + timestamp);
logger.info("getBIPToken → 签名: {}", signature);
// URL with parameters
String urlString = String.format(
domainName + API_ENDPOINT + "?appKey=%s&timestamp=%s&signature=%s",
APP_KEY, timestamp, signature
);
URL url = new URL(urlString);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
// 禁用 SSL 证书验证
disableSSLCertificateChecking();
// 构建URL
String urlString = domainName + API_ENDPOINT +
"?appKey=" + APP_KEY +
"&timestamp=" + currentTimeMillis +
"&signature=" + signature;
disableSSLCertificateValidation();
// 禁用 SSL 证书验证
// disableSSLCertificateChecking();
// return urlString;
logger.info("getBIPToken → 构建URL: {}", urlString);
try {
// 发送请求
Request request = buildRequest(urlString);
Response response = client.newCall(request).execute();
logger.info("getBIPToken → 获取返回数据:{}", response);
token = handleResponse(response);
} catch (Exception e) {
logger.error("getBIPToken → 请求失败: ", e);
token = null;
// 设置自定义主机名
connection.setHostnameVerifier((hostname, session) -> {
logger.info("获取验证主机名: " + hostname);
return true; // Always return true to bypass verification
});
// 设置请求方法
connection.setRequestMethod("GET");
// 发送请求并获取响应
int responseCode = connection.getResponseCode();
logger.info("响应code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// 处理响应
logger.info("getBIPToken method completed successfully");
in.close();
logger.info("响应结果: " + response);
} catch (Exception e) {
logger.info("Error in getBIPToken", e);
logger.info("getBIPToken错误: " + e);
}
return token;
}
private static Request buildRequest(String urlString) {
return new Request.Builder()
.url(urlString)
.get()
.build();
}
private static String handleResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String responseBody = response.body().string();
logger.info("handleResponse → 响应内容: {}", responseBody);
JSONObject jsonObject = JSONObject.parseObject(responseBody);
JSONObject data = jsonObject.getJSONObject("data");
return data.getString("access_token");
} else {
logger.error("handleResponse → 请求失败,响应码: {}", response.code());
return null;
}
}
public static SSLContext createInsecureSSLContext() throws Exception {
// 创建一个信任所有证书的 TrustManager
private static void disableSSLCertificateValidation() throws Exception {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
@ -148,28 +107,19 @@ public class ApiService {
}
};
// 安装信任所有证书的 TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext;
}
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
public static void disableSSLCertificateChecking() {
try {
// 创建一个信任所有证书的 SSLContext
SSLContext sslContext = createInsecureSSLContext();
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = (hostname, session) -> {
logger.info("HostnameVerifier called for: " + hostname);
return true;
};
// 设置 HttpsURLConnection 使用这个 SSLContext
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
// 创建一个 HostnameVerifier信任所有主机名
HostnameVerifier allHostsValid = (hostname, session) -> true;
// 安装这个 HostnameVerifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (Exception e) {
e.printStackTrace();
}
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
private static String generateSignature(String toSign) {
@ -261,6 +211,30 @@ public class ApiService {
break;
// 收款处理
case "https://biptestctny.invest.com.cn:8002/iuap-api-auth/yonbip/fi/fundcollection/save":
saveResponseBody = pushBill(objects[0], saveUrl, saveRequestBody);
logger.info("收款处理 → BIP保存接口 → 返回结果:{}", saveResponseBody);
jsonObject = JSON.parseObject(saveResponseBody);
code = jsonObject.getString("code");
if ("200".equals(code)) {
logger.info("收款处理 → BIP保存接口 → 成功\n失败单据编号{}\n推送失败接口返回数据{}", dynamic.getString("billno"), saveResponseBody);
data = jsonObject.getJSONObject("data");
dynamic.set("shkd_businessnumber", data.getString("code"));
dynamic.set("shkd_businessid", data.getString("id"));
dynamic.set("shkd_businessname", "BIP");
dynamic.set("shkd_pushstatus", "已结算");
dynamicObjects.add(dynamic);
submitUrl = objects[0].getString("shkd_submiturl");
submitRequestBody = getSubmitRequestBody("cmp.fundcollection.FundCollection", "cmp_fundcollection", data.getString("id"));
// 提交
submitResponseBody = pushBill(objects[0], submitUrl, JSON.toJSONString(submitRequestBody));
logger.info("收款处理 → BIP提交接口 → 返回结果:{}", submitResponseBody);
result = "推送 → 收款处理 → 成功";
} else {
logger.info("收款处理 → BIP保存接口 → 成功\n失败单据编号{}\n推送失败接口返回数据{}", dynamic.getString("billno"), saveResponseBody);
result = "推送 → 收款处理 → 失败";
}
break;
// 银行收付处理
case "https://biptestctny.invest.com.cn:8002/iuap-api-auth/yonbip/ctm/cmp/bankReconciliationBatchSave":

View File

@ -1,6 +1,5 @@
package shkd.sys.sys.plugin.form;
import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.form.control.Toolbar;
import kd.bos.form.control.events.ItemClickEvent;
@ -8,7 +7,6 @@ import kd.sdk.plugin.Plugin;
import shkd.sys.sys.mservice.ApiService;
import java.util.EventObject;
import java.util.Map;
/**
* 单据界面插件

View File

@ -63,13 +63,10 @@ public class PushTaskPlugin extends AbstractTask implements Plugin {
"shkd_pushstatus,shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus,bankpaystatus"
, new QFilter("bizdate", QCP.large_equals, date)
.and("shkd_pushstatus", QCP.not_equals, "已结算")
.and("shkd_pushstatus", QCP.not_equals, "未结算")
.and("shkd_pushstatus", QCP.not_equals, "结算失败")
.and("billstatus", QCP.equals, "D").toArray());
dynamicObjects = Arrays.asList(objects1);
dynamicObjects.forEach(dynamicObject -> {
ApiService.paymentSlipsJson(dynamicObject, "BIP", dynamicObjectList);
});
dynamicObjects.forEach(dynamicObject -> ApiService.paymentSlipsJson(dynamicObject, "BIP", dynamicObjectList));
SaveServiceHelper.save(dynamicObjectList.toArray(new DynamicObject[0]));
break;
// 收款处理
@ -79,8 +76,10 @@ public class PushTaskPlugin extends AbstractTask implements Plugin {
"payertype,org,bizdate,accountbank,payernumber,actrecamt,txt_description,shkd_pushstatus," +
"shkd_businessnumber,shkd_businessid,shkd_businessname,billstatus"
, new QFilter("bizdate", QCP.large_equals, date)
.and("shkd_pushstatus", QCP.not_equals, "已推送").toArray());
.and("shkd_pushstatus", QCP.not_equals, "已结算")
.and("billstatus", QCP.equals, "D").toArray());
dynamicObjects = Arrays.asList(objects2);
dynamicObjects.forEach(dynamicObject -> ApiService.paymentSlipsJson(dynamicObject, "BIP", dynamicObjectList));
billName = "收款处理";
break;
// 银行收付处理