mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
重构bean和builder的包结构
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
public class BaseBuilder<T> {
|
||||
protected String msgType;
|
||||
protected String toUser;
|
||||
|
||||
public T toUser(String toUser) {
|
||||
this.toUser = toUser;
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = new WxMpKefuMessage();
|
||||
m.setMsgType(this.msgType);
|
||||
m.setToUser(this.toUser);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 获得消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpKefuMessage.IMAGE().mediaId(...).toUser(...).build();
|
||||
* </pre>
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
|
||||
private String mediaId;
|
||||
|
||||
public ImageBuilder() {
|
||||
this.msgType = WxConsts.CUSTOM_MSG_IMAGE;
|
||||
}
|
||||
|
||||
public ImageBuilder mediaId(String media_id) {
|
||||
this.mediaId = media_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setMediaId(this.mediaId);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 图文消息builder
|
||||
* <pre>
|
||||
* 用法:
|
||||
* WxMpKefuMessage m = WxMpKefuMessage.NEWS().mediaId("xxxxx").toUser(...).build();
|
||||
* </pre>
|
||||
* @author Binary Wang
|
||||
*
|
||||
*/
|
||||
public final class MpNewsBuilder extends BaseBuilder<MpNewsBuilder> {
|
||||
private String mediaId;
|
||||
|
||||
public MpNewsBuilder() {
|
||||
this.msgType = WxConsts.CUSTOM_MSG_MPNEWS;
|
||||
}
|
||||
|
||||
public MpNewsBuilder mediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setMpNewsMediaId(this.mediaId);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 音乐消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpKefuMessage.MUSIC()
|
||||
* .musicUrl(...)
|
||||
* .hqMusicUrl(...)
|
||||
* .title(...)
|
||||
* .thumbMediaId(..)
|
||||
* .description(..)
|
||||
* .toUser(...)
|
||||
* .build();
|
||||
* </pre>
|
||||
*/
|
||||
public final class MusicBuilder extends BaseBuilder<MusicBuilder> {
|
||||
private String title;
|
||||
private String description;
|
||||
private String thumbMediaId;
|
||||
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 thumbMediaId(String thumb_media_id) {
|
||||
this.thumbMediaId = thumb_media_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setMusicUrl(this.musicUrl);
|
||||
m.setHqMusicUrl(this.hqMusicUrl);
|
||||
m.setTitle(this.title);
|
||||
m.setDescription(this.description);
|
||||
m.setThumbMediaId(this.thumbMediaId);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 图文消息builder
|
||||
* <pre>
|
||||
* 用法:
|
||||
* WxMpKefuMessage m = WxMpKefuMessage.NEWS().addArticle(article).toUser(...).build();
|
||||
* </pre>
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public final class NewsBuilder extends BaseBuilder<NewsBuilder> {
|
||||
|
||||
private List<WxMpKefuMessage.WxArticle> articles = new ArrayList<>();
|
||||
|
||||
public NewsBuilder() {
|
||||
this.msgType = WxConsts.CUSTOM_MSG_NEWS;
|
||||
}
|
||||
|
||||
public NewsBuilder addArticle(WxMpKefuMessage.WxArticle article) {
|
||||
this.articles.add(article);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setArticles(this.articles);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 文本消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpKefuMessage.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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setContent(this.content);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 视频消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpKefuMessage.VOICE()
|
||||
* .mediaId(...)
|
||||
* .title(...)
|
||||
* .thumbMediaId(..)
|
||||
* .description(..)
|
||||
* .toUser(...)
|
||||
* .build();
|
||||
* </pre>
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public final class VideoBuilder extends BaseBuilder<VideoBuilder> {
|
||||
private String mediaId;
|
||||
private String title;
|
||||
private String description;
|
||||
private String thumbMediaId;
|
||||
|
||||
public VideoBuilder() {
|
||||
this.msgType = WxConsts.CUSTOM_MSG_VIDEO;
|
||||
}
|
||||
|
||||
public VideoBuilder mediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VideoBuilder title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VideoBuilder description(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VideoBuilder thumbMediaId(String thumb_media_id) {
|
||||
this.thumbMediaId = thumb_media_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setMediaId(this.mediaId);
|
||||
m.setTitle(this.title);
|
||||
m.setDescription(this.description);
|
||||
m.setThumbMediaId(this.thumbMediaId);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 语音消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpKefuMessage.VOICE().mediaId(...).toUser(...).build();
|
||||
* </pre>
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder> {
|
||||
private String mediaId;
|
||||
|
||||
public VoiceBuilder() {
|
||||
this.msgType = WxConsts.CUSTOM_MSG_VOICE;
|
||||
}
|
||||
|
||||
public VoiceBuilder mediaId(String media_id) {
|
||||
this.mediaId = media_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setMediaId(this.mediaId);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.mp.builder.kefu;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
|
||||
/**
|
||||
* 卡券消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpKefuMessage.WXCARD().cardId(...).toUser(...).build();
|
||||
* </pre>
|
||||
* @author mgcnrx11
|
||||
*
|
||||
*/
|
||||
public final class WxCardBuilder extends BaseBuilder<WxCardBuilder> {
|
||||
private String cardId;
|
||||
|
||||
public WxCardBuilder() {
|
||||
this.msgType = WxConsts.CUSTOM_MSG_WXCARD;
|
||||
}
|
||||
|
||||
public WxCardBuilder cardId(String cardId) {
|
||||
this.cardId = cardId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKefuMessage build() {
|
||||
WxMpKefuMessage m = super.build();
|
||||
m.setCardId(this.cardId);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
||||
|
||||
public abstract class BaseBuilder<BuilderType, ValueType> {
|
||||
|
||||
protected String toUserName;
|
||||
|
||||
protected String fromUserName;
|
||||
|
||||
public BuilderType toUser(String touser) {
|
||||
this.toUserName = touser;
|
||||
return (BuilderType) this;
|
||||
}
|
||||
|
||||
public BuilderType fromUser(String fromusername) {
|
||||
this.fromUserName = fromusername;
|
||||
return (BuilderType) this;
|
||||
}
|
||||
|
||||
public abstract ValueType build();
|
||||
|
||||
public void setCommon(WxMpXmlOutMessage m) {
|
||||
m.setToUserName(this.toUserName);
|
||||
m.setFromUserName(this.fromUserName);
|
||||
m.setCreateTime(System.currentTimeMillis() / 1000l);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutImageMessage;
|
||||
|
||||
/**
|
||||
* 图片消息builder
|
||||
* @author chanjarster
|
||||
*/
|
||||
public final class ImageBuilder extends BaseBuilder<ImageBuilder, WxMpXmlOutImageMessage> {
|
||||
|
||||
private String mediaId;
|
||||
|
||||
public ImageBuilder mediaId(String media_id) {
|
||||
this.mediaId = media_id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutImageMessage build() {
|
||||
WxMpXmlOutImageMessage m = new WxMpXmlOutImageMessage();
|
||||
setCommon(m);
|
||||
m.setMediaId(this.mediaId);
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMusicMessage;
|
||||
|
||||
/**
|
||||
* 音乐消息builder
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
public final class MusicBuilder extends BaseBuilder<MusicBuilder, WxMpXmlOutMusicMessage> {
|
||||
|
||||
private String title;
|
||||
private String description;
|
||||
private String hqMusicUrl;
|
||||
private String musicUrl;
|
||||
private String thumbMediaId;
|
||||
|
||||
public MusicBuilder title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MusicBuilder description(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MusicBuilder hqMusicUrl(String hqMusicUrl) {
|
||||
this.hqMusicUrl = hqMusicUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MusicBuilder musicUrl(String musicUrl) {
|
||||
this.musicUrl = musicUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MusicBuilder thumbMediaId(String thumbMediaId) {
|
||||
this.thumbMediaId = thumbMediaId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutMusicMessage build() {
|
||||
WxMpXmlOutMusicMessage m = new WxMpXmlOutMusicMessage();
|
||||
setCommon(m);
|
||||
m.setTitle(this.title);
|
||||
m.setDescription(this.description);
|
||||
m.setHqMusicUrl(this.hqMusicUrl);
|
||||
m.setMusicUrl(this.musicUrl);
|
||||
m.setThumbMediaId(this.thumbMediaId);
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 图文消息builder
|
||||
* @author chanjarster
|
||||
*/
|
||||
public final class NewsBuilder extends BaseBuilder<NewsBuilder, WxMpXmlOutNewsMessage> {
|
||||
|
||||
protected final List<WxMpXmlOutNewsMessage.Item> articles = new ArrayList<>();
|
||||
|
||||
public NewsBuilder addArticle(WxMpXmlOutNewsMessage.Item item) {
|
||||
this.articles.add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutNewsMessage build() {
|
||||
WxMpXmlOutNewsMessage m = new WxMpXmlOutNewsMessage();
|
||||
for(WxMpXmlOutNewsMessage.Item item : this.articles) {
|
||||
m.addArticle(item);
|
||||
}
|
||||
setCommon(m);
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
|
||||
|
||||
/**
|
||||
* 文本消息builder
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public final class TextBuilder extends BaseBuilder<TextBuilder, WxMpXmlOutTextMessage> {
|
||||
private String content;
|
||||
|
||||
public TextBuilder content(String content) {
|
||||
this.content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutTextMessage build() {
|
||||
WxMpXmlOutTextMessage m = new WxMpXmlOutTextMessage();
|
||||
setCommon(m);
|
||||
m.setContent(this.content);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.common.util.StringUtils;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTransferKefuMessage;
|
||||
|
||||
/**
|
||||
* 客服消息builder
|
||||
* <pre>
|
||||
* 用法: WxMpKefuMessage m = WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().content(...).toUser(...).build();
|
||||
* </pre>
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
public final class TransferCustomerServiceBuilder extends BaseBuilder<TransferCustomerServiceBuilder, WxMpXmlOutTransferKefuMessage> {
|
||||
private String kfAccount;
|
||||
|
||||
public TransferCustomerServiceBuilder kfAccount(String kf) {
|
||||
this.kfAccount = kf;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutTransferKefuMessage build() {
|
||||
WxMpXmlOutTransferKefuMessage m = new WxMpXmlOutTransferKefuMessage();
|
||||
setCommon(m);
|
||||
if(StringUtils.isNotBlank(this.kfAccount)){
|
||||
WxMpXmlOutTransferKefuMessage.TransInfo transInfo = new WxMpXmlOutTransferKefuMessage.TransInfo();
|
||||
transInfo.setKfAccount(this.kfAccount);
|
||||
m.setTransInfo(transInfo);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutVideoMessage;
|
||||
|
||||
/**
|
||||
* 视频消息builder
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public final class VideoBuilder extends BaseBuilder<VideoBuilder, WxMpXmlOutVideoMessage> {
|
||||
|
||||
private String mediaId;
|
||||
private String title;
|
||||
private String description;
|
||||
|
||||
public VideoBuilder title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
public VideoBuilder description(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
public VideoBuilder mediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutVideoMessage build() {
|
||||
WxMpXmlOutVideoMessage m = new WxMpXmlOutVideoMessage();
|
||||
setCommon(m);
|
||||
m.setTitle(this.title);
|
||||
m.setDescription(this.description);
|
||||
m.setMediaId(this.mediaId);
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package me.chanjar.weixin.mp.builder.outxml;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutVoiceMessage;
|
||||
|
||||
/**
|
||||
* 语音消息builder
|
||||
* @author chanjarster
|
||||
*/
|
||||
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder, WxMpXmlOutVoiceMessage> {
|
||||
|
||||
private String mediaId;
|
||||
|
||||
public VoiceBuilder mediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutVoiceMessage build() {
|
||||
WxMpXmlOutVoiceMessage m = new WxMpXmlOutVoiceMessage();
|
||||
setCommon(m);
|
||||
m.setMediaId(this.mediaId);
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user