wxSendMessage.java 4.7 KB

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