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