Message2.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. /**
  24. * 公众号,推送信息,推给商家信息
  25. */
  26. public class Message2 {
  27. private static Logger logger = LoggerFactory.getLogger(Message2.class);
  28. //小程序
  29. // static String appId="wx2fc3f45732fae5d3";
  30. // static String secret="7eee4a49a4470a77f9222995e8511547";
  31. //公众号
  32. static String appId="wxa46ef222053a1047";
  33. static String secret="16f74a1265c314fd79fdf90670173467";
  34. /**
  35. * 公众号推送消息
  36. * @param openid 公众号openId
  37. * @param type 事件
  38. * @param location 地点
  39. * @param dateTime 时间
  40. * @return
  41. * @throws Exception
  42. */
  43. public static String send(String openid,String type,String location,String dateTime)
  44. throws Exception {
  45. String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;
  46. String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
  47. JSONObject jsonObject = JSONObject.parseObject(s);
  48. String access_token = jsonObject.getString("access_token");
  49. JSONObject message = new JSONObject();
  50. message.put("touser", openid);
  51. message.put("template_id", "vPuSS7nivkQHxcGzpZB-NedVKzqJ4ifk0FTiBI7Aa20");
  52. JSONObject small = new JSONObject();
  53. small.put("appid", "wx2fc3f45732fae5d3");
  54. small.put("pagepath", "pages/msgWarn/msgWarn");
  55. message.put("miniprogram", small);
  56. // 报警类型
  57. JSONObject const2 = new JSONObject();
  58. const2.put("value", type);
  59. const2.put("color", "#173177");
  60. // 报警位置
  61. JSONObject thing3 = new JSONObject();
  62. thing3.put("value", location);
  63. thing3.put("color", "#173177");
  64. // 报警时间
  65. JSONObject time4 = new JSONObject();
  66. time4.put("value", dateTime);
  67. time4.put("color", "#173177");
  68. // 封装data
  69. JSONObject data = new JSONObject();
  70. data.put("const2",const2);
  71. data.put("thing3",thing3);
  72. data.put("time4",time4);
  73. message.put("data",data);
  74. String s1 = HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, message);
  75. logger.info("s1 = " + s1);
  76. return s1;
  77. }
  78. /**
  79. * 学生出入消息
  80. * @param openid 公众号openId
  81. * @param name 姓名
  82. * @param dateTime 时间
  83. * @param location 地点
  84. * @return
  85. * @throws Exception
  86. */
  87. public static String send2(String openid,String name,String dateTime,String location)
  88. throws Exception {
  89. String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;
  90. String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
  91. JSONObject jsonObject = JSONObject.parseObject(s);
  92. String access_token = jsonObject.getString("access_token");
  93. JSONObject message = new JSONObject();
  94. message.put("touser", openid);
  95. message.put("template_id", "L624rcJY1EpFz5ikL-_2mBarfvsry1CH8C3FLOxwvl4");
  96. JSONObject small = new JSONObject();
  97. small.put("appid", "wx2fc3f45732fae5d3");
  98. small.put("pagepath", "pages/msgWarn/msgWarn");
  99. message.put("miniprogram", small);
  100. // 名字
  101. // 报警位置
  102. JSONObject thing1 = new JSONObject();
  103. thing1.put("value", name);
  104. thing1.put("color", "#173177");
  105. // 时间
  106. JSONObject time2 = new JSONObject();
  107. time2.put("value", dateTime);
  108. time2.put("color", "#173177");
  109. // 地点
  110. JSONObject thing6 = new JSONObject();
  111. thing6.put("value", location);
  112. thing6.put("color", "#173177");
  113. // 封装data
  114. JSONObject data = new JSONObject();
  115. data.put("thing1",thing1);
  116. data.put("time2",time2);
  117. data.put("thing6",thing6);
  118. message.put("data",data);
  119. String s1 = HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, message);
  120. logger.info("s1 = " + s1);
  121. return s1;
  122. }
  123. public static String getToken() throws Exception {
  124. String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;
  125. String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
  126. JSONObject jsonObject = JSONObject.parseObject(s);
  127. logger.info("jsonObject = " + jsonObject);
  128. String access_token = jsonObject.getString("access_token");
  129. // HttpsClient.httpsRequestReturnString("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token, "POST", message.toJSONString());
  130. return access_token;
  131. }
  132. public static void main(String[] args) throws Exception {
  133. String o9MsY67wqvQ__o_pOzF7oeN4MQPM = send2("o9MsY67wqvQ__o_pOzF7oeN4MQPM","张玉玲","2024-01-29 16:16:14","IPCamera 01");
  134. }
  135. }