完成基本功能!

This commit is contained in:
Kane Wang 2022-04-23 23:53:29 +08:00
parent 69434b3b58
commit 70884ab305
9 changed files with 174 additions and 154 deletions

View File

@ -7,10 +7,6 @@
{
"city_name": "厦门",
"city_code": "101230201"
},
{
"city_name": "东山",
"city_code": "101230608"
}
],
"notify_stuffs": [

View File

@ -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)
{

View File

@ -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()
{

View File

@ -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()
{
@ -38,7 +49,8 @@ public class 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

View File

@ -1,9 +1,80 @@
/*
* @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
{
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<DBTable> getTables()
{
return tables;
}
public void setTables( Vector<DBTable> tables )
{
this.tables = tables;
}
private String tnsName;
private String ipAddr;
private String jdbcURL;

View File

@ -51,8 +51,6 @@ public class HttpUtils
// 设置请求头参数
for ( HashMap.Entry<String, String> head : headers.entrySet())
{
String key = head.getKey();
conn.setRequestProperty( head.getKey(), head.getValue() );
}

View File

@ -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,150 +10,36 @@
*/
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
{
private static HashMap<String, String> 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<QWeatherDisasterWarningItem> warningItems = warning.getWarning();
StringBuilder requestBody = null;
// 设置请求头参数
HashMap<String, String> headers = new HashMap<String, String>();
@ -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" );

View File

@ -2,26 +2,30 @@
* @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
@ -34,11 +38,11 @@ public class sendMessageTest
}
}
@Test
// @Test
public void postNotifyMessage()
{
HashMap<String, String> headers = new HashMap<String, String>();
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米的雾。请注意防范";
@ -55,4 +59,24 @@ public class sendMessageTest
}
}
@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() );
}
}
}

View File

@ -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