99import lombok .Builder ;
1010import lombok .Data ;
1111import lombok .NoArgsConstructor ;
12- import me .chanjar .weixin .common .api .WxConsts ;
13- import me .chanjar .weixin .cp .WxCpConsts ;
12+ import me .chanjar .weixin .cp .WxCpConsts .AppChatMsgType ;
1413import me .chanjar .weixin .cp .bean .article .MpnewsArticle ;
1514import me .chanjar .weixin .cp .bean .article .NewArticle ;
1615
@@ -46,7 +45,7 @@ public class WxCpAppChatMessage implements Serializable {
4645 */
4746 public static WxCpAppChatMessage buildTextMsg (String chatId , String content , boolean safe ) {
4847 final WxCpAppChatMessage message = new WxCpAppChatMessage ();
49- message .setMsgType (WxCpConsts . AppChatMsgType .TEXT );
48+ message .setMsgType (AppChatMsgType .TEXT );
5049 message .setContent (content );
5150 message .setChatId (chatId );
5251 message .setSafe (safe );
@@ -61,94 +60,104 @@ public String toJson() {
6160 messageJson .addProperty ("msgtype" , this .getMsgType ());
6261 messageJson .addProperty ("chatid" , this .getChatId ());
6362
64- if (WxConsts .KefuMsgType .TEXT .equals (this .getMsgType ())) {
65- JsonObject text = new JsonObject ();
66- text .addProperty ("content" , this .getContent ());
67- messageJson .add ("text" , text );
68- }
69-
70- if (WxConsts .KefuMsgType .MARKDOWN .equals (this .getMsgType ())) {
71- JsonObject text = new JsonObject ();
72- text .addProperty ("content" , this .getContent ());
73- messageJson .add ("markdown" , text );
74- }
75-
76- if (WxConsts .KefuMsgType .TEXTCARD .equals (this .getMsgType ())) {
77- JsonObject text = new JsonObject ();
78- text .addProperty ("title" , this .getTitle ());
79- text .addProperty ("description" , this .getDescription ());
80- text .addProperty ("url" , this .getUrl ());
81- text .addProperty ("btntxt" , this .getBtnTxt ());
82- messageJson .add ("textcard" , text );
83- }
84-
85- if (WxConsts .KefuMsgType .IMAGE .equals (this .getMsgType ())) {
86- JsonObject image = new JsonObject ();
87- image .addProperty ("media_id" , this .getMediaId ());
88- messageJson .add ("image" , image );
89- }
90-
91- if (WxConsts .KefuMsgType .FILE .equals (this .getMsgType ())) {
92- JsonObject image = new JsonObject ();
93- image .addProperty ("media_id" , this .getMediaId ());
94- messageJson .add ("file" , image );
95- }
96-
97- if (WxConsts .KefuMsgType .VOICE .equals (this .getMsgType ())) {
98- JsonObject voice = new JsonObject ();
99- voice .addProperty ("media_id" , this .getMediaId ());
100- messageJson .add ("voice" , voice );
101- }
102-
10363 if (this .getSafe () != null && this .getSafe ()) {
10464 messageJson .addProperty ("safe" , 1 );
10565 }
10666
107- if (WxConsts .KefuMsgType .VIDEO .equals (this .getMsgType ())) {
108- JsonObject video = new JsonObject ();
109- video .addProperty ("media_id" , this .getMediaId ());
110- video .addProperty ("title" , this .getTitle ());
111- video .addProperty ("description" , this .getDescription ());
112- messageJson .add ("video" , video );
113- }
67+ this .handleMsgType (messageJson );
11468
115- if (WxConsts .KefuMsgType .NEWS .equals (this .getMsgType ())) {
116- JsonObject newsJsonObject = new JsonObject ();
117- JsonArray articleJsonArray = new JsonArray ();
118- for (NewArticle article : this .getArticles ()) {
119- JsonObject articleJson = new JsonObject ();
120- articleJson .addProperty ("title" , article .getTitle ());
121- articleJson .addProperty ("description" , article .getDescription ());
122- articleJson .addProperty ("url" , article .getUrl ());
123- articleJson .addProperty ("picurl" , article .getPicUrl ());
124- articleJsonArray .add (articleJson );
125- }
126- newsJsonObject .add ("articles" , articleJsonArray );
127- messageJson .add ("news" , newsJsonObject );
128- }
69+ return messageJson .toString ();
70+ }
12971
130- if (WxConsts .KefuMsgType .MPNEWS .equals (this .getMsgType ())) {
131- JsonObject newsJsonObject = new JsonObject ();
132- if (this .getMediaId () != null ) {
133- newsJsonObject .addProperty ("media_id" , this .getMediaId ());
134- } else {
72+ private void handleMsgType (JsonObject messageJson ) {
73+ switch (this .getMsgType ()) {
74+ case AppChatMsgType .TEXT : {
75+ JsonObject text = new JsonObject ();
76+ text .addProperty ("content" , this .getContent ());
77+ messageJson .add ("text" , text );
78+ break ;
79+ }
80+ case AppChatMsgType .MARKDOWN : {
81+ JsonObject text = new JsonObject ();
82+ text .addProperty ("content" , this .getContent ());
83+ messageJson .add ("markdown" , text );
84+ break ;
85+ }
86+ case AppChatMsgType .TEXTCARD : {
87+ JsonObject text = new JsonObject ();
88+ text .addProperty ("title" , this .getTitle ());
89+ text .addProperty ("description" , this .getDescription ());
90+ text .addProperty ("url" , this .getUrl ());
91+ text .addProperty ("btntxt" , this .getBtnTxt ());
92+ messageJson .add ("textcard" , text );
93+ break ;
94+ }
95+ case AppChatMsgType .IMAGE : {
96+ JsonObject image = new JsonObject ();
97+ image .addProperty ("media_id" , this .getMediaId ());
98+ messageJson .add ("image" , image );
99+ break ;
100+ }
101+ case AppChatMsgType .FILE : {
102+ JsonObject image = new JsonObject ();
103+ image .addProperty ("media_id" , this .getMediaId ());
104+ messageJson .add ("file" , image );
105+ break ;
106+ }
107+ case AppChatMsgType .VOICE : {
108+ JsonObject voice = new JsonObject ();
109+ voice .addProperty ("media_id" , this .getMediaId ());
110+ messageJson .add ("voice" , voice );
111+ break ;
112+ }
113+ case AppChatMsgType .VIDEO : {
114+ JsonObject video = new JsonObject ();
115+ video .addProperty ("media_id" , this .getMediaId ());
116+ video .addProperty ("title" , this .getTitle ());
117+ video .addProperty ("description" , this .getDescription ());
118+ messageJson .add ("video" , video );
119+ break ;
120+ }
121+ case AppChatMsgType .NEWS : {
122+ JsonObject newsJsonObject = new JsonObject ();
135123 JsonArray articleJsonArray = new JsonArray ();
136- for (MpnewsArticle article : this .getMpnewsArticles ()) {
124+ for (NewArticle article : this .getArticles ()) {
137125 JsonObject articleJson = new JsonObject ();
138126 articleJson .addProperty ("title" , article .getTitle ());
139- articleJson .addProperty ("thumb_media_id" , article .getThumbMediaId ());
140- articleJson .addProperty ("author" , article .getAuthor ());
141- articleJson .addProperty ("content_source_url" , article .getContentSourceUrl ());
142- articleJson .addProperty ("content" , article .getContent ());
143- articleJson .addProperty ("digest" , article .getDigest ());
127+ articleJson .addProperty ("description" , article .getDescription ());
128+ articleJson .addProperty ("url" , article .getUrl ());
129+ articleJson .addProperty ("picurl" , article .getPicUrl ());
144130 articleJsonArray .add (articleJson );
145131 }
146-
147132 newsJsonObject .add ("articles" , articleJsonArray );
133+ messageJson .add ("news" , newsJsonObject );
134+ break ;
135+ }
136+ case AppChatMsgType .MPNEWS : {
137+ JsonObject newsJsonObject = new JsonObject ();
138+ if (this .getMediaId () != null ) {
139+ newsJsonObject .addProperty ("media_id" , this .getMediaId ());
140+ } else {
141+ JsonArray articleJsonArray = new JsonArray ();
142+ for (MpnewsArticle article : this .getMpnewsArticles ()) {
143+ JsonObject articleJson = new JsonObject ();
144+ articleJson .addProperty ("title" , article .getTitle ());
145+ articleJson .addProperty ("thumb_media_id" , article .getThumbMediaId ());
146+ articleJson .addProperty ("author" , article .getAuthor ());
147+ articleJson .addProperty ("content_source_url" , article .getContentSourceUrl ());
148+ articleJson .addProperty ("content" , article .getContent ());
149+ articleJson .addProperty ("digest" , article .getDigest ());
150+ articleJsonArray .add (articleJson );
151+ }
152+
153+ newsJsonObject .add ("articles" , articleJsonArray );
154+ }
155+ messageJson .add ("mpnews" , newsJsonObject );
156+ break ;
157+ }
158+ default : {
159+ //do nothing
148160 }
149- messageJson .add ("mpnews" , newsJsonObject );
150161 }
151-
152- return messageJson .toString ();
153162 }
154163}
0 commit comments