From 70884ab305f4a7deb29938a9e4a76c3a80dfa368 Mon Sep 17 00:00:00 2001 From: Kane Wang Date: Sat, 23 Apr 2022 23:53:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E6=9C=AC=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/java/DisasterWarning/config.json | 4 - .../src/main/java/AppMain.java | 6 +- .../java/com/cpic/xim/config/CpicxmStuff.java | 18 +- .../java/com/cpic/xim/config/db/DBTable.java | 20 ++- .../com/cpic/xim/config/db/OracleConfig.java | 81 ++++++++- .../java/com/cpic/xim/httpUtil/HttpUtils.java | 2 - .../wechat/officalAccount/sendMessage.java | 154 ++++-------------- .../officalAccount/sendMessageTest.java | 38 ++++- 数据/和风天气/实例-东山.json | 5 +- 9 files changed, 174 insertions(+), 154 deletions(-) diff --git a/code/java/DisasterWarning/config.json b/code/java/DisasterWarning/config.json index c9e1c5f..014224c 100644 --- a/code/java/DisasterWarning/config.json +++ b/code/java/DisasterWarning/config.json @@ -7,10 +7,6 @@ { "city_name": "厦门", "city_code": "101230201" - }, - { - "city_name": "东山", - "city_code": "101230608" } ], "notify_stuffs": [ diff --git a/code/java/DisasterWarning/src/main/java/AppMain.java b/code/java/DisasterWarning/src/main/java/AppMain.java index fb533b5..1b52676 100644 --- a/code/java/DisasterWarning/src/main/java/AppMain.java +++ b/code/java/DisasterWarning/src/main/java/AppMain.java @@ -2,7 +2,7 @@ * @Author: Kane * @Date: 2022-04-22 10:53:49 * @LastEditors: Kane - * @LastEditTime: 2022-04-22 16:18:48 + * @LastEditTime: 2022-04-23 23:22:57 * @FilePath: \DisasterWarning\src\main\java\AppMain.java * @Description: 和风天气预警推送厦门太保公众号主程序! * @@ -12,6 +12,7 @@ import com.cpic.xim.config.City; import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber; import com.cpic.xim.config.WeatherDisasterNotifyConfig; +import com.cpic.xim.wechat.officalAccount.sendMessage; import java.io.IOException; import java.util.Vector; @@ -57,6 +58,9 @@ public class AppMain { continue; } + + sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(), + warning ); } catch (IOException error) { diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java index 643dbfb..b58ce8b 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java @@ -1,10 +1,22 @@ +/* + * @Author: Kane + * @Date: 2022-04-22 17:33:30 + * @LastEditors: Kane + * @LastEditTime: 2022-04-23 23:50:45 + * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\CpicxmStuff.java + * @Description: 产险厦门分公司人员信息 + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ + package com.cpic.xim.config; import java.util.Objects; public class CpicxmStuff { - public CpicxmStuff() {}; + public CpicxmStuff() + {}; public String getStuffName() { @@ -29,11 +41,11 @@ public class CpicxmStuff @Override public boolean equals( Object o ) { - if ( this == o ) + if ( this == o) { return true; } - if ( !( o instanceof CpicxmStuff ) ) + if ( !(o instanceof CpicxmStuff)) { return false; } diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java index 9843d27..2c5a8e1 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java @@ -1,10 +1,21 @@ +/* + * @Author: Kane + * @Date: 2022-04-22 17:33:30 + * @LastEditors: Kane + * @LastEditTime: 2022-04-23 23:50:20 + * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\DBTable.java + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ package com.cpic.xim.config.db; import java.util.Objects; public class DBTable { - public DBTable() {} + public DBTable() + {} public String getTableName() { @@ -29,16 +40,17 @@ public class DBTable @Override public boolean equals( Object o ) { - if ( this == o ) + if ( this == o) { return true; } - if ( !( o instanceof DBTable ) ) + if ( !(o instanceof DBTable)) { return false; } DBTable dbTable = (DBTable) o; - return tableName.equals( dbTable.tableName ) && tableDescription.equals( dbTable.tableDescription ); + return tableName.equals( dbTable.tableName ) + && tableDescription.equals( dbTable.tableDescription ); } @Override diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java index 868ae8c..4caaccd 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java @@ -1,13 +1,84 @@ +/* + * @Author: Kane + * @Date: 2022-04-22 17:33:30 + * @LastEditors: Kane + * @LastEditTime: 2022-04-23 23:51:46 + * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\OracleConfig.java + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ package com.cpic.xim.config.db; import java.util.Vector; public class OracleConfig { - private String tnsName; - private String ipAddr; - private String jdbcURL; - private String userName; - private String password; + + public String getTnsName() + { + return tnsName; + } + + public void setTnsName( String tnsName ) + { + this.tnsName = tnsName; + } + + public String getIpAddr() + { + return ipAddr; + } + + public void setIpAddr( String ipAddr ) + { + this.ipAddr = ipAddr; + } + + public String getJdbcURL() + { + return jdbcURL; + } + + public void setJdbcURL( String jdbcURL ) + { + this.jdbcURL = jdbcURL; + } + + public String getUserName() + { + return userName; + } + + public void setUserName( String userName ) + { + this.userName = userName; + } + + public String getPassword() + { + return password; + } + + public void setPassword( String password ) + { + this.password = password; + } + + public Vector getTables() + { + return tables; + } + + public void setTables( Vector tables ) + { + this.tables = tables; + } + + private String tnsName; + private String ipAddr; + private String jdbcURL; + private String userName; + private String password; private Vector tables; } diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java index 9336ce4..bf161cc 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java @@ -51,8 +51,6 @@ public class HttpUtils // 设置请求头参数 for ( HashMap.Entry head : headers.entrySet()) { - String key = head.getKey(); - conn.setRequestProperty( head.getKey(), head.getValue() ); } diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java index d1436bc..221f4ca 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java @@ -2,7 +2,7 @@ * @Author: Kane * @Date: 2022-04-22 10:53:49 * @LastEditors: Kane - * @LastEditTime: 2022-04-23 00:13:29 + * @LastEditTime: 2022-04-23 23:46:54 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java * @Description: 用来推送公众号消息的程序库。 * @@ -10,24 +10,19 @@ */ package com.cpic.xim.wechat.officalAccount; -import com.fasterxml.jackson.annotation.JsonInclude; + import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.*; - -import java.net.URL; -import java.net.HttpURLConnection; -import java.nio.charset.StandardCharsets; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.Vector; import com.cpic.xim.notify.disaster.*; +import com.cpic.xim.httpUtil.HttpUtils; /** * 推送微信公众号消息。 - * @Description + * * @author Kane */ public class sendMessage @@ -35,125 +30,16 @@ public class sendMessage private static HashMap warningLevel; /** - * 向公众号推送通知消息。接口文档参考 彭奕洁 编写《消息发送接口调用文档》 - * - * @param wechatOfficalAccountURL 公众号接口网址。 - * @param title 通知的标题。 - * @param notifyType 通知类型。 - * @param notifyMessage 通知文本内容。 - */ - public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title, - String notifyType, String notifyMessage ) - { - // 设置推送内容 - WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter(); - - param.setFirst( title ); - param.setKeyword1( notifyType ); - param.setKeyword1Color( "#ff0000" ); - param.setKeyword2( notifyMessage ); - - // 转换成json - ObjectMapper mapper; - String json = null; - - try - { - mapper = new ObjectMapper(); - mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL ); - - json = mapper.writeValueAsString( param ); - - if ( json.isEmpty()) - { - - } - - System.out.println( json ); - } - catch (JsonProcessingException error) - { - error.printStackTrace(); - - return; - } - - // 推送数据 - URL url = null; - HttpURLConnection connection = null; - - StringBuilder result = new StringBuilder(); - OutputStreamWriter out = null; - BufferedReader in = null; - - try - { - url = new URL( wechatOfficalAccountURL ); - connection = (HttpURLConnection) url.openConnection(); - - connection.setRequestMethod( "POST" ); - connection.setRequestProperty( "accept", "*/*" ); - connection.setRequestProperty( "Connection", "Keep-Alive" ); - connection.setRequestProperty( "Content-Type", "application/json" ); - connection.setDoInput( true ); - connection.setDoOutput( true ); - connection.connect(); - - out = new OutputStreamWriter( connection.getOutputStream(), StandardCharsets.UTF_8 ); - out.write( json ); - out.flush(); - - in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) ); - - String line = in.readLine(); - - while (line != null) - { - result.append( line ); - - line = in.readLine(); - } - } - catch (Exception error) - { - error.printStackTrace(); - } - finally - { - try - { - if ( in != null) - { - in.close(); - } - - if ( out != null) - { - out.close(); - } - - if ( connection != null) - { - connection.disconnect(); - } - } - catch (Exception error) - { - error.printStackTrace(); - } - } - - } - - /** - * 推送天气灾害预警!接口文档参考 彭奕洁 编写《消息发送接口调用文档》 + * 推送天气灾害预警!接口文档参考 彭奕洁 编写《消息发送接口调用文档》 + * * @param officalAccountURL 产险厦门分公司公众号接口网址 - * @param warning 灾害警报对象 + * @param warning 灾害预警对象 */ public static void sendWeatherDisasterWarning( String officalAccountURL, QWeatherDisasterWarning warning ) { Vector warningItems = warning.getWarning(); + StringBuilder requestBody = null; // 设置请求头参数 HashMap headers = new HashMap(); @@ -168,13 +54,31 @@ public class sendMessage // 拼接消息内容 String title = item.getTitle(); String text = item.getText(); - String color = warningLevel.keySet().contains( item.getLevel() ) ? "#000000" - : warningLevel.get( item.getLevel() ); + String color = warningLevel.keySet().contains( item.getLevel() ) + ? warningLevel.get( item.getLevel() ) + : "#000000"; + + requestBody = new StringBuilder(); + + requestBody.append( "tplid=57&groupid=1&first=尊敬的#realname#,您好!&" ); + requestBody.append( "keyword1=" ).append( title ).append( "&" ); + requestBody.append( "keyword1color=" ).append( color ).append( "&" ); + requestBody.append( "keyword2=" ).append( text ); + + try + { + HttpUtils.postHttpRequest( officalAccountURL, headers, requestBody.toString() ); + } + catch (MalformedURLException error) + { + error.printStackTrace(); + } } } static { + // 预警级别色彩 warningLevel = new HashMap<>(); warningLevel.put( "蓝色", "#0000FF" ); diff --git a/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java b/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java index 51cc096..2cda05e 100644 --- a/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java +++ b/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java @@ -2,43 +2,47 @@ * @Author: Kane * @Date: 2022-04-22 10:53:49 * @LastEditors: Kane - * @LastEditTime: 2022-04-22 16:02:05 + * @LastEditTime: 2022-04-23 23:44:54 * @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java * @Description: * * Copyright (c) ${2022} by Kane, All Rights Reserved. */ - package com.cpic.xim.wechat.officalAccount; +import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; + import org.junit.Test; import static org.junit.Assert.*; import com.cpic.xim.httpUtil.*; +import java.io.IOException; import java.util.*; +import com.fasterxml.jackson.databind.ObjectMapper; + public class sendMessageTest { private static String url = "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage"; - @Test + // @Test public void postNotifyMessageJson() { try { // sendMessage.postNotifyMessageJSON( url, "警报", "警报标题", "警报内容!" ); } - catch ( Exception error ) + catch (Exception error) { fail( "测试失败!" ); } } - @Test + // @Test public void postNotifyMessage() { HashMap headers = new HashMap(); - String param = "tplid=57&" + "groupid=1&" + "first=亲爱的#realname#,您好!&" + String param = "tplid=57&groupid=1&first=亲爱的#realname#,您好!&" + "keyword1=东山县气象台发布大雾黄色预警[Ⅲ级/较重]&" + "keyword1color=#ffff00&" + "keyword2=东山县气象台2022年04月22日06时12分发布大雾黄色预警信号:预计未来12小时内,我县、闽南渔场和台湾浅滩渔场将出现能见度小于500米的雾。请注意防范!"; @@ -50,9 +54,29 @@ public class sendMessageTest { HttpUtils.postHttpRequest( url, headers, param ); } - catch ( Exception error ) + catch (Exception error) { } } + + @Test + public void testSendWeatherDisasterWarning() throws IOException + { + String warningJSON = + "{\"code\":\"200\",\"updateTime\":\"2022-04-23T23:25+08:00\",\"fxLink\":\"http://hfx.link/3645\",\"warning\":[{\"id\":\"10123060820220423203500874007118\",\"sender\":\"东山县气象台\",\"pubTime\":\"2022-04-23T20:35+08:00\",\"title\":\"东山县气象台发布大雾黄色预警[Ⅲ级/较重]\",\"status\":\"active\",\"level\":\"黄色\",\"type\":\"1017\",\"typeName\":\"大雾\",\"text\":\"东山县气象台2022年04月23日20时35分发布大雾黄色预警信号:预计未来12小时内,我县、闽南渔场和台湾浅滩渔场将出现能见度小于500米的雾。请注意防范!\",\"related\":\"\",\"urgency\":\"\",\"certainty\":\"\"}],\"refer\":{\"sources\":[\"12379\"],\"license\":[\"no commercial use\"]}}"; + + try + { + ObjectMapper mapper = new ObjectMapper(); + QWeatherDisasterWarning warning = + mapper.readValue( warningJSON, QWeatherDisasterWarning.class ); + + sendMessage.sendWeatherDisasterWarning( url, warning ); + } + catch (Exception error) + { + fail( error.getMessage() ); + } + } } diff --git a/数据/和风天气/实例-东山.json b/数据/和风天气/实例-东山.json index b31c9e2..5cf31f7 100644 --- a/数据/和风天气/实例-东山.json +++ b/数据/和风天气/实例-东山.json @@ -18,7 +18,6 @@ "certainty": "" } ], - "refer": { "sources": ["12379"], "license": ["no commercial use"] } + "refer": { "sources": ["12379"], "license": ["no commercial use"] }, + "testURL": "https://devapi.qweather.com/v7/warning/now?key=fe9fa8eeeb6f4301a92541eed565dd15&location=101230608" } - -https://devapi.qweather.com/v7/warning/now?key=fe9fa8eeeb6f4301a92541eed565dd15&location=101230608