diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java index b38c6b868c..952391cf90 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java @@ -28,12 +28,12 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic @Override public WxCpContactWayResult addContactWay(@NonNull WxCpContactWayInfo info) throws WxErrorException { - if (info.getUsers() != null && info.getUsers().size() > 100) { + if (info.getContactWay().getUsers() != null && info.getContactWay().getUsers().size() > 100) { throw new RuntimeException("「联系我」使用人数默认限制不超过100人(包括部门展开后的人数)"); } final String url = this.mainService.getWxCpConfigStorage().getApiUrl(ADD_CONTACT_WAY); - String responseContent = this.mainService.post(url, info.toJson()); + String responseContent = this.mainService.post(url, info.getContactWay().toJson()); return WxCpContactWayResult.fromJson(responseContent); } @@ -45,21 +45,20 @@ public WxCpContactWayInfo getContactWay(@NonNull String configId) throws WxError final String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_CONTACT_WAY); String responseContent = this.mainService.post(url, json.toString()); - return WxCpContactWayInfo.fromJson(responseContent); } @Override public WxCpBaseResp updateContactWay(@NonNull WxCpContactWayInfo info) throws WxErrorException { - if (StringUtils.isBlank(info.getConfigId())) { + if (StringUtils.isBlank(info.getContactWay().getConfigId())) { throw new RuntimeException("更新「联系我」方式需要指定configId"); } - if (info.getUsers() != null && info.getUsers().size() > 100) { + if (info.getContactWay().getUsers() != null && info.getContactWay().getUsers().size() > 100) { throw new RuntimeException("「联系我」使用人数默认限制不超过100人(包括部门展开后的人数)"); } final String url = this.mainService.getWxCpConfigStorage().getApiUrl(UPDATE_CONTACT_WAY); - String responseContent = this.mainService.post(url, info.toJson()); + String responseContent = this.mainService.post(url, info.getContactWay().toJson()); return WxCpBaseResp.fromJson(responseContent); } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpContactWayInfo.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpContactWayInfo.java index 3f62e4f0e4..8ef22a587b 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpContactWayInfo.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpContactWayInfo.java @@ -3,7 +3,9 @@ import com.google.gson.annotations.JsonAdapter; import com.google.gson.annotations.SerializedName; import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import me.chanjar.weixin.cp.util.json.WxCpConclusionAdapter; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; @@ -18,124 +20,168 @@ @NoArgsConstructor public class WxCpContactWayInfo { - /** - * 联系方式的配置id - */ - @SerializedName("config_id") - private String configId; - - /** - *
-   * 必填
-   * 联系方式类型,1-单人, 2-多人
-   * 
- */ - private TYPE type; - - /** - *
-   * 必填
-   * 场景,1-在小程序中联系,2-通过二维码联系
-   * 
- */ - private SCENE scene; - - /** - *
-   * 非必填
-   * 在小程序中联系时使用的控件样式
-   * 单人样式(type=1)时可选1,2,3
-   * 多人样式(type=2)时可选1,2
-   * 
- */ - private Integer style; - - /** - *
-   * 非必填
-   * 联系方式的备注信息,用于助记,不超过30个字符
-   * 
- */ - private String remark; - - /** - *
-   * 非必填
-   * 外部客户添加时是否无需验证,默认为true
-   * 
- */ - @SerializedName("skip_verify") - private Boolean skipVerify = Boolean.TRUE; - - /** - *
-   * 非必填
-   * 企业自定义的state参数,用于区分不同的添加渠道,在调用“获取外部联系人详情(WxCpExternalContactService.getContactDetail)”  时会返回该参数值,不超过30个字符
-   * 
- */ - private String state; - - /** - *
-   * 使用该联系方式的用户userID列表,在type为1时为必填,且只能有一个
-   * 
- */ - @SerializedName("user") - private List users; - - - /** - *
-   * 非必填
-   * 使用该联系方式的部门id列表,只在type为2时有效
-   * 
- */ - @SerializedName("party") - private List partys; - - /** - *
-   * 非必填
-   * 是否临时会话模式,true表示使用临时会话模式,默认为false
-   * 
- */ - @SerializedName("is_temp") - private Boolean isTemp = Boolean.FALSE; - - /** - *
-   * 非必填
-   * 临时会话二维码有效期,以秒为单位。该参数仅在is_temp为true时有效,默认7天
-   * 
- */ - @SerializedName("expires_in") - private Integer expiresIn; - - /** - *
-   * 非必填
-   * 临时会话有效期,以秒为单位。该参数仅在is_temp为true时有效,默认为添加好友后24小时
-   * 
- */ - @SerializedName("chat_expires_in") - private Integer chatExpiresIn; - - /** - *
-   * 非必填
-   * 可进行临时会话的客户unionid,该参数仅在is_temp为true时有效,如不指定则不进行限制
-   * 
- */ - @SerializedName("unionid") - private String unionId; - - /** - *
-   * 非必填
-   * 结束语,会话结束时自动发送给客户,可参考“结束语定义”,仅在is_temp为true时有效
-   * 
- */ - private Conclusion conclusions; + @SerializedName("contact_way") + private ContactWay contactWay; + + @Getter + @Setter + public static class ContactWay { + /** + * 联系方式的配置id + */ + @SerializedName("config_id") + private String configId; + + /** + *
+     * 必填
+     * 联系方式类型,1-单人, 2-多人
+     * 
+ */ + private TYPE type; + + /** + *
+     * 必填
+     * 场景,1-在小程序中联系,2-通过二维码联系
+     * 
+ */ + private SCENE scene; + + /** + *
+     * 非必填
+     * 在小程序中联系时使用的控件样式
+     * 单人样式(type=1)时可选1,2,3
+     * 多人样式(type=2)时可选1,2
+     * 
+ */ + private Integer style; + + /** + *
+     * 非必填
+     * 联系方式的备注信息,用于助记,不超过30个字符
+     * 
+ */ + private String remark; + + /** + *
+     * 非必填
+     * 外部客户添加时是否无需验证,默认为true
+     * 
+ */ + @SerializedName("skip_verify") + private Boolean skipVerify = Boolean.TRUE; + + /** + *
+     * 非必填
+     * 企业自定义的state参数,用于区分不同的添加渠道,在调用“获取外部联系人详情(WxCpExternalContactService.getContactDetail)”  时会返回该参数值,不超过30个字符
+     * 
+ */ + private String state; + + /** + *
+     * 联系二维码的URL,仅在scene为2时返回
+     * 
+ */ + @SerializedName("qr_code") + private String qrCode; + + /** + *
+     * 使用该联系方式的用户userID列表,在type为1时为必填,且只能有一个
+     * 
+ */ + @SerializedName("user") + private List users; + + + /** + *
+     * 非必填
+     * 使用该联系方式的部门id列表,只在type为2时有效
+     * 
+ */ + @SerializedName("party") + private List partys; + + /** + *
+     * 非必填
+     * 是否临时会话模式,true表示使用临时会话模式,默认为false
+     * 
+ */ + @SerializedName("is_temp") + private Boolean isTemp = Boolean.FALSE; + + /** + *
+     * 非必填
+     * 临时会话二维码有效期,以秒为单位。该参数仅在is_temp为true时有效,默认7天
+     * 
+ */ + @SerializedName("expires_in") + private Integer expiresIn; + + /** + *
+     * 非必填
+     * 临时会话有效期,以秒为单位。该参数仅在is_temp为true时有效,默认为添加好友后24小时
+     * 
+ */ + @SerializedName("chat_expires_in") + private Integer chatExpiresIn; + + /** + *
+     * 非必填
+     * 可进行临时会话的客户unionid,该参数仅在is_temp为true时有效,如不指定则不进行限制
+     * 
+ */ + @SerializedName("unionid") + private String unionId; + + /** + *
+     * 非必填
+     * 结束语,会话结束时自动发送给客户,可参考“结束语定义”,仅在is_temp为true时有效
+     * 
+ */ + private Conclusion conclusions; + + public static WxCpContactWayInfo.ContactWay fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpContactWayInfo.ContactWay.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + + /** + * 结束语定义 + */ + @Data + @JsonAdapter(WxCpConclusionAdapter.class) + public static class Conclusion { + private String textContent; + private String imgMediaId; + private String imgPicUrl; + private String linkTitle; + private String linkPicUrl; + private String linkDesc; + private String linkUrl; + private String miniProgramTitle; + private String miniProgramPicMediaId; + private String miniProgramAppId; + private String miniProgramPage; + } + + } + public static WxCpContactWayInfo fromJson(String json) { return WxCpGsonBuilder.create().fromJson(json, WxCpContactWayInfo.class); @@ -145,25 +191,6 @@ public String toJson() { return WxCpGsonBuilder.create().toJson(this); } - /** - * 结束语定义 - */ - @Data - @JsonAdapter(WxCpConclusionAdapter.class) - public static class Conclusion { - private String textContent; - private String imgMediaId; - private String imgPicUrl; - private String linkTitle; - private String linkPicUrl; - private String linkDesc; - private String linkUrl; - private String miniProgramTitle; - private String miniProgramPicMediaId; - private String miniProgramAppId; - private String miniProgramPage; - } - public enum TYPE { /** * 单人 diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpConclusionAdapter.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpConclusionAdapter.java index 1a9aab8bf9..2d7e27da9d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpConclusionAdapter.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpConclusionAdapter.java @@ -11,12 +11,12 @@ * * @author element */ -public class WxCpConclusionAdapter implements JsonSerializer, JsonDeserializer { +public class WxCpConclusionAdapter implements JsonSerializer, JsonDeserializer { @Override - public WxCpContactWayInfo.Conclusion deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + public WxCpContactWayInfo.ContactWay.Conclusion deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); - WxCpContactWayInfo.Conclusion conclusion = new WxCpContactWayInfo.Conclusion(); + WxCpContactWayInfo.ContactWay.Conclusion conclusion = new WxCpContactWayInfo.ContactWay.Conclusion(); if (jsonObject.get("text") != null) { JsonObject jsonText = jsonObject.get("text").getAsJsonObject(); @@ -77,7 +77,7 @@ public WxCpContactWayInfo.Conclusion deserialize(JsonElement json, Type typeOfT, } @Override - public JsonElement serialize(WxCpContactWayInfo.Conclusion src, Type typeOfSrc, JsonSerializationContext context) { + public JsonElement serialize(WxCpContactWayInfo.ContactWay.Conclusion src, Type typeOfSrc, JsonSerializationContext context) { JsonObject json = new JsonObject(); if (StringUtils.isNotBlank(src.getTextContent())) { JsonObject jsonText = new JsonObject(); 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 df086037a5..8a5a76496b 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 @@ -35,21 +35,24 @@ public void testGetExternalContact() throws WxErrorException { @Test public void testAddContactWay() throws WxErrorException { - final String concatUserId = "符合要求的userId"; + final String concatUserId = "HuangXiaoMing"; + + WxCpContactWayInfo.ContactWay wayInfo = new WxCpContactWayInfo.ContactWay(); + wayInfo.setType(WxCpContactWayInfo.TYPE.SIGLE); + wayInfo.setScene(WxCpContactWayInfo.SCENE.QRCODE); + wayInfo.setUsers(Lists.newArrayList(concatUserId)); + wayInfo.setRemark("CreateDate:" + DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(new Date())); WxCpContactWayInfo info = new WxCpContactWayInfo(); - info.setType(WxCpContactWayInfo.TYPE.SIGLE); - info.setScene(WxCpContactWayInfo.SCENE.MINIPROGRAM); - info.setUsers(Lists.newArrayList(concatUserId)); - info.setRemark("CreateDate:" + DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(new Date())); + info.setContactWay(wayInfo); this.wxCpService.getExternalContactService().addContactWay(info); } @Test public void testGetContactWay() throws WxErrorException { - final String configId = "2d7a68c657663afbd1d90db19a4b5ee9"; + final String configId = "39fea3d93e30faaa8c7a9edd4cfe4d08"; WxCpContactWayInfo contactWayInfo = this.wxCpService.getExternalContactService().getContactWay(configId); - System.out.println(contactWayInfo); + System.out.println(contactWayInfo.toJson()); assertNotNull(contactWayInfo); } @@ -57,10 +60,12 @@ public void testGetContactWay() throws WxErrorException { public void testUpdateContactWay() throws WxErrorException { final String configId = "2d7a68c657663afbd1d90db19a4b5ee9"; final String concatUserId = "符合要求的userId"; + WxCpContactWayInfo.ContactWay wayInfo = new WxCpContactWayInfo.ContactWay(); + wayInfo.setConfigId(configId); + wayInfo.setUsers(Lists.newArrayList(concatUserId)); + wayInfo.setRemark("CreateDate:" + DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(new Date())); WxCpContactWayInfo info = new WxCpContactWayInfo(); - info.setConfigId(configId); - info.setUsers(Lists.newArrayList(concatUserId)); - info.setRemark("CreateDate:" + DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(new Date())); + info.setContactWay(wayInfo); WxCpBaseResp resp = this.wxCpService.getExternalContactService().updateContactWay(info); System.out.println(resp); assertNotNull(resp);