From 769aa8e2b4b26df6a97099cdbbdaa2bc223ad66c Mon Sep 17 00:00:00 2001 From: zy <276996223@qq.com> Date: Tue, 11 Feb 2025 11:16:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/weixin/WeChatController.java | 33 +++++++++ .../system/service/weixin/WeChatService.java | 67 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/weixin/WeChatController.java create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/WeChatService.java diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/weixin/WeChatController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/weixin/WeChatController.java new file mode 100644 index 0000000..f8bc08c --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/weixin/WeChatController.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.module.system.controller.admin.weixin; + +import cn.iocoder.yudao.module.system.service.weixin.WeChatService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.security.PermitAll; + +@RestController +@RequestMapping("/wechat") +@PermitAll +public class WeChatController { + + @Autowired + private WeChatService weChatService; + + @GetMapping("/send") + @PermitAll + public String sendMessage() { + try { + String accessToken = weChatService.getAccessToken(); + String openId = "ogEz46_ADXNd_SmC9f57tYZkswRg"; // 替换为用户的 OpenID + String templateId = "E8RK91cPLMios6ZHoKx6FJOV4H2kodx6yPWYp7jpLJY"; // 替换为订阅消息模板 ID + weChatService.sendSubscribeMessage(accessToken, openId, templateId); + return "Message sent successfully"; + } catch (Exception e) { + e.printStackTrace(); + return "Failed to send message"; + } + } +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/WeChatService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/WeChatService.java new file mode 100644 index 0000000..3658028 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/WeChatService.java @@ -0,0 +1,67 @@ +package cn.iocoder.yudao.module.system.service.weixin; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Service +public class WeChatService { + @Autowired + private ObjectMapper objectMapper; + @Value("${wx.miniapp.appid}") + private String APP_ID ; // 替换为你的小程序 AppID + @Value("${wx.miniapp.secret}") + private String APP_SECRET; // 替换为你的小程序 AppSecret + + /** + * 获取微信 Access Token + */ + public String getAccessToken() throws Exception { + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(url); + String response = EntityUtils.toString(httpClient.execute(httpGet).getEntity()); + httpClient.close(); + + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(response); + return node.get("access_token").asText(); + } + + + /** + * 发送订阅消息 + */ + public void sendSubscribeMessage(String accessToken, String openId, String templateId) throws Exception { + String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken; + + // 构建消息内容 + ObjectNode data = objectMapper.createObjectNode(); + data.put("touser", openId); // 用户的 OpenID + data.put("template_id", templateId); // 订阅消息模板 ID + data.put("page", "pages/index/index"); // 点击消息跳转的小程序页面 + data.put("data", objectMapper.createObjectNode() + .put("thing1", objectMapper.createObjectNode().put("value", "Hello, World!")) + .at("thing2")); + + // 发送 HTTP 请求 + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + httpPost.setHeader("Content-Type", "application/json"); + httpPost.setEntity(new StringEntity(data.toString())); + String response = EntityUtils.toString(httpClient.execute(httpPost).getEntity()); + httpClient.close(); + + System.out.println("订阅消息发送结果: " + response); + } + + +}