Message2.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.template.common.utils;
  2. import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.gson.Gson;
  5. import com.google.gson.reflect.TypeToken;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.StatusLine;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.HttpResponseException;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClientBuilder;
  17. import org.apache.http.impl.client.HttpClients;
  18. import org.apache.http.util.EntityUtils;
  19. import java.io.IOException;
  20. import java.util.HashMap;
  21. /**
  22. * 公众号,推送信息,推给商家信息
  23. */
  24. public class Message2 {
  25. //小程序
  26. // static String appId="wx2fc3f45732fae5d3";
  27. // static String secret="7eee4a49a4470a77f9222995e8511547";
  28. //公众号
  29. static String appId="wxa46ef222053a1047";
  30. static String secret="16f74a1265c314fd79fdf90670173467";
  31. /**
  32. * 公众号推送消息
  33. * @param openid 公众号openId
  34. * @param type 事件
  35. * @param location 地点
  36. * @param dateTime 时间
  37. * @return
  38. * @throws Exception
  39. */
  40. public static String send(String openid,String type,String location,String dateTime)
  41. throws Exception {
  42. String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;
  43. String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
  44. JSONObject jsonObject = JSONObject.parseObject(s);
  45. String access_token = jsonObject.getString("access_token");
  46. JSONObject message = new JSONObject();
  47. message.put("touser", openid);
  48. message.put("template_id", "vPuSS7nivkQHxcGzpZB-NedVKzqJ4ifk0FTiBI7Aa20");
  49. JSONObject small = new JSONObject();
  50. small.put("appid", "wx2fc3f45732fae5d3");
  51. small.put("pagepath", "pages/msgWarn/msgWarn");
  52. message.put("miniprogram", small);
  53. // 报警类型
  54. JSONObject const2 = new JSONObject();
  55. const2.put("value", type);
  56. const2.put("color", "#173177");
  57. // 报警位置
  58. JSONObject thing3 = new JSONObject();
  59. thing3.put("value", location);
  60. thing3.put("color", "#173177");
  61. // 报警时间
  62. JSONObject time4 = new JSONObject();
  63. time4.put("value", dateTime);
  64. time4.put("color", "#173177");
  65. // 封装data
  66. JSONObject data = new JSONObject();
  67. data.put("const2",const2);
  68. data.put("thing3",thing3);
  69. data.put("time4",time4);
  70. message.put("data",data);
  71. String s1 = HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, message);
  72. System.out.println("s1 = " + s1);
  73. return s1;
  74. }
  75. public static String getToken() throws Exception {
  76. String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;
  77. String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
  78. JSONObject jsonObject = JSONObject.parseObject(s);
  79. System.out.println("jsonObject = " + jsonObject);
  80. String access_token = jsonObject.getString("access_token");
  81. // HttpsClient.httpsRequestReturnString("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token, "POST", message.toJSONString());
  82. return access_token;
  83. }
  84. public static void main(String[] args) throws Exception {
  85. String o9MsY67wqvQ__o_pOzF7oeN4MQPM = send("o9MsY67wqvQ__o_pOzF7oeN4MQPM","进入区域","IPCamera 01","2024-01-29 16:16:14");
  86. // String token = getToken();
  87. // System.out.println("token = " + token);
  88. }
  89. }