package com.wxxcx.index; import com.alibaba.fastjson.JSON; import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; 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.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; @Slf4j public class wxSendMessage { private static final String APPID = "wx9b479fa3c2af4f21"; private static final String SECRET = "426effb7b58f9ba7c5f6dd1062db0074"; private static final Gson gson = new Gson(); public static void queryAccessToken(String devId, String devName, String phoneNo, String fwhOpenId) { // 1. 获取 access_token String accessToken = getAccessToken(); if (accessToken == null) { System.out.println("获取 access_token 失败"); log.info("获取 access_token 失败"); return; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(new Date()); Map data = new HashMap<>(); // 1. 每个字段需要包装成包含 value 的 Map Map characterString1 = new HashMap<>(); characterString1.put("value", devId); data.put("character_string1", characterString1); Map thing8 = new HashMap<>(); // 设备名称 // thing8.put("value", "测试设备"); thing8.put("value", devName); data.put("thing8", thing8); Map thing10 = new HashMap<>(); // 用户手机号 // thing10.put("value", "17356519496"); thing10.put("value", phoneNo); data.put("thing10", thing10); Map time3 = new HashMap<>(); time3.put("value", currentTime); // 时间字段需符合格式要求 data.put("time3", time3); Map const2 = new HashMap<>(); const2.put("value", "设备运行异常,给用户发送推送"); data.put("const2", const2); System.out.println(data); log.info("mqttutil--sendmessage:{}", JSON.toJSONString(data)); // String openid = "oWlo-6iXL0pQeYWZxEpwB8knv6D8"; boolean result = sendTemplateMessage(accessToken, fwhOpenId, data); System.out.println("发送结果: " + (result ? "成功" : "失败")); log.info("发送结果: " + (result ? "成功" : "失败")); } private static String getAccessToken() { String url = "https://api.weixin.qq.com/cgi-bin/token" + "?grant_type=client_credential" + "&appid=" + APPID + "&secret=" + SECRET; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(url); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity); Map map = gson.fromJson(result, Map.class); System.out.println(map.get("access_token")); return (String) map.get("access_token"); } } catch (IOException e) { e.printStackTrace(); return null; } } private static boolean sendTemplateMessage(String accessToken, String openid, Map data) { String url = "https://api.weixin.qq.com/cgi-bin/message/template/send" + "?access_token=" + accessToken; // 构建请求体 Map params = new HashMap<>(); params.put("touser", openid); params.put("template_id", "-wNA7XW0_4hscmIUK-hmolNpccd-zMlyGnKUvpGdfZQ"); params.put("data", data); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(gson.toJson(params), "UTF-8")); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { String result = EntityUtils.toString(response.getEntity()); Map resultMap = gson.fromJson(result, Map.class); System.out.println("打印结果: " + resultMap); return resultMap.get("errmsg").equals("ok"); } } catch (Exception e) { e.printStackTrace(); return false; } } public static void main(String[] args) { // 你的程序逻辑 System.out.println("Hello, world!"); // 其他业务代码... } }