package com.template.common.utils; import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpResponseException; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.HashMap; /** * 公众号,推送信息,推给商家信息 */ public class Message2 { private static Logger logger = LoggerFactory.getLogger(Message2.class); //小程序 // static String appId="wx2fc3f45732fae5d3"; // static String secret="7eee4a49a4470a77f9222995e8511547"; //公众号 static String appId="wxa46ef222053a1047"; static String secret="16f74a1265c314fd79fdf90670173467"; /** * 公众号推送消息 * @param openid 公众号openId * @param type 事件 * @param location 地点 * @param dateTime 时间 * @return * @throws Exception */ public static String send(String openid,String type,String location,String dateTime) throws Exception { String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret; String s = HttpsClient.httpsRequestReturnString(url, "GET", ""); JSONObject jsonObject = JSONObject.parseObject(s); String access_token = jsonObject.getString("access_token"); JSONObject message = new JSONObject(); message.put("touser", openid); message.put("template_id", "vPuSS7nivkQHxcGzpZB-NedVKzqJ4ifk0FTiBI7Aa20"); JSONObject small = new JSONObject(); small.put("appid", "wx2fc3f45732fae5d3"); small.put("pagepath", "pages/msgWarn/msgWarn"); message.put("miniprogram", small); // 报警类型 JSONObject const2 = new JSONObject(); const2.put("value", type); const2.put("color", "#173177"); // 报警位置 JSONObject thing3 = new JSONObject(); thing3.put("value", location); thing3.put("color", "#173177"); // 报警时间 JSONObject time4 = new JSONObject(); time4.put("value", dateTime); time4.put("color", "#173177"); // 封装data JSONObject data = new JSONObject(); data.put("const2",const2); data.put("thing3",thing3); data.put("time4",time4); message.put("data",data); String s1 = HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, message); logger.info("s1 = " + s1); return s1; } /** * 学生出入消息 * @param openid 公众号openId * @param name 姓名 * @param dateTime 时间 * @param location 地点 * @return * @throws Exception */ public static String send2(String openid,String name,String dateTime,String location) throws Exception { String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret; String s = HttpsClient.httpsRequestReturnString(url, "GET", ""); JSONObject jsonObject = JSONObject.parseObject(s); String access_token = jsonObject.getString("access_token"); JSONObject message = new JSONObject(); message.put("touser", openid); message.put("template_id", "L624rcJY1EpFz5ikL-_2mBarfvsry1CH8C3FLOxwvl4"); JSONObject small = new JSONObject(); small.put("appid", "wx2fc3f45732fae5d3"); small.put("pagepath", "pages/msgWarn/msgWarn"); message.put("miniprogram", small); // 名字 // 报警位置 JSONObject thing1 = new JSONObject(); thing1.put("value", name); thing1.put("color", "#173177"); // 时间 JSONObject time2 = new JSONObject(); time2.put("value", dateTime); time2.put("color", "#173177"); // 地点 JSONObject thing6 = new JSONObject(); thing6.put("value", location); thing6.put("color", "#173177"); // 封装data JSONObject data = new JSONObject(); data.put("thing1",thing1); data.put("time2",time2); data.put("thing6",thing6); message.put("data",data); String s1 = HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, message); logger.info("s1 = " + s1); return s1; } public static String getToken() throws Exception { String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret; String s = HttpsClient.httpsRequestReturnString(url, "GET", ""); JSONObject jsonObject = JSONObject.parseObject(s); logger.info("jsonObject = " + jsonObject); String access_token = jsonObject.getString("access_token"); // HttpsClient.httpsRequestReturnString("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token, "POST", message.toJSONString()); return access_token; } public static void main(String[] args) throws Exception { String o9MsY67wqvQ__o_pOzF7oeN4MQPM = send2("o9MsY67wqvQ__o_pOzF7oeN4MQPM","张玉玲","2024-01-29 16:16:14","IPCamera 01"); } }