wxSendMessage.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.wxxcx.index;
  2. import com.alibaba.fastjson.JSON;
  3. import com.google.gson.Gson;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.client.methods.CloseableHttpResponse;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.util.EntityUtils;
  13. import java.io.IOException;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. @Slf4j
  19. public class wxSendMessage {
  20. private static final String APPID = "wx9b479fa3c2af4f21";
  21. private static final String SECRET = "426effb7b58f9ba7c5f6dd1062db0074";
  22. private static final Gson gson = new Gson();
  23. public static void queryAccessToken(String devId, String devName, String phoneNo, String fwhOpenId) {
  24. // 1. 获取 access_token
  25. String accessToken = getAccessToken();
  26. if (accessToken == null) {
  27. System.out.println("获取 access_token 失败");
  28. log.info("获取 access_token 失败");
  29. return;
  30. }
  31. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  32. String currentTime = sdf.format(new Date());
  33. Map<String, Object> data = new HashMap<>();
  34. // 1. 每个字段需要包装成包含 value 的 Map
  35. Map<String, String> characterString1 = new HashMap<>();
  36. characterString1.put("value", devId);
  37. data.put("character_string1", characterString1);
  38. Map<String, String> thing8 = new HashMap<>();
  39. // 设备名称
  40. // thing8.put("value", "测试设备");
  41. thing8.put("value", devName);
  42. data.put("thing8", thing8);
  43. Map<String, String> thing10 = new HashMap<>();
  44. // 用户手机号
  45. // thing10.put("value", "17356519496");
  46. thing10.put("value", phoneNo);
  47. data.put("thing10", thing10);
  48. Map<String, String> time3 = new HashMap<>();
  49. time3.put("value", currentTime); // 时间字段需符合格式要求
  50. data.put("time3", time3);
  51. Map<String, String> const2 = new HashMap<>();
  52. const2.put("value", "设备运行异常,给用户发送推送");
  53. data.put("const2", const2);
  54. System.out.println(data);
  55. log.info("mqttutil--sendmessage:{}", JSON.toJSONString(data));
  56. // String openid = "oWlo-6iXL0pQeYWZxEpwB8knv6D8";
  57. boolean result = sendTemplateMessage(accessToken, fwhOpenId, data);
  58. System.out.println("发送结果: " + (result ? "成功" : "失败"));
  59. log.info("发送结果: " + (result ? "成功" : "失败"));
  60. }
  61. private static String getAccessToken() {
  62. String url = "https://api.weixin.qq.com/cgi-bin/token" +
  63. "?grant_type=client_credential" +
  64. "&appid=" + APPID +
  65. "&secret=" + SECRET;
  66. try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
  67. HttpGet httpGet = new HttpGet(url);
  68. try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
  69. HttpEntity entity = response.getEntity();
  70. String result = EntityUtils.toString(entity);
  71. Map<String, Object> map = gson.fromJson(result, Map.class);
  72. System.out.println(map.get("access_token"));
  73. return (String) map.get("access_token");
  74. }
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. return null;
  78. }
  79. }
  80. private static boolean sendTemplateMessage(String accessToken, String openid,
  81. Map<String, Object> data) {
  82. String url = "https://api.weixin.qq.com/cgi-bin/message/template/send" +
  83. "?access_token=" + accessToken;
  84. // 构建请求体
  85. Map<String, Object> params = new HashMap<>();
  86. params.put("touser", openid);
  87. params.put("template_id", "-wNA7XW0_4hscmIUK-hmolNpccd-zMlyGnKUvpGdfZQ");
  88. params.put("data", data);
  89. try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
  90. HttpPost httpPost = new HttpPost(url);
  91. httpPost.setHeader("Content-Type", "application/json");
  92. httpPost.setEntity(new StringEntity(gson.toJson(params), "UTF-8"));
  93. try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
  94. String result = EntityUtils.toString(response.getEntity());
  95. Map<String, Object> resultMap = gson.fromJson(result, Map.class);
  96. System.out.println("打印结果: " + resultMap);
  97. return resultMap.get("errmsg").equals("ok");
  98. }
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. return false;
  102. }
  103. }
  104. public static void main(String[] args) {
  105. // 你的程序逻辑
  106. System.out.println("Hello, world!");
  107. // 其他业务代码...
  108. }
  109. }