From eaad6367de1ee3aba5f1ca7de67da88dd718eb03 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 2 Jul 2017 18:23:21 +0800 Subject: [PATCH] =?UTF-8?q?#252=20=E5=8E=9F=E6=9C=89=E5=9B=BE=E6=96=87?= =?UTF-8?q?=E7=B4=A0=E6=9D=90=E7=AE=A1=E7=90=86=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=95=99=E8=A8=80=E7=AE=A1=E7=90=86=E6=89=80=E9=9C=80?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E5=8F=82=E6=95=B0=EF=BC=9Aneed=5Fopen=5Fcomm?= =?UTF-8?q?ent=20=E5=92=8C=20only=5Ffans=5Fcan=5Fcomment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mp/api/impl/WxMpMaterialServiceImpl.java | 16 +++--- .../mp/bean/material/WxMpMaterialNews.java | 52 ++++++++++++++++++- .../material/WxMpMaterialUploadResult.java | 25 +++++++-- ...lVoiceAndImageDownloadRequestExecutor.java | 2 - ...ApacheMaterialNewsInfoRequestExecutor.java | 5 ++ ...lVoiceAndImageDownloadRequestExecutor.java | 5 +- .../JoddMaterialNewsInfoRequestExecutor.java | 4 ++ ...lVoiceAndImageDownloadRequestExecutor.java | 3 +- ...OkhttpMaterialNewsInfoRequestExecutor.java | 4 ++ ...lVoiceAndImageDownloadRequestExecutor.java | 3 +- .../WxMpMaterialNewsArticleGsonAdapter.java | 21 ++++++++ .../json/WxMpMaterialNewsGsonAdapter.java | 13 +++++ .../api/impl/WxMpMaterialServiceImplTest.java | 5 +- 13 files changed, 137 insertions(+), 21 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java index 99a63f91..90594fe0 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java @@ -77,19 +77,19 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { } @Override - public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException { + public InputStream materialImageOrVoiceDownload(String mediaId) throws WxErrorException { return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor - .create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), MATERIAL_GET_URL, media_id); + .create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), MATERIAL_GET_URL, mediaId); } @Override - public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException { - return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, media_id); + public WxMpMaterialVideoInfoResult materialVideoInfo(String mediaId) throws WxErrorException { + return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, mediaId); } @Override - public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException { - return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, media_id); + public WxMpMaterialNews materialNewsInfo(String mediaId) throws WxErrorException { + return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, mediaId); } @Override @@ -104,8 +104,8 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { } @Override - public boolean materialDelete(String media_id) throws WxErrorException { - return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_DEL_URL, media_id); + public boolean materialDelete(String mediaId) throws WxErrorException { + return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_DEL_URL, mediaId); } @Override diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialNews.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialNews.java index a107faca..51f2d0c6 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialNews.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialNews.java @@ -4,12 +4,16 @@ import me.chanjar.weixin.common.util.ToStringUtils; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import java.io.Serializable; +import java.util.Date; import java.util.ArrayList; import java.util.List; public class WxMpMaterialNews implements Serializable { private static final long serialVersionUID = -3283203652013494976L; + private Date createdTime; + private Date updatedTime; + private List articles = new ArrayList<>(); public List getArticles() { @@ -28,9 +32,25 @@ public class WxMpMaterialNews implements Serializable { return this.articles == null || this.articles.isEmpty(); } + public Date getCreatedTime() { + return this.createdTime; + } + + public void setCreatedTime(Date createdTime) { + this.createdTime = createdTime; + } + + public Date getUpdatedTime() { + return this.updatedTime; + } + + public void setUpdatedTime(Date updatedTime) { + this.updatedTime = updatedTime; + } + @Override public String toString() { - return ToStringUtils.toSimpleString(this); + return this.toJson(); } /** @@ -44,6 +64,8 @@ public class WxMpMaterialNews implements Serializable { * 6. digest 图文消息的描述 * 7. showCoverPic 是否显示封面,true为显示,false为不显示 * 8. url 点击图文消息跳转链接 + * 9. need_open_comment(新增字段) 否 Uint32 是否打开评论,0不打开,1打开 + * 10. only_fans_can_comment(新增字段) 否 Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论 * * * @author chanjarster @@ -87,6 +109,18 @@ public class WxMpMaterialNews implements Serializable { */ private String url; + /** + * need_open_comment + * 是否打开评论,0不打开,1打开 + */ + private Boolean needOpenComment; + + /** + * only_fans_can_comment + * 是否粉丝才可评论,0所有人可评论,1粉丝才可评论 + */ + private Boolean onlyFansCanComment; + public String getThumbMediaId() { return this.thumbMediaId; } @@ -159,6 +193,22 @@ public class WxMpMaterialNews implements Serializable { this.thumbUrl = thumbUrl; } + public Boolean getNeedOpenComment() { + return this.needOpenComment; + } + + public void setNeedOpenComment(Boolean needOpenComment) { + this.needOpenComment = needOpenComment; + } + + public Boolean getOnlyFansCanComment() { + return this.onlyFansCanComment; + } + + public void setOnlyFansCanComment(Boolean onlyFansCanComment) { + this.onlyFansCanComment = onlyFansCanComment; + } + @Override public String toString() { return ToStringUtils.toSimpleString(this); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialUploadResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialUploadResult.java index ec1d1154..0e5546b9 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialUploadResult.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/material/WxMpMaterialUploadResult.java @@ -1,17 +1,16 @@ package me.chanjar.weixin.mp.bean.material; +import me.chanjar.weixin.common.util.ToStringUtils; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import java.io.Serializable; public class WxMpMaterialUploadResult implements Serializable { - - /** - * - */ private static final long serialVersionUID = -128818731449449537L; private String mediaId; private String url; + private Integer errCode; + private String errMsg; public static WxMpMaterialUploadResult fromJson(String json) { return WxMpGsonBuilder.create().fromJson(json, WxMpMaterialUploadResult.class); @@ -33,9 +32,25 @@ public class WxMpMaterialUploadResult implements Serializable { this.url = url; } + public Integer getErrCode() { + return this.errCode; + } + + public void setErrCode(Integer errCode) { + this.errCode = errCode; + } + + public String getErrMsg() { + return this.errMsg; + } + + public void setErrMsg(String errMsg) { + this.errMsg = errMsg; + } + @Override public String toString() { - return "WxMpMaterialUploadResult [media_id=" + this.mediaId + ", url=" + this.url + "]"; + return ToStringUtils.toSimpleString(this); } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java index 8bf55d03..09cb70d0 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java @@ -13,13 +13,11 @@ public abstract class MaterialVoiceAndImageDownloadRequestExecutor impleme protected RequestHttp requestHttp; protected File tmpDirFile; - public MaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { this.requestHttp = requestHttp; this.tmpDirFile = tmpDirFile; } - public static RequestExecutor create(RequestHttp requestHttp, File tmpDirFile) { switch (requestHttp.getRequestType()) { case APACHE_HTTP: diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java index 77d22659..5298160f 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java @@ -14,6 +14,8 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.HashMap; @@ -23,6 +25,8 @@ import java.util.Map; * Created by ecoolper on 2017/5/5. */ public class ApacheMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + public ApacheMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) { super(requestHttp); } @@ -40,6 +44,7 @@ public class ApacheMaterialNewsInfoRequestExecutor extends MaterialNewsInfoReque httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + this.logger.debug("响应原始数据:{}", responseContent); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialVoiceAndImageDownloadRequestExecutor.java index 87d7b545..b1011a52 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialVoiceAndImageDownloadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialVoiceAndImageDownloadRequestExecutor.java @@ -18,6 +18,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -41,10 +42,10 @@ public class ApacheMaterialVoiceAndImageDownloadRequestExecutor extends Material params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost); - InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { + InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) { // 下载媒体文件出错 byte[] responseContent = IOUtils.toByteArray(inputStream); - String responseContentString = new String(responseContent, "UTF-8"); + String responseContentString = new String(responseContent, StandardCharsets.UTF_8); if (responseContentString.length() < 100) { try { WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java index a5d5ad1f..181a6407 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java @@ -12,6 +12,8 @@ import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -19,6 +21,7 @@ import java.io.IOException; * Created by ecoolper on 2017/5/5. */ public class JoddMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); public JoddMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) { super(requestHttp); } @@ -36,6 +39,7 @@ public class JoddMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequest response.charset(StringPool.UTF_8); String responseContent = response.bodyText(); + this.logger.debug("响应原始数据:{}", responseContent); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialVoiceAndImageDownloadRequestExecutor.java index a6317d5a..ca43a145 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialVoiceAndImageDownloadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialVoiceAndImageDownloadRequestExecutor.java @@ -17,6 +17,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; /** * Created by ecoolper on 2017/5/5. @@ -40,7 +41,7 @@ public class JoddMaterialVoiceAndImageDownloadRequestExecutor extends MaterialVo try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { // 下载媒体文件出错 byte[] responseContent = IOUtils.toByteArray(inputStream); - String responseContentString = new String(responseContent, "UTF-8"); + String responseContentString = new String(responseContent, StandardCharsets.UTF_8); if (responseContentString.length() < 100) { try { WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java index d51fbbe4..a0a1c052 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java @@ -8,6 +8,8 @@ import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import okhttp3.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -15,6 +17,7 @@ import java.io.IOException; * Created by ecoolper on 2017/5/5. */ public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); public OkhttpMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) { super(requestHttp); } @@ -44,6 +47,7 @@ public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoReque Response response = client.newCall(request).execute(); String responseContent = response.body().string(); + this.logger.debug("响应原始数据:{}", responseContent); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialVoiceAndImageDownloadRequestExecutor.java index 336b3a98..1d0a5f7f 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialVoiceAndImageDownloadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialVoiceAndImageDownloadRequestExecutor.java @@ -13,6 +13,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; /** * Created by ecoolper on 2017/5/5. @@ -50,7 +51,7 @@ public class OkhttpMaterialVoiceAndImageDownloadRequestExecutor extends Material // 下载媒体文件出错 byte[] responseContent = IOUtils.toByteArray(inputStream); - String responseContentString = new String(responseContent, "UTF-8"); + String responseContentString = new String(responseContent, StandardCharsets.UTF_8); if (responseContentString.length() < 100) { try { WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialNewsArticleGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialNewsArticleGsonAdapter.java index a74877f1..5a17a7bd 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialNewsArticleGsonAdapter.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialNewsArticleGsonAdapter.java @@ -11,6 +11,7 @@ package me.chanjar.weixin.mp.util.json; import com.google.gson.*; import me.chanjar.weixin.common.util.json.GsonHelper; import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; +import org.apache.commons.lang3.BooleanUtils; import java.lang.reflect.Type; @@ -37,6 +38,16 @@ public class WxMpMaterialNewsArticleGsonAdapter implements JsonSerializer, JsonDeserializer { @@ -41,6 +43,17 @@ public class WxMpMaterialNewsGsonAdapter implements JsonSerializer