first commit

This commit is contained in:
Mauve
2026-09-08 16:56:12 +08:00
commit 6ce4e4a166
898 changed files with 234303 additions and 0 deletions
@@ -0,0 +1,209 @@
package com.mgm.mcrm.common.services;
import com.alibaba.fastjson.JSON;
import com.mgm.mcrm.common.dtos.HttpResult;
import com.mgm.mcrm.common.enums.HttpMethods;
import com.mgm.mcrm.common.httpclient.HttpClientExecutor;
import com.mgm.mcrm.common.httpclient.HttpRequestOptions;
import jakarta.annotation.Resource;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* 請求服務
* @author Aaka
*/
@Service
public class HttpService {
private static final Logger log = LoggerFactory.getLogger(HttpService.class);
@Resource
@Qualifier("httpClientWithSSL")
private CloseableHttpClient httpClient;
@Resource
@Qualifier("httpRequestConfig")
private RequestConfig requestConfig;
@Value("${daylight.url}")
private String daylightUrl;
public String sslGet(String url, Header[] headers, Map<String, Object> map) throws Exception {
// todo 在這裏需要把請求頭的信息插入到數據庫裏
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
// 使用 HttpRequestOptions 配置 GET 请求(带参数)
HttpRequestOptions options = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.map(map)
.encoding("utf-8")
.client(httpClient);
String result = HttpClientExecutor.get(options, headers);
log.info("====== GET request result:{}", result);
return result;
}
/**
* 使用 HttpConfig 配置 POST 请求(form-urlencoded 格式)
* @param url 請求路徑
* @param headers 請求頭
* @param map 請求參數
* @return 接口結果的json數據
* @throws Exception 異常
*/
public String sslPostForm(String url, Header[] headers, Map<String, Object> map) throws Exception {
log.info("======sslPostForm======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
// 使用 HttpRequestOptions 配置 POST 请求(form-urlencoded 格式)
HttpRequestOptions options = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.method(HttpMethods.POST)
.map(map) // 使用 map,会自动转换为 form-urlencoded
.encoding("utf-8")
.client(httpClient);
String result = HttpClientExecutor.post(options, headers);
log.info("====== POST Form request result:{}", result);
return result;
}
/**
* 使用 HttpConfig 配置 POST 请求(JSON 格式)
* @param url 請求路徑
* @param headers 請求頭
* @param map 請求參數
* @return 接口結果的json數據
* @throws Exception 異常
*/
public String sslPostJson(String url, Header[] headers, Map<String, Object> map) throws Exception {
log.info("======sslPostJson======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
// 使用 HttpRequestOptions 配置 POST 请求(JSON 格式)
HttpRequestOptions options = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.method(HttpMethods.POST)
.json(JSON.toJSONString(map)) // 使用 map,会自动转换为 json
.encoding("utf-8")
.client(httpClient);
String result = HttpClientExecutor.post(options, headers);
log.info("====== POST Json request result:{}", result);
return result;
}
public String sslPostJsonContent(String url, Header[] headers, String jsonContent) throws Exception {
// 使用 HttpRequestOptions 配置 POST 请求(JSON 格式)
HttpRequestOptions options = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.method(HttpMethods.POST)
.json(jsonContent)
.encoding("utf-8")
.client(httpClient);
return HttpClientExecutor.post(options, headers);
}
/**
* 使用 HttpConfig 配置 PUT 请求
* @param url 請求路徑
* @param headers 請求頭
* @param map 請求參數
* @return 接口結果的json數據
* @throws Exception 異常
*/
public String sslPut(String url, Header[] headers, Map<String, Object> map) throws Exception {
log.info("======sslPut======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
HttpRequestOptions options = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.method(HttpMethods.PUT)
.json(JSON.toJSONString(map))
.encoding("utf-8")
.client(httpClient);
String result = HttpClientExecutor.execute(options, headers);
log.info("====== PUT request result:{}", result);
return result;
}
/**
* 使用 HttpConfig 配置 Delete 请求
* @param url 請求路徑
* @param headers 請求頭
* @param map 請求參數
* @return 接口結果的json數據
* @throws Exception 異常
*/
public String sslPutDelete(String url, Header[] headers, Map<String, Object> map) throws Exception {
log.info("======sslPutDelete======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
HttpRequestOptions options = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.method(HttpMethods.DELETE)
.client(httpClient);
String result = HttpClientExecutor.execute(options, headers);
System.out.println("DELETE 请求结果:\n" + result);
log.info("====== DELETE request result:{}", result);
return result;
}
/**
* 使用 HttpConfig 配置 Delete 请求
* @param url 請求路徑
* @param headers 請求頭
* @param map 要传的数据
* @return 接口結果的json數據
* @throws Exception 異常
*/
public HttpResult sslPostHttpResult(String url, Header[] headers, Map<String,Object> map) throws Exception {
log.info("======sslPutDelete======url:{}===========headers:{}", daylightUrl + url,JSON.toJSONString(headers));
HttpRequestOptions postConfig = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.json(JSON.toJSONString(map))
.method(HttpMethods.POST)
.client(httpClient);
HttpResult result = HttpClientExecutor.sendAndPostResp(postConfig, headers);
System.out.println("POST 请求结果:\n" + result);
log.info("====== POST request result:{}", result);
return result;
}
public HttpResult sslGetHttpResult(String url, Header[] headers, Map<String, Object> map) throws Exception {
// todo 在這裏需要把請求頭的信息插入到數據庫裏
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
// 使用 HttpConfig 配置 GET 请求(带参数)
HttpRequestOptions getConfig = HttpRequestOptions.create()
.url(daylightUrl + url)
.timeout(requestConfig)
.map(map)
.encoding("utf-8")
.client(httpClient);
HttpResult result = HttpClientExecutor.sendAndGetResp(getConfig, headers);
log.info("====== GET request result:{}", result.getResult());
Map<String, String> mapResult = JSON.parseObject(result.getResult(), Map.class);
log.info("====== GET request result:{}", mapResult.get("results"));
return result;
}
}