@@ -1,8 +1,9 @@ | |||
package cn.binarywang.wx.miniapp.bean; | |||
import cn.binarywang.wx.miniapp.builder.ImageBuilder; | |||
import cn.binarywang.wx.miniapp.builder.LinkBuilder; | |||
import cn.binarywang.wx.miniapp.builder.TextBuilder; | |||
import cn.binarywang.wx.miniapp.builder.ImageMessageBuilder; | |||
import cn.binarywang.wx.miniapp.builder.LinkMessageBuilder; | |||
import cn.binarywang.wx.miniapp.builder.MaPageMessageBuilder; | |||
import cn.binarywang.wx.miniapp.builder.TextMessageBuilder; | |||
import com.google.gson.GsonBuilder; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.AllArgsConstructor; | |||
@@ -77,22 +78,29 @@ public class WxMaKefuMessage implements Serializable { | |||
/** | |||
* 获得文本消息builder. | |||
*/ | |||
public static TextBuilder newTextBuilder() { | |||
return new TextBuilder(); | |||
public static TextMessageBuilder newTextBuilder() { | |||
return new TextMessageBuilder(); | |||
} | |||
/** | |||
* 获得图片消息builder. | |||
*/ | |||
public static ImageBuilder newImageBuilder() { | |||
return new ImageBuilder(); | |||
public static ImageMessageBuilder newImageBuilder() { | |||
return new ImageMessageBuilder(); | |||
} | |||
/** | |||
* 获得图文链接消息builder. | |||
*/ | |||
public static LinkBuilder newLinkBuilder() { | |||
return new LinkBuilder(); | |||
public static LinkMessageBuilder newLinkBuilder() { | |||
return new LinkMessageBuilder(); | |||
} | |||
/** | |||
* 获得图文链接消息builder. | |||
*/ | |||
public static MaPageMessageBuilder newMaPageBuilder() { | |||
return new MaPageMessageBuilder(); | |||
} | |||
public String toJson() { | |||
@@ -1,21 +1,22 @@ | |||
package cn.binarywang.wx.miniapp.builder; | |||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | |||
import cn.binarywang.wx.miniapp.constant.WxMaConstants; | |||
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.KefuMsgType; | |||
/** | |||
* 图片消息builder. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
*/ | |||
public final class ImageBuilder extends BaseBuilder<ImageBuilder> { | |||
public final class ImageMessageBuilder extends BaseBuilder<ImageMessageBuilder> { | |||
private String mediaId; | |||
public ImageBuilder() { | |||
this.msgType = WxMaConstants.KefuMsgType.IMAGE; | |||
public ImageMessageBuilder() { | |||
this.msgType = KefuMsgType.IMAGE; | |||
} | |||
public ImageBuilder mediaId(String mediaId) { | |||
public ImageMessageBuilder mediaId(String mediaId) { | |||
this.mediaId = mediaId; | |||
return this; | |||
} |
@@ -1,39 +1,40 @@ | |||
package cn.binarywang.wx.miniapp.builder; | |||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | |||
import cn.binarywang.wx.miniapp.constant.WxMaConstants; | |||
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.KefuMsgType; | |||
/** | |||
* 图文链接builder | |||
* 图文链接消息builder | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
*/ | |||
public class LinkBuilder extends BaseBuilder<LinkBuilder> { | |||
public class LinkMessageBuilder extends BaseBuilder<LinkMessageBuilder> { | |||
private String title; | |||
private String description; | |||
private String url; | |||
private String thumbUrl; | |||
public LinkBuilder() { | |||
this.msgType = WxMaConstants.KefuMsgType.IMAGE; | |||
public LinkMessageBuilder() { | |||
this.msgType = KefuMsgType.LINK; | |||
} | |||
public LinkBuilder title(String title) { | |||
public LinkMessageBuilder title(String title) { | |||
this.title = title; | |||
return this; | |||
} | |||
public LinkBuilder description(String description) { | |||
public LinkMessageBuilder description(String description) { | |||
this.description = description; | |||
return this; | |||
} | |||
public LinkBuilder url(String url) { | |||
public LinkMessageBuilder url(String url) { | |||
this.url = url; | |||
return this; | |||
} | |||
public LinkBuilder thumbUrl(String thumbUrl) { | |||
public LinkMessageBuilder thumbUrl(String thumbUrl) { | |||
this.thumbUrl = thumbUrl; | |||
return this; | |||
} |
@@ -0,0 +1,47 @@ | |||
package cn.binarywang.wx.miniapp.builder; | |||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | |||
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.KefuMsgType; | |||
/** | |||
* 小程序卡片消息builder | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
*/ | |||
public class MaPageMessageBuilder extends BaseBuilder<MaPageMessageBuilder> { | |||
private String title; | |||
private String pagePath; | |||
private String thumbMediaId; | |||
public MaPageMessageBuilder() { | |||
this.msgType = KefuMsgType.MA_PAGE; | |||
} | |||
public MaPageMessageBuilder title(String title) { | |||
this.title = title; | |||
return this; | |||
} | |||
public MaPageMessageBuilder pagePath(String pagePath) { | |||
this.pagePath = pagePath; | |||
return this; | |||
} | |||
public MaPageMessageBuilder thumbMediaId(String thumbMediaId) { | |||
this.thumbMediaId = thumbMediaId; | |||
return this; | |||
} | |||
@Override | |||
public WxMaKefuMessage build() { | |||
WxMaKefuMessage m = super.build(); | |||
m.setMaPage(WxMaKefuMessage.KfMaPage.builder() | |||
.title(this.title) | |||
.pagePath(this.pagePath) | |||
.thumbMediaId(this.thumbMediaId) | |||
.build() | |||
); | |||
return m; | |||
} | |||
} |
@@ -1,21 +1,22 @@ | |||
package cn.binarywang.wx.miniapp.builder; | |||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | |||
import cn.binarywang.wx.miniapp.constant.WxMaConstants; | |||
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.KefuMsgType; | |||
/** | |||
* 文本消息builder. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
*/ | |||
public final class TextBuilder extends BaseBuilder<TextBuilder> { | |||
public final class TextMessageBuilder extends BaseBuilder<TextMessageBuilder> { | |||
private String content; | |||
public TextBuilder() { | |||
this.msgType = WxMaConstants.KefuMsgType.TEXT; | |||
public TextMessageBuilder() { | |||
this.msgType = KefuMsgType.TEXT; | |||
} | |||
public TextBuilder content(String content) { | |||
public TextMessageBuilder content(String content) { | |||
this.content = content; | |||
return this; | |||
} |
@@ -37,9 +37,21 @@ public class WxMaKefuMessageTest { | |||
.thumbUrl("thumbUrl") | |||
.build(); | |||
assertThat(reply.toJson()) | |||
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"image\"," + | |||
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"link\"," + | |||
"\"link\":{\"title\":\"title\",\"description\":\"description\",\"url\":\"url\",\"thumb_url\":\"thumbUrl\"}}"); | |||
} | |||
public void testMaPageBuilder() { | |||
WxMaKefuMessage reply = WxMaKefuMessage.newMaPageBuilder() | |||
.toUser("OPENID") | |||
.title("title") | |||
.pagePath("pagePath") | |||
.thumbMediaId("thumbMediaId") | |||
.build(); | |||
assertThat(reply.toJson()) | |||
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"miniprogrampage\"," + | |||
"\"miniprogrampage\":{\"title\":\"title\",\"pagepath\":\"pagePath\",\"thumb_media_id\":\"thumbMediaId\"}}"); | |||
} | |||
} |