From 21fe5196beabf6bf6367caa43b6f154a6a5e1030 Mon Sep 17 00:00:00 2001 From: chy Date: Fri, 13 Mar 2026 09:58:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=83=BD=E8=80=97=E9=87=87?= =?UTF-8?q?=E9=9B=86=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yaml | 4 +- .../framework/config/job/SaSetdtlSyncJob.java | 2 - .../framework/config/job/anyToGrBiNh.java | 136 ++++++++++++ .../lideeyunji/core/framework/utils/Func.java | 3 + lidee-tool/tool-common/pom.xml | 12 + .../framework/common/util/http/HttpUtils.java | 205 ++++++++++++++++++ 6 files changed, 358 insertions(+), 4 deletions(-) create mode 100644 lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/anyToGrBiNh.java diff --git a/lidee-admin/src/main/resources/application.yaml b/lidee-admin/src/main/resources/application.yaml index c8edbf6..50ca7c0 100644 --- a/lidee-admin/src/main/resources/application.yaml +++ b/lidee-admin/src/main/resources/application.yaml @@ -2,8 +2,8 @@ spring: application: name: lideeyunji-server profiles: -# active: local # 读取配置 - active: prod # 读取配置 + active: local # 读取配置 +# active: prod # 读取配置 main: allow-circular-references: true # 允许循环依赖,因为项目是三层架构,无法避免这个情况。 diff --git a/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/SaSetdtlSyncJob.java b/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/SaSetdtlSyncJob.java index 68434dd..f92d9da 100644 --- a/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/SaSetdtlSyncJob.java +++ b/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/SaSetdtlSyncJob.java @@ -17,8 +17,6 @@ import org.springframework.stereotype.Component; @Component("SaSetdtlSyncJob") public class SaSetdtlSyncJob implements JobHandler { - @Autowired - private ILideeYunJiService lideeYunJiService; @Autowired private IGrBiSaSetdtlService grBiSaSetdtlService; diff --git a/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/anyToGrBiNh.java b/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/anyToGrBiNh.java new file mode 100644 index 0000000..5a42a7a --- /dev/null +++ b/lidee-core/src/main/java/com/lideeyunji/core/framework/config/job/anyToGrBiNh.java @@ -0,0 +1,136 @@ + +package com.lideeyunji.core.framework.config.job; + +import cn.hutool.json.JSONObject; +import com.lideeyunji.core.framework.service.ILideeYunJiService; +import com.lideeyunji.tool.framework.common.util.http.HttpUtils; +import com.lideeyunji.tool.framework.quartz.core.handler.JobHandler; +import com.lideeyunji.tool.framework.tenant.core.aop.TenantIgnore; +import com.lideeyunji.tool.framework.yunji.component.redis.lideeYunjiRedisUtils; +import lombok.extern.slf4j.Slf4j; +import org.ehcache.xml.model.CacheTemplate; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.sql.*; +import java.util.HashMap; +import java.util.Map; + +/** + * 保存模块使用记录 能耗模块使用记录 + */ +@Slf4j +@Component("anyToGrBiNh") +public class anyToGrBiNh implements JobHandler { + + @Autowired + private lideeYunjiRedisUtils redisUtil; + + @Autowired + private ILideeYunJiService lideeYunJiService; + + @Override + @TenantIgnore + public String execute(String param) { + log.info("*********** 同步能耗数据 ************"); + + getToken(); + String resultStr = "*********** 同步能耗数据保存成功 ************"; + log.info(resultStr); + return resultStr; + } + + private void getToken() { + try { + String nhtoken = redisUtil.get("nh_token", String.class); + + System.out.println(nhtoken); + if(nhtoken==null){ + + // 2. POST 表单请求 + Map params = new HashMap<>(); + params.put("fLoginname", "9287ee99ae49b47dc7ce1746eac04f8b"); + params.put("fPassword", "d23d0731bcf61d824aa30372da5fae0b"); + params.put("loginType", "password"); + params.put("projectType", "4"); + String postResult = HttpUtils.doPost("http://111.39.171.64:8090/SubstationWEBV2/user/login", params, null); + + JSONObject jsonObject = new JSONObject(postResult); + jsonObject= new JSONObject(jsonObject.get("data").toString()); + redisUtil.set("nh_token",jsonObject.get("token"),(Long)jsonObject.get("tokenExpireTime")); + System.out.println(jsonObject.get("token")); + getNhsj(jsonObject.get("token").toString()); + } + else { + nhtoken = redisUtil.get("nh_token").toString(); + getNhsj(nhtoken); + } + + } catch (IOException e) { + e.printStackTrace(); + } + +} + + private void getNhsj(String token){ + try { + Map headers = new HashMap<>(); + headers.put("Authorization", token); + String getResult = HttpUtils.doGet("http://111.39.171.64:8090/SubstationWEBV2/main/Compare/overview?fCoaccountno=1", headers); + System.out.println("GET Response:\n" + getResult); + + + + + + + // 3. POST JSON 请求 +// String jsonBody = "{\"name\":\"Alice\",\"age\":25}"; +// Map headers = new HashMap<>(); +// headers.put("Authorization", "Bearer your_token_here"); +// String jsonResult = HttpUtils.doPostJson("https://api.example.com/user", jsonBody, headers); +// System.out.println("POST JSON Response:\n" + jsonResult); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + + + public void main(String[] args) { + getToken(); + // 2. POST 表单请求 +// Map params = new HashMap<>(); +// params.put("fLoginname", "2613504b593d16343ccf368879f9519c"); +// params.put("fPassword", "c7d967eaeaa0963fe33c740b46316590"); +// params.put("loginType", "password"); +// params.put("projectType", "4"); +// String postResult = HttpUtils.doPost("http://111.39.171.64:8090/SubstationWEBV2/user/login", params, null); +// System.out.println("POST Form Response:\n" + postResult); +// JSONObject jsonObject = new JSONObject(postResult); +// jsonObject= new JSONObject(jsonObject.get("data").toString()); +//System.out.println(jsonObject.get("token")); + + // 1. GET 请求 +// String getResult = HttpUtils.doGet("https://api.example.com/data", null); +// System.out.println("GET Response:\n" + getResult); + + + // 3. POST JSON 请求 +// String jsonBody = "{\"name\":\"Alice\",\"age\":25}"; +// Map headers = new HashMap<>(); +// headers.put("Authorization", "Bearer your_token_here"); +// String jsonResult = HttpUtils.doPostJson("https://api.example.com/user", jsonBody, headers); +// System.out.println("POST JSON Response:\n" + jsonResult); + + + } + + + +} diff --git a/lidee-core/src/main/java/com/lideeyunji/core/framework/utils/Func.java b/lidee-core/src/main/java/com/lideeyunji/core/framework/utils/Func.java index 54e63d3..d9b2efe 100644 --- a/lidee-core/src/main/java/com/lideeyunji/core/framework/utils/Func.java +++ b/lidee-core/src/main/java/com/lideeyunji/core/framework/utils/Func.java @@ -278,6 +278,9 @@ public class Func extends FuncBase { return fieldValObj; } + /* + * http post请求 + * */ public static String sendPost(String url, Object body) throws Exception { String accept = HttpRequest.post(url) .body(Func.json2Str(body)) diff --git a/lidee-tool/tool-common/pom.xml b/lidee-tool/tool-common/pom.xml index 7c75f1d..23f979a 100644 --- a/lidee-tool/tool-common/pom.xml +++ b/lidee-tool/tool-common/pom.xml @@ -7,6 +7,18 @@ lidee-tool ${lidee.version} + + + + org.apache.maven.plugins + maven-compiler-plugin + + 10 + 10 + + + + 4.0.0 tool-common jar diff --git a/lidee-tool/tool-common/src/main/java/com/lideeyunji/tool/framework/common/util/http/HttpUtils.java b/lidee-tool/tool-common/src/main/java/com/lideeyunji/tool/framework/common/util/http/HttpUtils.java index 7f1facc..2d946cb 100644 --- a/lidee-tool/tool-common/src/main/java/com/lideeyunji/tool/framework/common/util/http/HttpUtils.java +++ b/lidee-tool/tool-common/src/main/java/com/lideeyunji/tool/framework/common/util/http/HttpUtils.java @@ -10,8 +10,12 @@ import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import javax.servlet.http.HttpServletRequest; +import java.io.*; +import java.net.HttpURLConnection; import java.net.URI; +import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Map; /** @@ -21,6 +25,9 @@ import java.util.Map; */ public class HttpUtils { + private static final int CONNECT_TIMEOUT = 5000; // 连接超时(毫秒) + private static final int READ_TIMEOUT = 10000; // 读取超时(毫秒) + @SuppressWarnings("unchecked") public static String replaceUrlQuery(String url, String key, String value) { UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset()); @@ -122,5 +129,203 @@ public class HttpUtils { return null; } + /** + * 发送 HTTP GET 请求 + * + * @param urlStr 请求地址 + * @param headers 请求头(可为 null) + * @return 响应字符串 + */ + public static String doGet(String urlStr, Map headers) throws IOException { + HttpURLConnection conn = null; + BufferedReader reader = null; + try { + URL url = new URL(urlStr); + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(CONNECT_TIMEOUT); + conn.setReadTimeout(READ_TIMEOUT); + conn.setDoInput(true); + + // 设置请求头 + if (headers != null && !headers.isEmpty()) { + for (Map.Entry entry : headers.entrySet()) { + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } + + int responseCode = conn.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + reader = new BufferedReader( + new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + } + return result.toString(); + } else { + throw new IOException("GET request failed, response code: " + responseCode); + } + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException ignored) { + } + } + if (conn != null) { + conn.disconnect(); + } + } + } + + /** + * 发送 HTTP POST 请求(application/x-www-form-urlencoded) + * + * @param urlStr 请求地址 + * @param params 表单参数(可为 null) + * @param headers 请求头(可为 null) + * @return 响应字符串 + */ + public static String doPost(String urlStr, Map params, Map headers) + throws IOException { + HttpURLConnection conn = null; + BufferedWriter writer = null; + BufferedReader reader = null; + try { + URL url = new URL(urlStr); + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setConnectTimeout(CONNECT_TIMEOUT); + conn.setReadTimeout(READ_TIMEOUT); + conn.setDoOutput(true); + conn.setDoInput(true); + + // 设置请求头 + if (headers != null && !headers.isEmpty()) { + for (Map.Entry entry : headers.entrySet()) { + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } + + // 写入表单参数 + if (params != null && !params.isEmpty()) { + StringBuilder paramStr = new StringBuilder(); + for (Map.Entry entry : params.entrySet()) { + if (paramStr.length() > 0) { + paramStr.append("&"); + } + paramStr.append(entry.getKey()) + .append("=") + .append(java.net.URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)); + } + writer = new BufferedWriter( + new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8)); + writer.write(paramStr.toString()); + writer.flush(); + } + + int responseCode = conn.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + reader = new BufferedReader( + new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + } + return result.toString(); + } else { + throw new IOException("POST request failed, response code: " + responseCode); + } + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ignored) { + } + } + if (reader != null) { + try { + reader.close(); + } catch (IOException ignored) { + } + } + if (conn != null) { + conn.disconnect(); + } + } + } + + /** + * 发送 JSON 格式的 POST 请求 + * + * @param urlStr 请求地址 + * @param json 请求体 JSON 字符串 + * @param headers 请求头(可为 null) + * @return 响应字符串 + */ + public static String doPostJson(String urlStr, String json, Map headers) + throws IOException { + HttpURLConnection conn = null; + BufferedWriter writer = null; + BufferedReader reader = null; + try { + URL url = new URL(urlStr); + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setConnectTimeout(CONNECT_TIMEOUT); + conn.setReadTimeout(READ_TIMEOUT); + conn.setDoOutput(true); + conn.setDoInput(true); + + // 设置默认 Content-Type + conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + + // 设置自定义请求头 + if (headers != null && !headers.isEmpty()) { + for (Map.Entry entry : headers.entrySet()) { + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } + + // 写入 JSON 请求体 + writer = new BufferedWriter( + new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8)); + writer.write(json); + writer.flush(); + + int responseCode = conn.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + reader = new BufferedReader( + new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + } + return result.toString(); + } else { + throw new IOException("POST JSON request failed, response code: " + responseCode); + } + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ignored) { + } + } + if (reader != null) { + try { + reader.close(); + } catch (IOException ignored) { + } + } + if (conn != null) { + conn.disconnect(); + } + } + } }