1
0
mirror of synced 2025-12-26 20:48:00 +08:00

#256 企业微信发送消息增加文本卡片消息的支持

This commit is contained in:
Binary Wang
2017-07-02 17:01:51 +08:00
parent 131402f8f6
commit 22344ebe2c
7 changed files with 85 additions and 66 deletions

View File

@@ -30,6 +30,7 @@ public class WxCpMessage implements Serializable {
private String musicUrl;
private String hqMusicUrl;
private String safe;
private String url;
private List<NewArticle> articles = new ArrayList<>();
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
@@ -40,6 +41,13 @@ public class WxCpMessage implements Serializable {
return new TextBuilder();
}
/**
* 获得文本卡片消息builder
*/
public static TextCardBuilder TEXTCARD() {
return new TextCardBuilder();
}
/**
* 获得图片消息builder
*/
@@ -220,4 +228,11 @@ public class WxCpMessage implements Serializable {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import org.apache.commons.lang3.StringUtils;
public class BaseBuilder<T> {
protected String msgType;
@@ -43,8 +44,7 @@ public class BaseBuilder<T> {
m.setToUser(this.toUser);
m.setToParty(this.toParty);
m.setToTag(this.toTag);
m.setSafe(
(this.safe == null || "".equals(this.safe)) ? WxConsts.CUSTOM_MSG_SAFE_NO : this.safe);
m.setSafe(StringUtils.defaultIfBlank(this.safe, WxConsts.CUSTOM_MSG_SAFE_NO));
return m;
}

View File

@@ -0,0 +1,47 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* <pre>
* 文本卡片消息Builder
* 用法: WxCustomMessage m = WxCustomMessage.TEXTCARD().title(...)....toUser(...).build();
* Created by Binary Wang on 2017-7-2.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class TextCardBuilder extends BaseBuilder<TextCardBuilder> {
private String title;
private String description;
private String url;
public TextCardBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_TEXTCARD;
}
public TextCardBuilder title(String title) {
this.title = title;
return this;
}
public TextCardBuilder description(String description) {
this.description = description;
return this;
}
public TextCardBuilder url(String url) {
this.url = url;
return this;
}
@Override
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setTitle(this.title);
m.setDescription(this.description);
m.setUrl(this.url);
return m;
}
}

View File

@@ -43,6 +43,14 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.add("text", text);
}
if (WxConsts.CUSTOM_MSG_TEXTCARD.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("title", message.getTitle());
text.addProperty("description", message.getDescription());
text.addProperty("url", message.getUrl());
messageJson.add("textcard", text);
}
if (WxConsts.CUSTOM_MSG_IMAGE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());