parent
51627d7bcc
commit
a527230820
|
@ -1,6 +1,5 @@
|
||||||
package shkd.sys.sys.mservice;
|
package shkd.sys.sys.mservice;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import kd.bos.dataentity.entity.DynamicObject;
|
import kd.bos.dataentity.entity.DynamicObject;
|
||||||
|
@ -10,14 +9,14 @@ import kd.bos.logging.LogFactory;
|
||||||
import kd.bos.orm.query.QCP;
|
import kd.bos.orm.query.QCP;
|
||||||
import kd.bos.orm.query.QFilter;
|
import kd.bos.orm.query.QFilter;
|
||||||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
import javax.crypto.Mac;
|
import javax.crypto.Mac;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
import javax.net.ssl.*;
|
import javax.net.ssl.*;
|
||||||
import java.io.BufferedReader;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
@ -31,55 +30,58 @@ import java.util.*;
|
||||||
public class ApiService {
|
public class ApiService {
|
||||||
private static final Log logger = LogFactory.getLog(ApiService.class);
|
private static final Log logger = LogFactory.getLog(ApiService.class);
|
||||||
|
|
||||||
public static String getBIPToken(String domainName) {
|
private static final OkHttpClient client = new OkHttpClient();
|
||||||
logger.info("getBIPToken → 开始调用接口获取token\n域名:{}", domainName);
|
private static final String APP_KEY = "22564a240d3140d0b15582aca71a748c";
|
||||||
String access_token = null;
|
private static final String API_ENDPOINT = "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken";
|
||||||
|
|
||||||
|
public static Map<String, Object> getBIPToken(String domainName) {
|
||||||
long currentTimeMillis = System.currentTimeMillis();
|
long currentTimeMillis = System.currentTimeMillis();
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
String signature = generateSignature("appKey" + APP_KEY + "timestamp" + currentTimeMillis);
|
||||||
|
logger.info("getBIPToken → 签名: {}", signature);
|
||||||
try {
|
try {
|
||||||
// 禁用 SSL 证书验证
|
// 禁用 SSL 证书验证
|
||||||
disableSSLCertificateChecking();
|
disableSSLCertificateChecking();
|
||||||
// RestTemplate restTemplate = new RestTemplate();
|
|
||||||
String appKey = "22564a240d3140d0b15582aca71a748c";
|
|
||||||
String timestamp = String.valueOf(currentTimeMillis);
|
|
||||||
String signature = generateSignature("appKey" + appKey + "timestamp" + timestamp);
|
|
||||||
|
|
||||||
// 构建URL
|
// 构建URL
|
||||||
String urlString = domainName + "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken" +
|
String urlString = domainName + API_ENDPOINT +
|
||||||
"?appKey=22564a240d3140d0b15582aca71a748c" +
|
"?appKey=" + APP_KEY +
|
||||||
"×tamp=" + currentTimeMillis +
|
"×tamp=" + currentTimeMillis +
|
||||||
"&signature=" + signature;
|
"&signature=" + signature;
|
||||||
|
map.put("url", urlString);
|
||||||
|
// return urlString;
|
||||||
|
|
||||||
logger.info("getBIPToken → 构建URL: {}", urlString);
|
// 发送请求
|
||||||
URL url = new URL(urlString);
|
Request request = buildRequest(urlString);
|
||||||
|
logger.info("getBIPToken → 获取返回数据:{}", request);
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
Response response = client.newCall(request).execute();
|
||||||
connection.setRequestMethod("GET");
|
String token = handleResponse(response);
|
||||||
connection.setRequestProperty("Accept", "application/json");
|
map.put("token", token);
|
||||||
|
// 处理响应
|
||||||
int responseCode = connection.getResponseCode();
|
return map;
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
||||||
// 读取响应
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
||||||
String inputLine;
|
|
||||||
StringBuilder response = new StringBuilder();
|
|
||||||
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
response.append(inputLine);
|
|
||||||
}
|
|
||||||
in.close();
|
|
||||||
|
|
||||||
// 解析响应
|
|
||||||
JSONObject jsonObject = JSON.parseObject(response.toString());
|
|
||||||
JSONObject data = jsonObject.getJSONObject("data");
|
|
||||||
access_token = data.getString("access_token");
|
|
||||||
logger.info("getBIPToken → 接口调用成功,access_token: {}", access_token);
|
|
||||||
} else {
|
|
||||||
logger.error("getBIPToken → 接口调用失败,状态码: {}", responseCode);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("getBIPToken → 调用接口报错: {}", e);
|
logger.error("getBIPToken → 请求失败: ", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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().toString();
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
return access_token;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SSLContext createInsecureSSLContext() throws Exception {
|
public static SSLContext createInsecureSSLContext() throws Exception {
|
||||||
|
@ -122,15 +124,20 @@ public class ApiService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String generateSignature(String toSign) throws Exception {
|
private static String generateSignature(String toSign) {
|
||||||
// 计算 HmacSHA256 签名
|
try {
|
||||||
byte[] hmacData = computeHmacSha256(toSign);
|
// 计算 HmacSHA256 签名
|
||||||
|
byte[] hmacData = computeHmacSha256(toSign);
|
||||||
|
|
||||||
// Base64 编码
|
// Base64 编码
|
||||||
String base64Encoded = Base64.getEncoder().encodeToString(hmacData);
|
String base64Encoded = Base64.getEncoder().encodeToString(hmacData);
|
||||||
|
|
||||||
// URL 编码
|
// URL 编码
|
||||||
return URLEncoder.encode(base64Encoded, StandardCharsets.UTF_8.toString());
|
return URLEncoder.encode(base64Encoded, StandardCharsets.UTF_8.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.info("generateSignature → 签名计算失败: ", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] computeHmacSha256(String data) throws Exception {
|
private static byte[] computeHmacSha256(String data) throws Exception {
|
||||||
|
|
|
@ -172,8 +172,8 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin {
|
||||||
headMap.put("Content-Type", "application/json");
|
headMap.put("Content-Type", "application/json");
|
||||||
apiEntity.setHeaders(headMap);
|
apiEntity.setHeaders(headMap);
|
||||||
Map<String, Object> paramsMap = new HashMap<>();
|
Map<String, Object> paramsMap = new HashMap<>();
|
||||||
|
Map<String, Object> resultMap = ApiService.getBIPToken(domainName);
|
||||||
paramsMap.put("access_token", ApiService.getBIPToken(domainName));
|
paramsMap.put("access_token", resultMap != null ? resultMap.get("token") : "");
|
||||||
apiEntity.setQueryParams(paramsMap);
|
apiEntity.setQueryParams(paramsMap);
|
||||||
apiEntity.setRequestBody(codeEdit.getText());
|
apiEntity.setRequestBody(codeEdit.getText());
|
||||||
JSONObject responseBody = ApiEntity.getResponseBody(apiEntity);
|
JSONObject responseBody = ApiEntity.getResponseBody(apiEntity);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package shkd.sys.sys.plugin.form;
|
package shkd.sys.sys.plugin.form;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import kd.bos.bill.AbstractBillPlugIn;
|
import kd.bos.bill.AbstractBillPlugIn;
|
||||||
import kd.bos.form.control.Toolbar;
|
import kd.bos.form.control.Toolbar;
|
||||||
import kd.bos.form.control.events.ItemClickEvent;
|
import kd.bos.form.control.events.ItemClickEvent;
|
||||||
|
@ -7,6 +8,7 @@ import kd.sdk.plugin.Plugin;
|
||||||
import shkd.sys.sys.mservice.ApiService;
|
import shkd.sys.sys.mservice.ApiService;
|
||||||
|
|
||||||
import java.util.EventObject;
|
import java.util.EventObject;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单据界面插件
|
* 单据界面插件
|
||||||
|
@ -33,8 +35,14 @@ public class ApiTestBillPlugin extends AbstractBillPlugIn implements Plugin {
|
||||||
|
|
||||||
if ("shkd_token".equals(key)) {
|
if ("shkd_token".equals(key)) {
|
||||||
String url = this.getModel().getValue("shkd_url").toString();
|
String url = this.getModel().getValue("shkd_url").toString();
|
||||||
String bipToken = ApiService.getBIPToken(url);
|
Map<String, Object> resultMap = ApiService.getBIPToken(url);
|
||||||
this.getView().showTipNotification(bipToken);
|
this.getView().showTipNotification(resultMap != null ? resultMap.get("token").toString() : "数据为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("shkd_geturl".equals(key)) {
|
||||||
|
String url = this.getModel().getValue("shkd_url").toString();
|
||||||
|
Map<String, Object> resultMap = ApiService.getBIPToken(url);
|
||||||
|
this.getView().showTipNotification(resultMap != null ? resultMap.get("url").toString() : "数据为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("shkd_api".equals(key)) {
|
if ("shkd_api".equals(key)) {
|
||||||
|
|
Loading…
Reference in New Issue