diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java index b31228631..3bfd11753 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java @@ -23,14 +23,15 @@ public class WxConsts { /////////////////////// // 主动发送消息(即客服消息)的消息类型 /////////////////////// - public static final String CUSTOM_MSG_TEXT = "text"; - public static final String CUSTOM_MSG_IMAGE = "image"; - public static final String CUSTOM_MSG_VOICE = "voice"; - public static final String CUSTOM_MSG_VIDEO = "video"; - public static final String CUSTOM_MSG_MUSIC = "music"; - public static final String CUSTOM_MSG_NEWS = "news"; - public static final String CUSTOM_MSG_FILE = "file"; - public static final String CUSTOM_MSG_WXCARD = "wxcard"; + public static final String CUSTOM_MSG_TEXT = "text";//文本消息 + public static final String CUSTOM_MSG_IMAGE = "image";//图片消息 + public static final String CUSTOM_MSG_VOICE = "voice";//语音消息 + public static final String CUSTOM_MSG_VIDEO = "video";//视频消息 + public static final String CUSTOM_MSG_MUSIC = "music";//音乐消息 + public static final String CUSTOM_MSG_NEWS = "news";//图文消息(点击跳转到外链) + public static final String CUSTOM_MSG_MPNEWS = "mpnews";//图文消息(点击跳转到图文消息页面) + public static final String CUSTOM_MSG_FILE = "file";//发送文件(CP专用) + public static final String CUSTOM_MSG_WXCARD = "wxcard";//卡券消息 public static final String CUSTOM_MSG_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service"; public static final String CUSTOM_MSG_SAFE_NO = "0"; public static final String CUSTOM_MSG_SAFE_YES = "1"; @@ -193,7 +194,7 @@ public class WxConsts { * 弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息 */ public static final String OAUTH2_SCOPE_USER_INFO = "snsapi_userinfo"; - + /** * 网页应用登录授权作用域 snsapi_login */ diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMaterialNews.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMaterialNews.java index df40f6c3e..fc7c983c6 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMaterialNews.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMaterialNews.java @@ -1,17 +1,16 @@ package me.chanjar.weixin.mp.bean; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class WxMpMaterialNews implements Serializable { - - /** - * - */ private static final long serialVersionUID = -3283203652013494976L; + private List articles = new ArrayList<>(); public List getArticles() { @@ -81,7 +80,6 @@ public class WxMpMaterialNews implements Serializable { /** * 点击图文消息跳转链接 - * @return */ private String url; @@ -159,14 +157,12 @@ public class WxMpMaterialNews implements Serializable { @Override public String toString() { - return "WxMpMassNewsArticle [" + "thumbMediaId=" + this.thumbMediaId + "thumbUrl=" + this.thumbUrl + ", author=" + this.author + ", title=" + this.title + - ", contentSourceUrl=" + this.contentSourceUrl + ", content=" + this.content + ", digest=" + this.digest + - ", showCoverPic=" + this.showCoverPic +", url=" + this.url + "]"; + return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } @Override public String toString() { - return "WxMpMaterialNews [" + "articles=" + this.articles + "]"; + return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/custombuilder/MpNewsBuilder.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/custombuilder/MpNewsBuilder.java new file mode 100644 index 000000000..4ab90dce5 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/custombuilder/MpNewsBuilder.java @@ -0,0 +1,33 @@ +package me.chanjar.weixin.mp.bean.custombuilder; + +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; + +/** + * 图文消息builder + *
+ * 用法:
+ * WxMpKefuMessage m = WxMpKefuMessage.NEWS().mediaId("xxxxx").toUser(...).build();
+ * 
+ * @author Binary Wang + * + */ +public final class MpNewsBuilder extends BaseBuilder { + 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; + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/kefu/WxMpKefuMessage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/kefu/WxMpKefuMessage.java index 7963f53ef..36c2525cf 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/kefu/WxMpKefuMessage.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/kefu/WxMpKefuMessage.java @@ -1,16 +1,23 @@ package me.chanjar.weixin.mp.bean.kefu; -import me.chanjar.weixin.mp.bean.custombuilder.*; -import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; - import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import me.chanjar.weixin.mp.bean.custombuilder.ImageBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.MpNewsBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.MusicBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.NewsBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.TextBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.VideoBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.VoiceBuilder; +import me.chanjar.weixin.mp.bean.custombuilder.WxCardBuilder; +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; + /** * 客服消息 - * @author chanjarster * + * @author chanjarster */ public class WxMpKefuMessage implements Serializable { private static final long serialVersionUID = -9196732086954365246L; @@ -26,130 +33,9 @@ public class WxMpKefuMessage implements Serializable { private String hqMusicUrl; private String kfAccount; private String cardId; + private String mpNewsMediaId; private List articles = new ArrayList<>(); - public String getToUser() { - return this.toUser; - } - public void setToUser(String toUser) { - this.toUser = toUser; - } - public String getMsgType() { - return this.msgType; - } - - /** - *
-   * 请使用
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_TEXT}
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_IMAGE}
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VOICE}
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MUSIC}
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VIDEO}
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_NEWS}
-   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_WXCARD}
-   * 
- * @param msgType - */ - public void setMsgType(String msgType) { - this.msgType = msgType; - } - public String getContent() { - return this.content; - } - public void setContent(String content) { - this.content = content; - } - public String getMediaId() { - return this.mediaId; - } - public void setMediaId(String mediaId) { - this.mediaId = mediaId; - } - public String getThumbMediaId() { - return this.thumbMediaId; - } - public void setThumbMediaId(String thumbMediaId) { - this.thumbMediaId = thumbMediaId; - } - public String getTitle() { - return this.title; - } - public void setTitle(String title) { - this.title = title; - } - public String getDescription() { - return this.description; - } - public void setDescription(String description) { - this.description = description; - } - public String getMusicUrl() { - return this.musicUrl; - } - public void setMusicUrl(String musicUrl) { - this.musicUrl = musicUrl; - } - public String getHqMusicUrl() { - return this.hqMusicUrl; - } - public void setHqMusicUrl(String hqMusicUrl) { - this.hqMusicUrl = hqMusicUrl; - } - - public String getCardId() { - return this.cardId; - } - - public void setCardId(String cardId) { - this.cardId = cardId; - } - - public List getArticles() { - return this.articles; - } - public void setArticles(List articles) { - this.articles = articles; - } - - public String toJson() { - return WxMpGsonBuilder.INSTANCE.create().toJson(this); - } - - public static class WxArticle { - - private String title; - private String description; - private String url; - private String picUrl; - - public String getTitle() { - return this.title; - } - public void setTitle(String title) { - this.title = title; - } - public String getDescription() { - return this.description; - } - public void setDescription(String description) { - this.description = description; - } - public String getUrl() { - return this.url; - } - public void setUrl(String url) { - this.url = url; - } - public String getPicUrl() { - return this.picUrl; - } - public void setPicUrl(String picUrl) { - this.picUrl = picUrl; - } - - } - /** * 获得文本消息builder */ @@ -186,12 +72,18 @@ public class WxMpKefuMessage implements Serializable { } /** - * 获得图文消息builder + * 获得图文消息(点击跳转到外链)builder */ public static NewsBuilder NEWS() { return new NewsBuilder(); } + /** + * 获得图文消息(点击跳转到图文消息页面)builder + */ + public static MpNewsBuilder MPNEWS() { + return new MpNewsBuilder(); + } /** * 获得卡券消息builder @@ -200,6 +92,121 @@ public class WxMpKefuMessage implements Serializable { return new WxCardBuilder(); } + public String getToUser() { + return this.toUser; + } + + public void setToUser(String toUser) { + this.toUser = toUser; + } + + public String getMsgType() { + return this.msgType; + } + + public String getMpNewsMediaId() { + return this.mpNewsMediaId; + } + + public void setMpNewsMediaId(String mpNewsMediaId) { + this.mpNewsMediaId = mpNewsMediaId; + } + + /** + *
+   * 请使用
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_TEXT}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_IMAGE}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VOICE}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MUSIC}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VIDEO}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_NEWS}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MPNEWS}
+   * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_WXCARD}
+   * 
+ * + * @param msgType + */ + public void setMsgType(String msgType) { + this.msgType = msgType; + } + + public String getContent() { + return this.content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getMediaId() { + return this.mediaId; + } + + public void setMediaId(String mediaId) { + this.mediaId = mediaId; + } + + public String getThumbMediaId() { + return this.thumbMediaId; + } + + public void setThumbMediaId(String thumbMediaId) { + this.thumbMediaId = thumbMediaId; + } + + public String getTitle() { + return this.title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getMusicUrl() { + return this.musicUrl; + } + + public void setMusicUrl(String musicUrl) { + this.musicUrl = musicUrl; + } + + public String getHqMusicUrl() { + return this.hqMusicUrl; + } + + public void setHqMusicUrl(String hqMusicUrl) { + this.hqMusicUrl = hqMusicUrl; + } + + public String getCardId() { + return this.cardId; + } + + public void setCardId(String cardId) { + this.cardId = cardId; + } + + public List getArticles() { + return this.articles; + } + + public void setArticles(List articles) { + this.articles = articles; + } + + public String toJson() { + return WxMpGsonBuilder.INSTANCE.create().toJson(this); + } + public String getKfAccount() { return this.kfAccount; } @@ -207,4 +214,44 @@ public class WxMpKefuMessage implements Serializable { public void setKfAccount(String kfAccount) { this.kfAccount = kfAccount; } + + public static class WxArticle { + private String title; + private String description; + private String url; + private String picUrl; + + public String getTitle() { + return this.title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getUrl() { + return this.url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getPicUrl() { + return this.picUrl; + } + + public void setPicUrl(String picUrl) { + this.picUrl = picUrl; + } + + } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpKefuMessageGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpKefuMessageGsonAdapter.java index 5b310cb0f..abab87a11 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpKefuMessageGsonAdapter.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpKefuMessageGsonAdapter.java @@ -75,6 +75,12 @@ public class WxMpKefuMessageGsonAdapter implements JsonSerializer mediaIdsToDownload = new ArrayList<>(); + @Test(dataProvider="mediaFiles") public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { try(InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)){