From 9c1f141a0011dacabe4f4535545975b8cf5bc3d6 Mon Sep 17 00:00:00 2001 From: Kane Wang Date: Fri, 22 Apr 2022 23:35:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=B0=8F=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/DisasterWarning/disaster_warning.iml | 22 ++--- .../java/DisasterWarning/disaster_warning.ipr | 61 ++++++++----- .../wechat/officalAccount/sendMessage.java | 91 +++++++++++-------- .../officalAccount/sendMessageTest.java | 14 +-- 4 files changed, 104 insertions(+), 84 deletions(-) diff --git a/code/java/DisasterWarning/disaster_warning.iml b/code/java/DisasterWarning/disaster_warning.iml index 93e5b3b..a54c133 100644 --- a/code/java/DisasterWarning/disaster_warning.iml +++ b/code/java/DisasterWarning/disaster_warning.iml @@ -21,12 +21,13 @@ - - - - - - + + + + + + + @@ -34,14 +35,5 @@ - - - - - - - - - \ No newline at end of file diff --git a/code/java/DisasterWarning/disaster_warning.ipr b/code/java/DisasterWarning/disaster_warning.ipr index ac62426..6106a45 100644 --- a/code/java/DisasterWarning/disaster_warning.ipr +++ b/code/java/DisasterWarning/disaster_warning.ipr @@ -21,6 +21,8 @@ + + @@ -265,37 +267,37 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -320,15 +322,15 @@ - + - + - + - + @@ -353,26 +355,37 @@ - + - + - + - + - + - + - + - + + + + + + + + + + + + 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 c18df87..9b90176 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,11 +2,11 @@ * @Author: Kane * @Date: 2022-04-22 10:53:49 * @LastEditors: Kane - * @LastEditTime: 2022-04-22 17:20:22 + * @LastEditTime: 2022-04-22 23:04:58 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java * @Description: 用来推送公众号消息的程序库。 - * - * Copyright (c) ${2022} by Kane, All Rights Reserved. + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. */ package com.cpic.xim.wechat.officalAccount; @@ -27,7 +27,7 @@ import com.cpic.xim.notify.disaster.*; /** * 推送微信公众号消息。 - * + * * @author Kane */ public class sendMessage @@ -38,12 +38,12 @@ public class sendMessage * 向公众号推送通知消息。接口文档参考 彭奕洁 编写《消息发送接口调用文档》 * * @param wechatOfficalAccountURL 公众号接口网址。 - * @param title 通知的标题。 - * @param notifyType 通知类型。 - * @param notifyMessage 通知文本内容。 + * @param title 通知的标题。 + * @param notifyType 通知类型。 + * @param notifyMessage 通知文本内容。 */ public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title, - String notifyType, String notifyMessage ) + String notifyType, String notifyMessage ) { // 设置推送内容 WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter(); @@ -55,7 +55,7 @@ public class sendMessage // 转换成json ObjectMapper mapper; - String json = null; + String json = null; try { @@ -64,14 +64,14 @@ public class sendMessage json = mapper.writeValueAsString( param ); - if ( json.isEmpty()) + if ( json.isEmpty() ) { } System.out.println( json ); } - catch (JsonProcessingException error) + catch ( JsonProcessingException error ) { error.printStackTrace(); @@ -79,16 +79,16 @@ public class sendMessage } // 推送数据 - URL url; + URL url; HttpURLConnection connection = null; - StringBuilder result = new StringBuilder(); - OutputStreamWriter out = null; - BufferedReader in = null; + StringBuilder result = new StringBuilder(); + OutputStreamWriter out = null; + BufferedReader in = null; try { - url = new URL( wechatOfficalAccountURL ); + url = new URL( wechatOfficalAccountURL ); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod( "POST" ); @@ -107,14 +107,14 @@ public class sendMessage String line = in.readLine(); - while (line != null) + while ( line != null ) { result.append( line ); line = in.readLine(); } } - catch (Exception error) + catch ( Exception error ) { error.printStackTrace(); } @@ -122,22 +122,22 @@ public class sendMessage { try { - if ( in != null) + if ( in != null ) { in.close(); } - if ( out != null) + if ( out != null ) { out.close(); } - if ( connection != null) + if ( connection != null ) { connection.disconnect(); } } - catch (Exception error) + catch ( Exception error ) { error.printStackTrace(); } @@ -147,16 +147,31 @@ public class sendMessage /** * 推送天气灾害预警!接口文档参考 彭奕洁 编写《消息发送接口调用文档》 + * * @param officalAccountURL 产险厦门分公司公众号接口网址 - * @param warning 灾害警报 + * @param warning 灾害警报对象 */ public static void sendWeatherDisasterWarning( String officalAccountURL, - QWeatherDisasterWarning warning ) + QWeatherDisasterWarning warning ) { Vector warningItems = warning.getWarning(); - // 遍历消息 + // 设置请求头参数 + HashMap headers = new HashMap(); + headers.put( "accept", "*/*" ); + headers.put( "Connection", "Keep-Alive" ); + headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" ); + + // 遍历消息,将消息推送出去。 + for ( QWeatherDisasterWarningItem item : warningItems ) + { + // 拼接消息内容 + String title = item.getTitle(); + String text = item.getText(); + String color = warningLevel.keySet().contains( item.getLevel() ) ? "#000000" + : warningLevel.get( item.getLevel() ); + } } static @@ -176,44 +191,44 @@ class WechatOfficalAccountMessageParameter WechatOfficalAccountMessageParameter() { - tplID = 57; + tplID = 57; groupID = 1; } - @JsonProperty( "tplid") + @JsonProperty("tplid") private int tplID; - @JsonProperty( "groupid") + @JsonProperty("groupid") private int groupID; - @JsonProperty( "first") + @JsonProperty("first") private String first; - @JsonProperty( "keyword1") + @JsonProperty("keyword1") private String keyword1; - @JsonProperty( "keyword1color") + @JsonProperty("keyword1color") private String keyword1Color; - @JsonProperty( "keyword2") + @JsonProperty("keyword2") private String keyword2; - @JsonProperty( "keyword2color") + @JsonProperty("keyword2color") private String keyword2Color; - @JsonProperty( "remark") + @JsonProperty("remark") private String remark; - @JsonProperty( "remarkcolor") + @JsonProperty("remarkcolor") private String remarkColor; - @JsonProperty( "url") + @JsonProperty("url") private String url; - @JsonProperty( "appid") + @JsonProperty("appid") private String appid; - @JsonProperty( "pagepath") + @JsonProperty("pagepath") private String pagePath; public int getTplID() 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 81c4ed7..51cc096 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 @@ -4,9 +4,9 @@ * @LastEditors: Kane * @LastEditTime: 2022-04-22 16:02:05 * @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java - * @Description: - * - * Copyright (c) ${2022} by Kane, All Rights Reserved. + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. */ package com.cpic.xim.wechat.officalAccount; @@ -28,7 +28,7 @@ public class sendMessageTest { // sendMessage.postNotifyMessageJSON( url, "警报", "警报标题", "警报内容!" ); } - catch (Exception error) + catch ( Exception error ) { fail( "测试失败!" ); } @@ -42,15 +42,15 @@ public class sendMessageTest + "keyword1=东山县气象台发布大雾黄色预警[Ⅲ级/较重]&" + "keyword1color=#ffff00&" + "keyword2=东山县气象台2022年04月22日06时12分发布大雾黄色预警信号:预计未来12小时内,我县、闽南渔场和台湾浅滩渔场将出现能见度小于500米的雾。请注意防范!"; - // headers.put( "accept", "*/*" ); - // headers.put( "Connection", "Keep-Alive" ); + headers.put( "accept", "*/*" ); + headers.put( "Connection", "Keep-Alive" ); headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" ); try { HttpUtils.postHttpRequest( url, headers, param ); } - catch (Exception error) + catch ( Exception error ) { }