重构WxConsts类,对所有常量进行分类整理,便于阅读使用

This commit is contained in:
Binary Wang
2017-11-07 11:41:55 +08:00
parent f65e2fbdb8
commit 2146372502
62 changed files with 427 additions and 393 deletions

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.messagebuilder.*;
@@ -137,13 +138,13 @@ public class WxCpMessage implements Serializable {
/**
* <pre>
* 请使用
* {@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 WxConsts.KefuMsgType#TEXT}
* {@link WxConsts.KefuMsgType#IMAGE}
* {@link WxConsts.KefuMsgType#VOICE}
* {@link WxConsts.KefuMsgType#MUSIC}
* {@link WxConsts.KefuMsgType#VIDEO}
* {@link WxConsts.KefuMsgType#NEWS}
* {@link WxConsts.KefuMsgType#MPNEWS}
* </pre>
*
* @param msgType 消息类型

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.bean;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -224,13 +225,13 @@ public class WxCpXmlMessage implements Serializable {
/**
* <pre>
* 当接受用户消息时,可能会获得以下值:
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_TEXT}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_IMAGE}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_VOICE}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_VIDEO}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_LOCATION}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_LINK}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_EVENT}
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#LOCATION}
* {@link WxConsts.XmlMsgType#LINK}
* {@link WxConsts.XmlMsgType#EVENT}
* </pre>
*/
public String getMsgType() {
@@ -240,11 +241,11 @@ public class WxCpXmlMessage implements Serializable {
/**
* <pre>
* 当发送消息的时候使用:
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_TEXT}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_IMAGE}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_VOICE}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_VIDEO}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_NEWS}
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#NEWS}
* </pre>
*
* @param msgType

View File

@@ -14,7 +14,7 @@ public class WxCpXmlOutImageMessage extends WxCpXmlOutMessage {
private String mediaId;
public WxCpXmlOutImageMessage() {
this.msgType = WxConsts.XML_MSG_IMAGE;
this.msgType = WxConsts.XmlMsgType.IMAGE;
}
public String getMediaId() {

View File

@@ -18,7 +18,7 @@ public class WxCpXmlOutNewsMessage extends WxCpXmlOutMessage {
protected int articleCount;
public WxCpXmlOutNewsMessage() {
this.msgType = WxConsts.XML_MSG_NEWS;
this.msgType = WxConsts.XmlMsgType.NEWS;
}
public int getArticleCount() {

View File

@@ -14,7 +14,7 @@ public class WxCpXmlOutTextMessage extends WxCpXmlOutMessage {
private String content;
public WxCpXmlOutTextMessage() {
this.msgType = WxConsts.XML_MSG_TEXT;
this.msgType = WxConsts.XmlMsgType.TEXT;
}
public String getContent() {

View File

@@ -13,7 +13,7 @@ public class WxCpXmlOutVideoMessage extends WxCpXmlOutMessage {
protected final Video video = new Video();
public WxCpXmlOutVideoMessage() {
this.msgType = WxConsts.XML_MSG_VIDEO;
this.msgType = WxConsts.XmlMsgType.VIDEO;
}
public String getMediaId() {

View File

@@ -14,7 +14,7 @@ public class WxCpXmlOutVoiceMessage extends WxCpXmlOutMessage {
private String mediaId;
public WxCpXmlOutVoiceMessage() {
this.msgType = WxConsts.XML_MSG_VOICE;
this.msgType = WxConsts.XmlMsgType.VOICE;
}
public String getMediaId() {

View File

@@ -44,7 +44,7 @@ public class BaseBuilder<T> {
m.setToUser(this.toUser);
m.setToParty(this.toParty);
m.setToTag(this.toTag);
m.setSafe(StringUtils.defaultIfBlank(this.safe, WxConsts.CUSTOM_MSG_SAFE_NO));
m.setSafe(StringUtils.defaultIfBlank(this.safe, WxConsts.KefuMsgSafe.NO));
return m;
}

View File

@@ -15,7 +15,7 @@ public final class FileBuilder extends BaseBuilder<FileBuilder> {
private String mediaId;
public FileBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_FILE;
this.msgType = WxConsts.KefuMsgType.FILE;
}
public FileBuilder mediaId(String media_id) {

View File

@@ -15,7 +15,7 @@ public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
private String mediaId;
public ImageBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_IMAGE;
this.msgType = WxConsts.KefuMsgType.IMAGE;
}
public ImageBuilder mediaId(String media_id) {

View File

@@ -23,7 +23,7 @@ public final class MpnewsBuilder extends BaseBuilder<MpnewsBuilder> {
private String mediaId;
public MpnewsBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_MPNEWS;
this.msgType = WxConsts.KefuMsgType.MPNEWS;
}
public MpnewsBuilder mediaId(String mediaId) {

View File

@@ -22,7 +22,7 @@ public final class NewsBuilder extends BaseBuilder<NewsBuilder> {
private List<NewArticle> articles = new ArrayList<>();
public NewsBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_NEWS;
this.msgType = WxConsts.KefuMsgType.NEWS;
}
public NewsBuilder addArticle(NewArticle... articles) {

View File

@@ -15,7 +15,7 @@ public final class TextBuilder extends BaseBuilder<TextBuilder> {
private String content;
public TextBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_TEXT;
this.msgType = WxConsts.KefuMsgType.TEXT;
}
public TextBuilder content(String content) {

View File

@@ -18,7 +18,7 @@ public class TextCardBuilder extends BaseBuilder<TextCardBuilder> {
private String url;
public TextCardBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_TEXTCARD;
this.msgType = WxConsts.KefuMsgType.TEXTCARD;
}
public TextCardBuilder title(String title) {

View File

@@ -24,7 +24,7 @@ public final class VideoBuilder extends BaseBuilder<VideoBuilder> {
private String thumbMediaId;
public VideoBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_VIDEO;
this.msgType = WxConsts.KefuMsgType.VIDEO;
}
public VideoBuilder mediaId(String mediaId) {

View File

@@ -15,7 +15,7 @@ public final class VoiceBuilder extends BaseBuilder<VoiceBuilder> {
private String mediaId;
public VoiceBuilder() {
this.msgType = WxConsts.CUSTOM_MSG_VOICE;
this.msgType = WxConsts.KefuMsgType.VOICE;
}
public VoiceBuilder mediaId(String media_id) {

View File

@@ -37,13 +37,13 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
if (StringUtils.isNotBlank(message.getToTag())) {
messageJson.addProperty("totag", message.getToTag());
}
if (WxConsts.CUSTOM_MSG_TEXT.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.TEXT.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
messageJson.add("text", text);
}
if (WxConsts.CUSTOM_MSG_TEXTCARD.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.TEXTCARD.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("title", message.getTitle());
text.addProperty("description", message.getDescription());
@@ -51,19 +51,19 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.add("textcard", text);
}
if (WxConsts.CUSTOM_MSG_IMAGE.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.IMAGE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("image", image);
}
if (WxConsts.CUSTOM_MSG_FILE.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.FILE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("file", image);
}
if (WxConsts.CUSTOM_MSG_VOICE.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.VOICE.equals(message.getMsgType())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", message.getMediaId());
messageJson.add("voice", voice);
@@ -73,7 +73,7 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.addProperty("safe", message.getSafe());
}
if (WxConsts.CUSTOM_MSG_VIDEO.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.VIDEO.equals(message.getMsgType())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", message.getMediaId());
video.addProperty("thumb_media_id", message.getThumbMediaId());
@@ -82,7 +82,7 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.add("video", video);
}
if (WxConsts.CUSTOM_MSG_NEWS.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.NEWS.equals(message.getMsgType())) {
JsonObject newsJsonObject = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (NewArticle article : message.getArticles()) {
@@ -97,7 +97,7 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.add("news", newsJsonObject);
}
if (WxConsts.CUSTOM_MSG_MPNEWS.equals(message.getMsgType())) {
if (WxConsts.KefuMsgType.MPNEWS.equals(message.getMsgType())) {
JsonObject newsJsonObject = new JsonObject();
if (message.getMediaId() != null) {
newsJsonObject.addProperty("media_id", message.getMediaId());

View File

@@ -3,7 +3,6 @@ package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import org.testng.annotations.*;
@@ -32,7 +31,7 @@ public class WxCpMessageAPITest {
public void testSendMessage() throws WxErrorException {
WxCpMessage message = new WxCpMessage();
// message.setAgentId(configStorage.getAgentId());
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
message.setMsgType(WxConsts.KefuMsgType.TEXT);
message.setToUser(configStorage.getUserId());
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");

View File

@@ -27,21 +27,21 @@ public class WxCpMessageRouterTest {
router
.rule()
.async(async)
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK).eventKey("KEY_1").content("CONTENT_1")
.msgType(WxConsts.XmlMsgType.TEXT).event(WxConsts.EventType.CLICK).eventKey("KEY_1").content("CONTENT_1")
.handler(new WxEchoCpMessageHandler(sb, "COMBINE_4"))
.end()
.rule()
.async(async)
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK).eventKey("KEY_1")
.msgType(WxConsts.XmlMsgType.TEXT).event(WxConsts.EventType.CLICK).eventKey("KEY_1")
.handler(new WxEchoCpMessageHandler(sb, "COMBINE_3"))
.end()
.rule()
.async(async)
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK)
.msgType(WxConsts.XmlMsgType.TEXT).event(WxConsts.EventType.CLICK)
.handler(new WxEchoCpMessageHandler(sb, "COMBINE_2"))
.end()
.rule().async(async).msgType(WxConsts.XML_MSG_TEXT).handler(new WxEchoCpMessageHandler(sb, WxConsts.XML_MSG_TEXT)).end()
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoCpMessageHandler(sb, WxConsts.EVT_CLICK)).end()
.rule().async(async).msgType(WxConsts.XmlMsgType.TEXT).handler(new WxEchoCpMessageHandler(sb, WxConsts.XmlMsgType.TEXT)).end()
.rule().async(async).event(WxConsts.EventType.CLICK).handler(new WxEchoCpMessageHandler(sb, WxConsts.EventType.CLICK)).end()
.rule().async(async).eventKey("KEY_1").handler(new WxEchoCpMessageHandler(sb, "KEY_1")).end()
.rule().async(async).content("CONTENT_1").handler(new WxEchoCpMessageHandler(sb, "CONTENT_1")).end()
.rule().async(async).rContent(".*bc.*").handler(new WxEchoCpMessageHandler(sb, "abcd")).end()
@@ -104,10 +104,10 @@ public class WxCpMessageRouterTest {
@DataProvider(name = "messages-1")
public Object[][] messages2() {
WxCpXmlMessage message1 = new WxCpXmlMessage();
message1.setMsgType(WxConsts.XML_MSG_TEXT);
message1.setMsgType(WxConsts.XmlMsgType.TEXT);
WxCpXmlMessage message2 = new WxCpXmlMessage();
message2.setEvent(WxConsts.EVT_CLICK);
message2.setEvent(WxConsts.EventType.CLICK);
WxCpXmlMessage message3 = new WxCpXmlMessage();
message3.setEventKey("KEY_1");
@@ -125,24 +125,24 @@ public class WxCpMessageRouterTest {
message7.setFormat("strangeformat");
WxCpXmlMessage c2 = new WxCpXmlMessage();
c2.setMsgType(WxConsts.XML_MSG_TEXT);
c2.setEvent(WxConsts.EVT_CLICK);
c2.setMsgType(WxConsts.XmlMsgType.TEXT);
c2.setEvent(WxConsts.EventType.CLICK);
WxCpXmlMessage c3 = new WxCpXmlMessage();
c3.setMsgType(WxConsts.XML_MSG_TEXT);
c3.setEvent(WxConsts.EVT_CLICK);
c3.setMsgType(WxConsts.XmlMsgType.TEXT);
c3.setEvent(WxConsts.EventType.CLICK);
c3.setEventKey("KEY_1");
WxCpXmlMessage c4 = new WxCpXmlMessage();
c4.setMsgType(WxConsts.XML_MSG_TEXT);
c4.setEvent(WxConsts.EVT_CLICK);
c4.setMsgType(WxConsts.XmlMsgType.TEXT);
c4.setEvent(WxConsts.EventType.CLICK);
c4.setEventKey("KEY_1");
c4.setContent("CONTENT_1");
return new Object[][]{
new Object[]{message1, WxConsts.XML_MSG_TEXT + ","},
new Object[]{message2, WxConsts.EVT_CLICK + ","},
new Object[]{message1, WxConsts.XmlMsgType.TEXT + ","},
new Object[]{message2, WxConsts.EventType.CLICK + ","},
new Object[]{message3, "KEY_1,"},
new Object[]{message4, "CONTENT_1,"},
new Object[]{message5, "ALL,"},

View File

@@ -34,11 +34,11 @@ public class WxCpMediaServiceImplTest {
@DataProvider
public Object[][] mediaData() {
return new Object[][]{
new Object[]{WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_MP3, "mm.mp3"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_AMR, "mm.amr"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
new Object[]{WxConsts.MEDIA_VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MEDIA_FILE, TestConstants.FILE_JPG, "mm.jpeg"}
new Object[]{WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_AMR, "mm.amr"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
new Object[]{WxConsts.MediaFileType.VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MediaFileType.FILE, TestConstants.FILE_JPG, "mm.jpeg"}
};
}

View File

@@ -26,12 +26,12 @@ public class WxCpMenuServiceImplTest {
public Object[][] menuData() {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxConsts.BUTTON_CLICK);
button1.setType(WxConsts.MenuButtonType.CLICK);
button1.setName("今日歌曲");
button1.setKey("V1001_TODAY_MUSIC");
WxMenuButton button2 = new WxMenuButton();
button2.setType(WxConsts.BUTTON_CLICK);
button2.setType(WxConsts.MenuButtonType.CLICK);
button2.setName("歌手简介");
button2.setKey("V1001_TODAY_SINGER");
@@ -43,17 +43,17 @@ public class WxCpMenuServiceImplTest {
menu.getButtons().add(button3);
WxMenuButton button31 = new WxMenuButton();
button31.setType(WxConsts.BUTTON_VIEW);
button31.setType(WxConsts.MenuButtonType.VIEW);
button31.setName("搜索");
button31.setUrl("http://www.soso.com/");
WxMenuButton button32 = new WxMenuButton();
button32.setType(WxConsts.BUTTON_VIEW);
button32.setType(WxConsts.MenuButtonType.VIEW);
button32.setName("视频");
button32.setUrl("http://v.qq.com/");
WxMenuButton button33 = new WxMenuButton();
button33.setType(WxConsts.BUTTON_CLICK);
button33.setType(WxConsts.MenuButtonType.CLICK);
button33.setName("赞一下我们");
button33.setKey("V1001_GOOD");

View File

@@ -58,7 +58,7 @@ public class WxCpXmlMessageTest {
assertEquals(wxMessage.getToUserName(), "toUser");
assertEquals(wxMessage.getFromUserName(), "fromUser");
assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
assertEquals(wxMessage.getMsgType(), WxConsts.XmlMsgType.TEXT);
assertEquals(wxMessage.getContent(), "this is a test");
assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
assertEquals(wxMessage.getPicUrl(), "this is a url");
@@ -108,7 +108,7 @@ public class WxCpXmlMessageTest {
assertEquals(wxMessage.getToUserName(), "wx45a0972125658be9");
assertEquals(wxMessage.getFromUserName(), "xiaohe");
assertEquals(wxMessage.getCreateTime(), new Long(1502012364L));
assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_EVENT);
assertEquals(wxMessage.getMsgType(), WxConsts.XmlMsgType.EVENT);
assertEquals(wxMessage.getAgentId(), Integer.valueOf(1000004));
assertEquals(wxMessage.getEvent(), "pic_weixin");
assertEquals(wxMessage.getEventKey(), "faceSimilarity");