From 2a57cb0939e9e35f1e6eb7680660acdd8ca2c7a2 Mon Sep 17 00:00:00 2001 From: lzx_mac <2602107437@qq.com> Date: Sun, 2 Feb 2025 00:33:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/api/service/YaoDuNoticeService.java | 8 ++ .../api/service/impl/YaoDuNoticeServiceImpl.java | 126 +++++++++++++++++++++ .../api/yaoduapi/YaoDuNoticeController.java | 19 ++++ .../src/main/resources/application-dev.yml | 2 +- 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/YaoDuNoticeService.java create mode 100644 jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuNoticeServiceImpl.java create mode 100644 jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuNoticeController.java diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/YaoDuNoticeService.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/YaoDuNoticeService.java new file mode 100644 index 0000000..7bfebe1 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/YaoDuNoticeService.java @@ -0,0 +1,8 @@ +package org.jeecg.modules.api.service; + +public interface YaoDuNoticeService { + + + + +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuNoticeServiceImpl.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuNoticeServiceImpl.java new file mode 100644 index 0000000..8a939f6 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuNoticeServiceImpl.java @@ -0,0 +1,126 @@ +package org.jeecg.modules.api.service.impl; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.jeecg.modules.api.service.YaoDuNoticeService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +@Service +public class YaoDuNoticeServiceImpl implements YaoDuNoticeService { + + private static final String APP_ID = "wxa4d29e67e8a58d38"; + private static final String APP_SECRET = "866e4ba72bd86a4c79403b6b1341461b"; + private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET; + private static final String TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="; + + + private static String getAccessToken() throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(ACCESS_TOKEN_URL); + CloseableHttpResponse response = httpClient.execute(httpPost); + + String responseString = EntityUtils.toString(response.getEntity(), "UTF-8"); + Map responseMap = new ObjectMapper().readValue(responseString, Map.class); + return responseMap.get("access_token"); + } +// +// private static void sendTemplateMessage(String accessToken, String templateId, String openId, Map data) throws IOException { +// CloseableHttpClient httpClient = HttpClients.createDefault(); +// HttpPost httpPost = new HttpPost(TEMPLATE_MESSAGE_URL + accessToken); +// +// Map payload = new HashMap<>(); +// payload.put("touser", openId); +// payload.put("template_id", templateId); +// payload.put("data", data); +// +// StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(payload), "UTF-8"); +// httpPost.setEntity(entity); +// httpPost.setHeader("Content-type", "application/json"); +// +// CloseableHttpResponse response = httpClient.execute(httpPost); +// String responseString = EntityUtils.toString(response.getEntity(), "UTF-8"); +// System.out.println("Response: " + responseString); +// } + + +// private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; // 替换为你的access_token + private static final String TEMPLATE_ID = "uXZnHWrjtcX9JHlnMpdlWmzgJp71sKxCRiMn3TrE-EE"; // 替换为你的template_id + private static final String OPENID = "oFzrW4tqZUvN9T0RQzWPdCT1St68"; // 替换为用户的openid + + public static void main(String[] args) { + try { + sendSubscribeMessage(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void sendSubscribeMessage() throws IOException { + String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken(); + + // 构建请求数据 + Map data = new HashMap<>(); + Map thing01Data = new HashMap<>(); + thing01Data.put("value", "今天星期天"); + data.put("thing1", thing01Data); + + Map thing02Data = new HashMap<>(); + thing02Data.put("value", "一个可爱的评论者"); + data.put("thing2", thing02Data); + + Map thing03Data = new HashMap<>(); + thing03Data.put("value", "收到一条评论"); + data.put("thing3", thing03Data); + + Map date04Data = new HashMap<>(); + date04Data.put("value", "2018-01-01"); + data.put("time4", date04Data); + + Map requestPayload = new HashMap<>(); + requestPayload.put("touser", OPENID); // 确保OPENID是有效的 + requestPayload.put("template_id", TEMPLATE_ID); // 确保TEMPLATE_ID是有效的,且与模板消息配置相匹配 + requestPayload.put("page", "/pages/index/index"); // 用户点击消息后跳转的小程序页面路径 + requestPayload.put("data", data); // 这里应该是包含所有模板数据的映射 + requestPayload.put("miniprogram_state", "trial"); // 如果是测试阶段,使用trial;正式阶段使用formal + requestPayload.put("lang", "zh_CN"); + + // 将请求数据转换为JSON字符串 + ObjectMapper objectMapper = new ObjectMapper(); + String jsonRequest = objectMapper.writeValueAsString(requestPayload); + + // 创建HttpClient实例 + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpPost httpPost = new HttpPost(url); + httpPost.setEntity(new StringEntity(jsonRequest, StandardCharsets.UTF_8)); + httpPost.setHeader("Content-Type", "application/json"); + + // 发送请求并获取响应 + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + String jsonResponse = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + System.out.println("Response: " + jsonResponse); + + // 解析响应数据(这里只是简单打印,实际应用中应根据需要处理) + Map responseMap = objectMapper.readValue(jsonResponse, Map.class); + Integer errcode = (Integer) responseMap.get("errcode"); + String errmsg = (String) responseMap.get("errmsg"); + if (errcode == 0) { + System.out.println("Message sent successfully!"); + } else { + System.out.println("Error sending message: " + errmsg); + } + } + } + } + +} diff --git a/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuNoticeController.java b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuNoticeController.java new file mode 100644 index 0000000..b8714f4 --- /dev/null +++ b/jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuNoticeController.java @@ -0,0 +1,19 @@ +package org.jeecg.modules.api.yaoduapi; + + +import io.swagger.annotations.Api; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags="消息通知类接口") +@RestController +@RequestMapping("/notice") +@Slf4j +public class YaoDuNoticeController { + + + + + +} diff --git a/jeecg-boot-module-system/src/main/resources/application-dev.yml b/jeecg-boot-module-system/src/main/resources/application-dev.yml index 25370e5..b52598c 100644 --- a/jeecg-boot-module-system/src/main/resources/application-dev.yml +++ b/jeecg-boot-module-system/src/main/resources/application-dev.yml @@ -1,5 +1,5 @@ server: - port: 8000 + port: 8001 tomcat: max-swallow-size: -1 error: