From 566e5f31c7c74e202b47647c9aae1b3b51498dd9 Mon Sep 17 00:00:00 2001 From: pg Date: Mon, 21 Jun 2021 11:58:13 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#2155=20=E3=80=90=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E3=80=91=E5=8F=91=E9=80=81=E6=96=B0=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E6=AC=A2=E8=BF=8E=E8=AF=AD=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E8=A7=86=E9=A2=91=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8C=E5=90=8C=E6=97=B6=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cp/bean/external/WxCpWelcomeMsg.java | 12 +-- .../cp/bean/external/msg/Attachment.java | 76 +++++++++++++++++++ .../weixin/cp/bean/external/msg/Video.java | 19 +++++ .../weixin/cp/constant/WxCpConsts.java | 20 +++++ .../WxCpExternalContactServiceImplTest.java | 17 +++++ 5 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java index ce744b9f24..ade49684ce 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java @@ -2,13 +2,11 @@ import com.google.gson.annotations.SerializedName; import lombok.*; -import me.chanjar.weixin.cp.bean.external.msg.Image; -import me.chanjar.weixin.cp.bean.external.msg.Link; -import me.chanjar.weixin.cp.bean.external.msg.MiniProgram; -import me.chanjar.weixin.cp.bean.external.msg.Text; +import me.chanjar.weixin.cp.bean.external.msg.*; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; import java.io.Serializable; +import java.util.List; /** * 新客户欢迎语. @@ -28,11 +26,7 @@ public class WxCpWelcomeMsg implements Serializable { private Text text; - private Image image; - - private Link link; - - private MiniProgram miniprogram; + private List attachments; public String toJson() { return WxCpGsonBuilder.create().toJson(this); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java new file mode 100644 index 0000000000..0c64b9bf63 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java @@ -0,0 +1,76 @@ +package me.chanjar.weixin.cp.bean.external.msg; + +import com.google.gson.annotations.SerializedName; +import me.chanjar.weixin.cp.constant.WxCpConsts; + +import java.io.Serializable; + +public class Attachment implements Serializable { + private static final long serialVersionUID = -8078748379570640198L; + + @SerializedName("msgtype") + private String msgType; + + private Image image; + + private Link link; + + private MiniProgram miniprogram; + + private Video video; + + @Override + public String toString() { + return "Attachment{" + + "msgType='" + msgType + '\'' + + ", image=" + image + + ", link=" + link + + ", miniprogram=" + miniprogram + + ", video=" + video + + '}'; + } + + private String getMsgType() { + return msgType; + } + + private void setMsgType(String msgType) { + this.msgType = msgType; + } + + public Image getImage() { + return image; + } + + public void setImage(Image image) { + this.image = image; + this.msgType = WxCpConsts.WelcomeMsgType.IMAGE; + } + + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + this.msgType = WxCpConsts.WelcomeMsgType.LINK; + } + + public MiniProgram getMiniprogram() { + return miniprogram; + } + + public void setMiniprogram(MiniProgram miniprogram) { + this.miniprogram = miniprogram; + this.msgType = WxCpConsts.WelcomeMsgType.MINIPROGRAM; + } + + public Video getVideo() { + return video; + } + + public void setVideo(Video video) { + this.video = video; + this.msgType = WxCpConsts.WelcomeMsgType.VIDEO; + } +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java new file mode 100644 index 0000000000..237fb75cfe --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java @@ -0,0 +1,19 @@ +package me.chanjar.weixin.cp.bean.external.msg; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.io.Serializable; + +/** + * 视频消息 + * + * @author pg + * @date 2021-6-21 + */ +@Data +public class Video implements Serializable { + private static final long serialVersionUID = -6048642921382867138L; + @SerializedName("media_id") + private String mediaId; +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java index 4a41fa8f71..69db78efbe 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java @@ -334,4 +334,24 @@ public static class WorkBenchType { * */ public static final String WEBVIEW = "webview"; } + + @UtilityClass + public static class WelcomeMsgType { + /** + * 图片消息. + */ + public static final String IMAGE = "image"; + /** + * 图文消息. + */ + public static final String LINK = "link"; + /** + * 视频消息. + */ + public static final String VIDEO = "video"; + /** + * 小程序消息. + */ + public static final String MINIPROGRAM = "miniprogram"; + } } diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java index 29089d478d..4b6221d175 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java @@ -8,6 +8,9 @@ import me.chanjar.weixin.cp.bean.WxCpBaseResp; import me.chanjar.weixin.cp.bean.external.*; import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo; +import me.chanjar.weixin.cp.bean.external.msg.Attachment; +import me.chanjar.weixin.cp.bean.external.msg.Image; +import me.chanjar.weixin.cp.bean.external.msg.Video; import org.apache.commons.lang3.time.DateFormatUtils; import org.testng.annotations.Guice; import org.testng.annotations.Test; @@ -214,8 +217,22 @@ public void testAddMsgTemplate() { @Test public void testSendWelcomeMsg() throws WxErrorException { + Image image = new Image(); + image.setMediaId("123123"); + Attachment attachment = new Attachment(); + attachment.setImage(image); + + Video video = new Video(); + video.setMediaId("video_media_id"); + Attachment attachment2 = new Attachment(); + attachment2.setVideo(video); + + List attachments = new ArrayList<>(); + attachments.add(attachment); + attachments.add(attachment2); this.wxCpService.getExternalContactService().sendWelcomeMsg(WxCpWelcomeMsg.builder() .welcomeCode("abc") + .attachments(attachments) .build()); }