From c858ae26143fde37b8c77d403baeb7a050bc2ab6 Mon Sep 17 00:00:00 2001
From: zf <1603559716@qq.com>
Date: 星期一, 25 九月 2023 15:17:42 +0800
Subject: [PATCH] 非煤相关接口

---
 ruoyi-common/src/main/java/com/ruoyi/common/signature/SignatureUtils.java |  104 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 89 insertions(+), 15 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/signature/SignatureUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/signature/SignatureUtils.java
index 9a17e45..7fadc51 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/signature/SignatureUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/signature/SignatureUtils.java
@@ -1,6 +1,9 @@
 package com.ruoyi.common.signature;
 
 import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.ruoyi.common.enums.HttpMethod;
+import com.ruoyi.common.exception.ServiceException;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpStatus;
@@ -8,6 +11,8 @@
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
@@ -39,10 +44,22 @@
 
 	/**
 	 * 获取数据接口
-	 * @param getQueryParam
+	 * @param queryParam
+	 * @param url
+	 * @param method
 	 * @return
 	 */
-	public static Object getObject(String getQueryParam,String url){
+	public static String getObject(String queryParam, String url, HttpMethod method){
+
+		if(method.equals(HttpMethod.GET)){
+			return getMethod(url, queryParam);
+		}
+		if(method.equals(HttpMethod.POST)){
+			return postMethod(url,queryParam);
+		}
+		return null;
+	}
+	private static String getMethod(String url,String queryParam){
 		// 时间戳
 		Long ts = Calendar.getInstance().getTime().getTime();
 		// 随机数
@@ -50,10 +67,10 @@
 		// 接口header 中的公共参数
 		String commonParamUrl = String.format("appKey=%s" + "&" + "ts=%s" + "&" + "once=%s" + "&" + "signMethod=%s", appKey, ts, once, signMethod);
 
-		String getFullUrl = restSever + url + "?" + getQueryParam;
+		String getFullUrl = restSever + url + "?" + queryParam;
 		HttpGet httpGet = new HttpGet(getFullUrl);
 		// get 请求查询参数, 用在 URL 上的, 这里若是通过 ID 查询的, 接口中 ID 是作为路径存在的, 所以需要将 ID 组成
-		String getAllParamUrl = commonParamUrl + "&" + getQueryParam;
+		String getAllParamUrl = commonParamUrl + "&" + queryParam;
 		// 创建 HttpClient 对象
 		CloseableHttpClient httpclient = (CloseableHttpClient) SkipHttpsUtils.wrapClient();
 		// 对参数签名, 并放入请求 header 中的 signData 参数中
@@ -66,12 +83,9 @@
 			httpGet.addHeader("ts", ts.toString());
 			httpGet.addHeader("once", once);
 			httpGet.addHeader("signMethod", signMethod);
-			System.out.println("once:" + once);
 			httpGet.addHeader("signData", signData);
-			System.out.println("headers:" + httpGet.getAllHeaders());
 			String urlStr = httpGet.getURI().toString();
 			// 公共参数 URL
-			System.out.println("commonParamter:" + urlStr);
 			if (StringUtils.endsWith(urlStr, "/")) {
 				urlStr = StringUtils.removeEnd(urlStr, "/");
 			}
@@ -79,7 +93,6 @@
 			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)
 					.setConnectionRequestTimeout(6000).setSocketTimeout(6000).build();
 			httpGet.setConfig(requestConfig);
-			System.out.println("urlStr in request:" + httpGet.getURI().toString());
 			// 执行请求
 			CloseableHttpResponse response = httpclient.execute(httpGet);
 			// 取响应的结果
@@ -87,13 +100,66 @@
 			// 打印响应结果
 			if (statusCode == HttpStatus.SC_OK) {
 				String resp = EntityUtils.toString(response.getEntity(), "utf-8");
-				System.out.println("status:" + statusCode);
-				Object object = JSON.parse(resp);
-				System.out.println("result:" + resp);
-				return object;
+				Object object = JSONObject.parseObject(resp);
 
+				return resp;
 			} else {
-				System.out.println(statusCode);
+				throw new ServiceException("同步数据异常");
+			}
+		} catch (URISyntaxException e) {
+			logger.error("签名失败: ", e);
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+	private static String postMethod(String url, String bodyParam){
+		// 时间戳
+		Long ts = Calendar.getInstance().getTime().getTime();
+		// 随机数
+		String once = RandomStringUtils.randomAlphanumeric(32);
+		// 接口header 中的公共参数
+		String commonParamUrl = String.format("appKey=%s" + "&" + "ts=%s" + "&" + "once=%s" + "&" + "signMethod=%s", appKey, ts, once, signMethod);
+
+		String postFullUrl = restSever + url;
+		HttpPost httpPost = new HttpPost(postFullUrl);
+		String postAllParamUrl = commonParamUrl + "&bodyData=" + bodyParam;
+		// 创建 HttpClient 对象
+		CloseableHttpClient httpclient = (CloseableHttpClient) SkipHttpsUtils.wrapClient();
+		StringEntity bodyData = new StringEntity(bodyParam.toString(), "UTF-8");
+		httpPost.setEntity(bodyData);
+		// 对参数签名, 并放入请求 header 中的 signData 参数中
+		try {
+			// 签名数据
+			String signData = TokenUtils.getSignature(appPwd, postAllParamUrl);
+			// 添加 header 参数 appCode、 timestamp、 signatureNonce、 signature
+			httpPost.addHeader("appKey", appKey);
+			httpPost.addHeader("ts", ts.toString());
+			httpPost.addHeader("once", once);
+			httpPost.addHeader("signMethod", signMethod);
+			httpPost.addHeader("signData", signData);
+			String urlStr = httpPost.getURI().toString();
+			// 公共参数 URL
+			if (StringUtils.endsWith(urlStr, "/")) {
+				urlStr = StringUtils.removeEnd(urlStr, "/");
+			}
+			httpPost.setURI(new URI(urlStr));
+			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)
+					.setConnectionRequestTimeout(6000).setSocketTimeout(6000).build();
+			httpPost.setConfig(requestConfig);
+			// 执行请求
+			CloseableHttpResponse response = httpclient.execute(httpPost);
+			// 取响应的结果
+			int statusCode = response.getStatusLine().getStatusCode();
+			// 打印响应结果
+			if (statusCode == HttpStatus.SC_OK) {
+				String resp = EntityUtils.toString(response.getEntity(), "utf-8");
+				Object object = JSON.parse(resp);
+				return resp;
+			} else {
+				throw new ServiceException("同步数据异常");
 			}
 		} catch (URISyntaxException e) {
 			logger.error("签名失败: ", e);
@@ -110,7 +176,7 @@
 		 * GET 查询接口演示代码
 		 */
 		String startTime = "2018-05-25 00:00:00";
-		String endTime = "2023-06-01 21:00:00";
+		String endTime = "2023-09-01 21:00:00";
 		try {
 			startTime = URLEncoder.encode(startTime, "UTF-8");
 			endTime = URLEncoder.encode(endTime, "UTF-8");
@@ -119,8 +185,16 @@
 		}
 		String getQueryParam = "startTime=" + startTime + "&endTime=" + endTime;
 		System.out.println(getQueryParam);
-		 Object obj = getObject(getQueryParam,"/api/v1/exam/plan/enroll/download");
+		Object obj = getObject(getQueryParam,"/api/v1/exam/plan/enroll/download",HttpMethod.GET);
 		System.out.println(obj);
+		System.out.println("********************************************************************************************************************************");
+		System.out.println("********************************************************************************************************************************");
+		NcCertQuery1 ncCertQuery1 = new NcCertQuery1();
+		ncCertQuery1.setIdcardTypeCode("01");
+		ncCertQuery1.setIdcardNum("362421197712217718");
+		String bodyParam = JSON.toJSONString(ncCertQuery1);
+		Object obj2 = getObject(bodyParam,"/api/v1/cert/query",HttpMethod.POST);
+		System.out.println(obj2);
 
 	}
 

--
Gitblit v1.9.2