@@ -4,6 +4,13 @@ import java.util.ArrayList; | |||||
import java.util.List; | import java.util.List; | ||||
import chanjarster.weixin.api.WxConsts; | 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; | 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(); | |||||
} | |||||
} | } |
@@ -0,0 +1,20 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
public class BaseBuilder<T> { | |||||
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; | |||||
} | |||||
} |
@@ -0,0 +1,31 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.api.WxConsts; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
/** | |||||
* 获得消息builder | |||||
* <pre> | |||||
* 用法: WxCustomMessage m = WxCustomMessage.IMAGE().media_id(...).touser(...).build(); | |||||
* </pre> | |||||
* @author chanjarster | |||||
* | |||||
*/ | |||||
public final class ImageBuilder extends BaseBuilder<ImageBuilder> { | |||||
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; | |||||
} | |||||
} |
@@ -0,0 +1,64 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.api.WxConsts; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
/** | |||||
* 音乐消息builder | |||||
* <pre> | |||||
* 用法: WxCustomMessage m = WxCustomMessage.MUSIC() | |||||
* .musicurl(...) | |||||
* .hqmusicurl(...) | |||||
* .title(...) | |||||
* .thumb_media_id(..) | |||||
* .description(..) | |||||
* .touser(...) | |||||
* .build(); | |||||
* </pre> | |||||
*/ | |||||
public final class MusicBuilder extends BaseBuilder<MusicBuilder> { | |||||
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; | |||||
} | |||||
} |
@@ -0,0 +1,52 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
/** | |||||
* 图文消息文章builder | |||||
* <pre> | |||||
* 用法: WxCustomMessage.WxArticle m = WxCustomMessage.NEWS_ARTICLE() | |||||
* .url(...) | |||||
* .title(...) | |||||
* .picurl(...) | |||||
* .description(...) | |||||
* .build(); | |||||
* </pre> | |||||
* @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; | |||||
} | |||||
} |
@@ -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 | |||||
* <pre> | |||||
* 用法: | |||||
* WxCustomMessage.WxArticle article = WxCustomMessage.NEWS_ARTICLE() | |||||
* .url(...) | |||||
* .title(...) | |||||
* .picurl(...) | |||||
* .description(...) | |||||
* .build(); | |||||
* WxCustomMessage m = WxCustomMessage.NEWS().addArticle(article).touser(...).build(); | |||||
* </pre> | |||||
* @author chanjarster | |||||
* | |||||
*/ | |||||
public final class NewsBuilder extends BaseBuilder<NewsBuilder> { | |||||
private List<WxArticle> articles = new ArrayList<WxArticle>(); | |||||
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; | |||||
} | |||||
} |
@@ -0,0 +1,31 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.api.WxConsts; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
/** | |||||
* 文本消息builder | |||||
* <pre> | |||||
* 用法: WxCustomMessage m = WxCustomMessage.TEXT().content(...).touser(...).build(); | |||||
* </pre> | |||||
* @author chanjarster | |||||
* | |||||
*/ | |||||
public final class TextBuilder extends BaseBuilder<TextBuilder> { | |||||
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; | |||||
} | |||||
} |
@@ -0,0 +1,58 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.api.WxConsts; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
/** | |||||
* 视频消息builder | |||||
* <pre> | |||||
* 用法: WxCustomMessage m = WxCustomMessage.VOICE() | |||||
* .media_id(...) | |||||
* .title(...) | |||||
* .thumb_media_id(..) | |||||
* .description(..) | |||||
* .touser(...) | |||||
* .build(); | |||||
* </pre> | |||||
* @author chanjarster | |||||
* | |||||
*/ | |||||
public final class VideoBuilder extends BaseBuilder<VideoBuilder> { | |||||
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; | |||||
} | |||||
} |
@@ -0,0 +1,31 @@ | |||||
package chanjarster.weixin.bean.custom; | |||||
import chanjarster.weixin.api.WxConsts; | |||||
import chanjarster.weixin.bean.WxCustomMessage; | |||||
/** | |||||
* 语音消息builder | |||||
* <pre> | |||||
* 用法: WxCustomMessage m = WxCustomMessage.VOICE().media_id(...).touser(...).build(); | |||||
* </pre> | |||||
* @author chanjarster | |||||
* | |||||
*/ | |||||
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder> { | |||||
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; | |||||
} | |||||
} |