lc/lc123/cloud/app/api/utils/HttpClient.java

97 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tqq9.lc123.cloud.app.api.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import org.apache.commons.lang3.StringUtils;
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 javax.json.JsonObject;
import java.net.ConnectException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class HttpClient {
private static String URL;
private static String USERNAME;
private static String PASSWORD;
static {
DynamicObject URL = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
new QFilter[]{new QFilter("number", QCP.equals, "FinanceHub_Token_Url")});
DynamicObject userName = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
new QFilter[]{new QFilter("number", QCP.equals, "FinanceHub_Token_Username ")});
USERNAME = userName != null ? userName.getString("name") : null;
DynamicObject passWord = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
new QFilter[]{new QFilter("number", QCP.equals, "FinanceHub_Token_Password")});
PASSWORD = passWord != null ? passWord.getString("name") : null;
}
private static RestTemplate restTemplate = new RestTemplate();
private final static Log logger = LogFactory.getLog(HttpClient.class);
/**
*
* @param url 请求url
* @param body 请求体
* @param token_name 请求tokenname
* @param token_access 请求token
* @param queryMap 请求头query
* @return
* @throws ConnectException
*/
public static ResponseEntity sendRequest(String url, Object body,String token_name ,String token_access,HashMap<String,String> queryMap,String requestType ) throws ConnectException {
HttpHeaders headers = new HttpHeaders();
if (StringUtils.isNotBlank(token_access)) {
headers.set(token_name, token_access);
}
headers.set("Content-Type", "application/json;charset=UTF-8");
headers.set("Accept", "application/json;charset=UTF-8");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
HttpMethod httpMethod = "POST".equalsIgnoreCase(requestType) ? HttpMethod.POST : HttpMethod.GET;
if(!queryMap.isEmpty()){
for (Map.Entry<String, String> map : queryMap.entrySet()) {
builder.queryParam(map.getKey(),map.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity(JSON.toJSONString(body), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().toString(), httpMethod, requestEntity, String.class);
return responseEntity;
}
/**
* 财务中台token获取方法
* @return
* @throws ConnectException
*/
public static String Bearer_Token() throws ConnectException {
// 创建 Basic Auth 认证头
String auth = USERNAME + ":" + PASSWORD;
// 使用 Base64 编码进行认证
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
// 创建请求头
// AuthorizationBasic encodedAuth
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + encodedAuth);
HashMap<String, String> body = new HashMap<>();
body.put("sid","ERP");
ResponseEntity responseEntity = sendRequest(URL, body, null, null,null,Constants.POST);
Object responseEntityBody = responseEntity.getBody();
JSONObject jsonObject1 = JSON.parseObject(responseEntity.getBody().toString());
String accessToken = jsonObject1.get("accessToken").toString();
return accessToken;
}
}