This commit is contained in:
Kane Wang 2022-04-22 23:48:34 +08:00
parent 9c1f141a00
commit 1e8e58d3de
1 changed files with 34 additions and 35 deletions

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 10:53:49 * @Date: 2022-04-22 10:53:49
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-04-22 23:04:58 * @LastEditTime: 2022-04-22 23:48:28
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java
* @Description: 用来推送公众号消息的程序库 * @Description: 用来推送公众号消息的程序库
* *
@ -27,7 +27,7 @@ import com.cpic.xim.notify.disaster.*;
/** /**
* 推送微信公众号消息 * 推送微信公众号消息
* * @Description
* @author Kane * @author Kane
*/ */
public class sendMessage public class sendMessage
@ -43,7 +43,7 @@ public class sendMessage
* @param notifyMessage 通知文本内容 * @param notifyMessage 通知文本内容
*/ */
public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title, public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title,
String notifyType, String notifyMessage ) String notifyType, String notifyMessage )
{ {
// 设置推送内容 // 设置推送内容
WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter(); WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();
@ -55,7 +55,7 @@ public class sendMessage
// 转换成json // 转换成json
ObjectMapper mapper; ObjectMapper mapper;
String json = null; String json = null;
try try
{ {
@ -64,14 +64,14 @@ public class sendMessage
json = mapper.writeValueAsString( param ); json = mapper.writeValueAsString( param );
if ( json.isEmpty() ) if ( json.isEmpty())
{ {
} }
System.out.println( json ); System.out.println( json );
} }
catch ( JsonProcessingException error ) catch (JsonProcessingException error)
{ {
error.printStackTrace(); error.printStackTrace();
@ -79,16 +79,16 @@ public class sendMessage
} }
// 推送数据 // 推送数据
URL url; URL url = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
OutputStreamWriter out = null; OutputStreamWriter out = null;
BufferedReader in = null; BufferedReader in = null;
try try
{ {
url = new URL( wechatOfficalAccountURL ); url = new URL( wechatOfficalAccountURL );
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod( "POST" ); connection.setRequestMethod( "POST" );
@ -107,14 +107,14 @@ public class sendMessage
String line = in.readLine(); String line = in.readLine();
while ( line != null ) while (line != null)
{ {
result.append( line ); result.append( line );
line = in.readLine(); line = in.readLine();
} }
} }
catch ( Exception error ) catch (Exception error)
{ {
error.printStackTrace(); error.printStackTrace();
} }
@ -122,22 +122,22 @@ public class sendMessage
{ {
try try
{ {
if ( in != null ) if ( in != null)
{ {
in.close(); in.close();
} }
if ( out != null ) if ( out != null)
{ {
out.close(); out.close();
} }
if ( connection != null ) if ( connection != null)
{ {
connection.disconnect(); connection.disconnect();
} }
} }
catch ( Exception error ) catch (Exception error)
{ {
error.printStackTrace(); error.printStackTrace();
} }
@ -146,13 +146,12 @@ public class sendMessage
} }
/** /**
* 推送天气灾害预警接口文档参考 彭奕洁 编写消息发送接口调用文档 * 推送天气灾害预警接口文档参考 彭奕洁 编写消息发送接口调用文档
*
* @param officalAccountURL 产险厦门分公司公众号接口网址 * @param officalAccountURL 产险厦门分公司公众号接口网址
* @param warning 灾害警报对象 * @param warning 灾害警报对象
*/ */
public static void sendWeatherDisasterWarning( String officalAccountURL, public static void sendWeatherDisasterWarning( String officalAccountURL,
QWeatherDisasterWarning warning ) QWeatherDisasterWarning warning )
{ {
Vector<QWeatherDisasterWarningItem> warningItems = warning.getWarning(); Vector<QWeatherDisasterWarningItem> warningItems = warning.getWarning();
@ -164,11 +163,11 @@ public class sendMessage
headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" ); headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );
// 遍历消息将消息推送出去 // 遍历消息将消息推送出去
for ( QWeatherDisasterWarningItem item : warningItems ) for ( QWeatherDisasterWarningItem item : warningItems)
{ {
// 拼接消息内容 // 拼接消息内容
String title = item.getTitle(); String title = item.getTitle();
String text = item.getText(); String text = item.getText();
String color = warningLevel.keySet().contains( item.getLevel() ) ? "#000000" String color = warningLevel.keySet().contains( item.getLevel() ) ? "#000000"
: warningLevel.get( item.getLevel() ); : warningLevel.get( item.getLevel() );
} }
@ -191,44 +190,44 @@ class WechatOfficalAccountMessageParameter
WechatOfficalAccountMessageParameter() WechatOfficalAccountMessageParameter()
{ {
tplID = 57; tplID = 57;
groupID = 1; groupID = 1;
} }
@JsonProperty("tplid") @JsonProperty( "tplid")
private int tplID; private int tplID;
@JsonProperty("groupid") @JsonProperty( "groupid")
private int groupID; private int groupID;
@JsonProperty("first") @JsonProperty( "first")
private String first; private String first;
@JsonProperty("keyword1") @JsonProperty( "keyword1")
private String keyword1; private String keyword1;
@JsonProperty("keyword1color") @JsonProperty( "keyword1color")
private String keyword1Color; private String keyword1Color;
@JsonProperty("keyword2") @JsonProperty( "keyword2")
private String keyword2; private String keyword2;
@JsonProperty("keyword2color") @JsonProperty( "keyword2color")
private String keyword2Color; private String keyword2Color;
@JsonProperty("remark") @JsonProperty( "remark")
private String remark; private String remark;
@JsonProperty("remarkcolor") @JsonProperty( "remarkcolor")
private String remarkColor; private String remarkColor;
@JsonProperty("url") @JsonProperty( "url")
private String url; private String url;
@JsonProperty("appid") @JsonProperty( "appid")
private String appid; private String appid;
@JsonProperty("pagepath") @JsonProperty( "pagepath")
private String pagePath; private String pagePath;
public int getTplID() public int getTplID()