diff --git a/src/main/java/chanjarster/weixin/bean/WxCustomMessage.java b/src/main/java/chanjarster/weixin/bean/WxCustomMessage.java index a64648819..cac92eff0 100644 --- a/src/main/java/chanjarster/weixin/bean/WxCustomMessage.java +++ b/src/main/java/chanjarster/weixin/bean/WxCustomMessage.java @@ -4,6 +4,13 @@ import java.util.ArrayList; import java.util.List; import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.custom.ImageBuilder; +import chanjarster.weixin.bean.custom.MusicBuilder; +import chanjarster.weixin.bean.custom.NewsArticleBuilder; +import chanjarster.weixin.bean.custom.NewsBuilder; +import chanjarster.weixin.bean.custom.TextBuilder; +import chanjarster.weixin.bean.custom.VideoBuilder; +import chanjarster.weixin.bean.custom.VoiceBuilder; import chanjarster.weixin.util.json.WxGsonBuilder; /** @@ -135,4 +142,61 @@ public class WxCustomMessage { } } + + /** + * 获得文本消息builder + * @return + */ + public static TextBuilder TEXT() { + return new TextBuilder(); + } + + /** + * 获得图片消息builder + * @return + */ + public static ImageBuilder IMAGE() { + return new ImageBuilder(); + } + + /** + * 获得语音消息builder + * @return + */ + public static VoiceBuilder VOICE() { + return new VoiceBuilder(); + } + + /** + * 获得视频消息builder + * @return + */ + public static VideoBuilder VIDEO() { + return new VideoBuilder(); + } + + /** + * 获得音乐消息builder + * @return + */ + public static MusicBuilder MUSIC() { + return new MusicBuilder(); + } + + /** + * 获得图文消息builder + * @return + */ + public static NewsBuilder NEWS() { + return new NewsBuilder(); + } + + /** + * 获得图文消息文章builder + * @return + */ + public static NewsArticleBuilder NEWS_ARTICLE() { + return new NewsArticleBuilder(); + } + } diff --git a/src/main/java/chanjarster/weixin/bean/custom/BaseBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/BaseBuilder.java new file mode 100644 index 000000000..24fd24e58 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/BaseBuilder.java @@ -0,0 +1,20 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.bean.WxCustomMessage; + +public class BaseBuilder { + protected String msgtype; + protected String touser; + + public T touser(String touser) { + this.touser = touser; + return (T) this; + } + + public WxCustomMessage build() { + WxCustomMessage m = new WxCustomMessage(); + m.setMsgtype(this.msgtype); + m.setTouser(this.touser); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/ImageBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/ImageBuilder.java new file mode 100644 index 000000000..7368af8c2 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/ImageBuilder.java @@ -0,0 +1,31 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.WxCustomMessage; + +/** + * 获得消息builder + *
+ * 用法: WxCustomMessage m = WxCustomMessage.IMAGE().media_id(...).touser(...).build();
+ * 
+ * @author chanjarster + * + */ +public final class ImageBuilder extends BaseBuilder { + private String media_id; + + public ImageBuilder() { + this.msgtype = WxConsts.CUSTOM_MSG_IMAGE; + } + + public ImageBuilder media_id(String media_id) { + this.media_id = media_id; + return this; + } + + public WxCustomMessage build() { + WxCustomMessage m = super.build(); + m.setMedia_id(this.media_id); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/MusicBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/MusicBuilder.java new file mode 100644 index 000000000..1c353a099 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/MusicBuilder.java @@ -0,0 +1,64 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.WxCustomMessage; + +/** + * 音乐消息builder + *
+ * 用法: WxCustomMessage m = WxCustomMessage.MUSIC()
+ *                      .musicurl(...)
+ *                      .hqmusicurl(...)
+ *                      .title(...)
+ *                      .thumb_media_id(..)
+ *                      .description(..)
+ *                      .touser(...)
+ *                      .build();
+ * 
+ */ +public final class MusicBuilder extends BaseBuilder { + private String title; + private String description; + private String thumb_media_id; + private String musicurl; + private String hqmusicurl; + + public MusicBuilder() { + this.msgtype = WxConsts.CUSTOM_MSG_MUSIC; + } + + public MusicBuilder musicurl(String musicurl) { + this.musicurl = musicurl; + return this; + } + + public MusicBuilder hqmusicurl(String hqmusicurl) { + this.hqmusicurl = hqmusicurl; + return this; + } + + public MusicBuilder title(String title) { + this.title = title; + return this; + } + + public MusicBuilder description(String description) { + this.description = description; + return this; + } + + public MusicBuilder thumb_media_id(String thumb_media_id) { + this.thumb_media_id = thumb_media_id; + return this; + } + + public WxCustomMessage build() { + WxCustomMessage m = super.build(); + m.setMusicurl(this.musicurl); + m.setHqmusicurl(this.hqmusicurl); + m.setTitle(title); + m.setDescription(description); + m.setThumb_media_id(thumb_media_id); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/NewsArticleBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/NewsArticleBuilder.java new file mode 100644 index 000000000..88344eab6 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/NewsArticleBuilder.java @@ -0,0 +1,52 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.bean.WxCustomMessage; + +/** + * 图文消息文章builder + *
+ * 用法: WxCustomMessage.WxArticle m = WxCustomMessage.NEWS_ARTICLE()
+ *                              .url(...)
+ *                              .title(...)
+ *                              .picurl(...)
+ *                              .description(...)
+ *                              .build();
+ * 
+ * @author chanjarster + * + */ +public final class NewsArticleBuilder { + private String title; + private String description; + private String url; + private String picurl; + + public NewsArticleBuilder url(String url) { + this.url = url; + return this; + } + + public NewsArticleBuilder title(String title) { + this.title = title; + return this; + } + + public NewsArticleBuilder description(String description) { + this.description = description; + return this; + } + + public NewsArticleBuilder picurl(String picurl) { + this.picurl = picurl; + return this; + } + + public WxCustomMessage.WxArticle build() { + WxCustomMessage.WxArticle m = new WxCustomMessage.WxArticle(); + m.setPicurl(this.picurl); + m.setTitle(title); + m.setDescription(description); + m.setUrl(this.url); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/NewsBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/NewsBuilder.java new file mode 100644 index 000000000..9680720ae --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/NewsBuilder.java @@ -0,0 +1,43 @@ +package chanjarster.weixin.bean.custom; + +import java.util.ArrayList; +import java.util.List; + +import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.WxCustomMessage; +import chanjarster.weixin.bean.WxCustomMessage.WxArticle; + +/** + * 图文消息builder + *
+ * 用法:
+ * WxCustomMessage.WxArticle article = WxCustomMessage.NEWS_ARTICLE()
+ *                              .url(...)
+ *                              .title(...)
+ *                              .picurl(...)
+ *                              .description(...)
+ *                              .build();
+ * WxCustomMessage m = WxCustomMessage.NEWS().addArticle(article).touser(...).build();
+ * 
+ * @author chanjarster + * + */ +public final class NewsBuilder extends BaseBuilder { + + private List articles = new ArrayList(); + + public NewsBuilder() { + this.msgtype = WxConsts.CUSTOM_MSG_NEWS; + } + + public NewsBuilder addArticle(WxCustomMessage.WxArticle article) { + this.articles.add(article); + return this; + } + + public WxCustomMessage build() { + WxCustomMessage m = super.build(); + m.setArticles(this.articles); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/TextBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/TextBuilder.java new file mode 100644 index 000000000..573190a26 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/TextBuilder.java @@ -0,0 +1,31 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.WxCustomMessage; + +/** + * 文本消息builder + *
+ * 用法: WxCustomMessage m = WxCustomMessage.TEXT().content(...).touser(...).build();
+ * 
+ * @author chanjarster + * + */ +public final class TextBuilder extends BaseBuilder { + private String content; + + public TextBuilder() { + this.msgtype = WxConsts.CUSTOM_MSG_TEXT; + } + + public TextBuilder content(String content) { + this.content = content; + return this; + } + + public WxCustomMessage build() { + WxCustomMessage m = super.build(); + m.setContent(this.content); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/VideoBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/VideoBuilder.java new file mode 100644 index 000000000..71ee1089d --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/VideoBuilder.java @@ -0,0 +1,58 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.WxCustomMessage; + +/** + * 视频消息builder + *
+ * 用法: WxCustomMessage m = WxCustomMessage.VOICE()
+ *                              .media_id(...)
+ *                              .title(...)
+ *                              .thumb_media_id(..)
+ *                              .description(..)
+ *                              .touser(...)
+ *                              .build();
+ * 
+ * @author chanjarster + * + */ +public final class VideoBuilder extends BaseBuilder { + private String media_id; + private String title; + private String description; + private String thumb_media_id; + + public VideoBuilder() { + this.msgtype = WxConsts.CUSTOM_MSG_VIDEO; + } + + public VideoBuilder media_id(String media_id) { + this.media_id = media_id; + return this; + } + + public VideoBuilder title(String title) { + this.title = title; + return this; + } + + public VideoBuilder description(String description) { + this.description = description; + return this; + } + + public VideoBuilder thumb_media_id(String thumb_media_id) { + this.thumb_media_id = thumb_media_id; + return this; + } + + public WxCustomMessage build() { + WxCustomMessage m = super.build(); + m.setMedia_id(this.media_id); + m.setTitle(title); + m.setDescription(description); + m.setThumb_media_id(thumb_media_id); + return m; + } +} diff --git a/src/main/java/chanjarster/weixin/bean/custom/VoiceBuilder.java b/src/main/java/chanjarster/weixin/bean/custom/VoiceBuilder.java new file mode 100644 index 000000000..5330077d4 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/custom/VoiceBuilder.java @@ -0,0 +1,31 @@ +package chanjarster.weixin.bean.custom; + +import chanjarster.weixin.api.WxConsts; +import chanjarster.weixin.bean.WxCustomMessage; + +/** + * 语音消息builder + *
+ * 用法: WxCustomMessage m = WxCustomMessage.VOICE().media_id(...).touser(...).build();
+ * 
+ * @author chanjarster + * + */ +public final class VoiceBuilder extends BaseBuilder { + private String media_id; + + public VoiceBuilder() { + this.msgtype = WxConsts.CUSTOM_MSG_VOICE; + } + + public VoiceBuilder media_id(String media_id) { + this.media_id = media_id; + return this; + } + + public WxCustomMessage build() { + WxCustomMessage m = super.build(); + m.setMedia_id(this.media_id); + return m; + } +}