package com.moral.monitor.listener.message; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.HttpsURLConnection; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import static com.moral.monitor.listener.quartz.GetAccessToken.access_token; /** * Created by a on 2017/3/13. */ public class WeChat implements Runnable { private Logger logger = LoggerFactory.getLogger(WeChat.class); private int level; private String zu; private String wx; private Long time; private String mac; public void run() { long ssatime = new Date().getTime(); String result = sendWechatmsgToUser(level, zu, wx, mac, time); long esatime = new Date().getTime(); logger.warn(" 微信报警耗时 " + (esatime - ssatime) + "==========" + Thread.currentThread().getName() + "========="); if (!"success".equals(result)) { logger.warn("微信发送失败" + result); } } public static JSONObject packJsonmsg(Integer level, String zu, String time) { String lev = null; String col = null; if (level == 3) { lev = "三级"; col = "#fd416e"; } else if (level == 2) { lev = "二级"; col = "#fe926f"; } else if (level == 1) { lev = "一级"; col = "#fdf24a"; } JSONObject json = new JSONObject(); // 标题 JSONObject first = new JSONObject(); first.put("value", "七星瓢虫坏境监控智能报警"); first.put("color", "#000000"); json.put("first", first); // 报警来源 JSONObject keyword1 = new JSONObject(); keyword1.put("value", "江苏省昆山市摩瑞尔电器有限公司"); keyword1.put("color", "#000000"); json.put("keyword1", keyword1); // 报警内容 JSONObject keyword2 = new JSONObject(); keyword2.put("value", "环境数(工业级)监测到异常数据"); keyword2.put("color", "#000000"); json.put("keyword2", keyword2); // 报警组别 JSONObject keyword3 = new JSONObject(); keyword3.put("value", zu + "指数超标"); keyword3.put("color", "#000000"); json.put("keyword3", keyword3); // 报警等级 JSONObject keyword4 = new JSONObject(); keyword4.put("value", lev); keyword4.put("color", col); json.put("keyword4", keyword4); // 报警时间 JSONObject keyword5 = new JSONObject(); keyword5.put("value", time); keyword5.put("color", "#000000"); json.put("keyword5", keyword5); // 提示语 JSONObject remark = new JSONObject(); remark.put("value", "情况紧急,请马上处理(点击查看详情)"); remark.put("color", "#000000"); json.put("remark", remark); return json; } //微信: 等级,传感中文名,联系人,报警信息 public static String sendWechatmsgToUser(int level, String zu, String wx, String mac, Long time) { String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token; //处理时间,准备mac 地址,做为微信公从号推送内容的参数 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String times = sdf.format(new Date()); //获取 要发送的 json 对象 .报警组,报警数据,日期 JSONObject data = packJsonmsg(level, zu, times); // 微信详情页 时间,mac, 等级 String clickurl = "http://101.37.22.173:8080/task/detail.do?time=" + time + "&mac=" + mac + "&level=" + level; String topcolor = "#FF0000"; String templat_id = "j8E0d-GgKljSCpuTlCoYdWS6kquaSE1AnvmklE5MoO0"; JSONObject json = new JSONObject(); try { json.put("touser", wx); json.put("template_id", templat_id); json.put("url", clickurl); json.put("topcolor", topcolor); json.put("data", data); } catch (JSONException e) { e.printStackTrace(); } String result = httpsRequest(url, "POST", json.toString()); try { JSONObject resultJson = new JSONObject(result); String errmsg = (String) resultJson.get("errmsg"); if (!"ok".equals(errmsg)) { //如果为errmsg为ok,则代表发送成功,公众号推送信息给用户了。 return errmsg; } } catch (JSONException e) { e.printStackTrace(); } return "success"; } public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) { try { URL url = new URL(requestUrl); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 设置请求方式(GET/POST) conn.setRequestMethod(requestMethod); conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); // 当outputStr不为null时向输出流写数据 if (null != outputStr) { OutputStream outputStream = conn.getOutputStream(); // 注意编码格式 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } // 从输入流读取返回内容 InputStream inputStream = conn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } // 释放资源 bufferedReader.close(); inputStreamReader.close(); inputStream.close(); inputStream = null; conn.disconnect(); return buffer.toString(); } catch (ConnectException ce) { System.out.println("连接超时:{}"); } catch (Exception e) { System.out.println("https请求异常:{}"); } return null; } public WeChat(int level, String zu, String wx, String mac, long time) { this.level = level; this.zu = zu; this.wx = wx; this.mac = mac; this.time = time; } }