| @@ -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 | |||
| @@ -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<WxMpMaterialNewsArticle> articles = new ArrayList<>(); | |||
| public List<WxMpMaterialNewsArticle> 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粉丝才可评论 | |||
| * </pre> | |||
| * | |||
| * @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); | |||
| @@ -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); | |||
| } | |||
| } | |||
| @@ -13,13 +13,11 @@ public abstract class MaterialVoiceAndImageDownloadRequestExecutor<H, P> impleme | |||
| protected RequestHttp<H, P> requestHttp; | |||
| protected File tmpDirFile; | |||
| public MaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) { | |||
| this.requestHttp = requestHttp; | |||
| this.tmpDirFile = tmpDirFile; | |||
| } | |||
| public static RequestExecutor<InputStream, String> create(RequestHttp requestHttp, File tmpDirFile) { | |||
| switch (requestHttp.getRequestType()) { | |||
| case APACHE_HTTP: | |||
| @@ -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<CloseableHttpClient, HttpHost> { | |||
| 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); | |||
| @@ -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); | |||
| @@ -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<HttpConnectionProvider, ProxyInfo> { | |||
| 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); | |||
| @@ -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); | |||
| @@ -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<ConnectionPool, OkHttpProxyInfo> { | |||
| 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) { | |||
| @@ -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); | |||
| @@ -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<WxMpMa | |||
| if (null != article.getUrl()) { | |||
| articleJson.addProperty("url", article.getUrl()); | |||
| } | |||
| if (null != article.getNeedOpenComment()) { | |||
| articleJson.addProperty("need_open_comment", | |||
| BooleanUtils.toInteger(article.getNeedOpenComment(), 1, 0)); | |||
| } | |||
| if (null != article.getOnlyFansCanComment()) { | |||
| articleJson.addProperty("only_fans_can_comment", | |||
| BooleanUtils.toInteger(article.getOnlyFansCanComment(), 1, 0)); | |||
| } | |||
| return articleJson; | |||
| } | |||
| @@ -81,6 +92,16 @@ public class WxMpMaterialNewsArticleGsonAdapter implements JsonSerializer<WxMpMa | |||
| if (url != null && !url.isJsonNull()) { | |||
| article.setUrl(GsonHelper.getAsString(url)); | |||
| } | |||
| JsonElement needOpenComment = articleInfo.get("need_open_comment"); | |||
| if (needOpenComment != null && !needOpenComment.isJsonNull()) { | |||
| article.setNeedOpenComment(GsonHelper.getAsBoolean(needOpenComment)); | |||
| } | |||
| JsonElement onlyFansCanComment = articleInfo.get("only_fans_can_comment"); | |||
| if (onlyFansCanComment != null && !onlyFansCanComment.isJsonNull()) { | |||
| article.setOnlyFansCanComment(GsonHelper.getAsBoolean(onlyFansCanComment)); | |||
| } | |||
| return article; | |||
| } | |||
| } | |||
| @@ -9,9 +9,11 @@ | |||
| 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 java.lang.reflect.Type; | |||
| import java.util.Date; | |||
| public class WxMpMaterialNewsGsonAdapter implements JsonSerializer<WxMpMaterialNews>, JsonDeserializer<WxMpMaterialNews> { | |||
| @@ -41,6 +43,17 @@ public class WxMpMaterialNewsGsonAdapter implements JsonSerializer<WxMpMaterialN | |||
| wxMpMaterialNews.addArticle(article); | |||
| } | |||
| } | |||
| if (json.get("create_time") != null && !json.get("create_time").isJsonNull()) { | |||
| Date createTime = new Date(GsonHelper.getAsLong(json.get("create_time"))); | |||
| wxMpMaterialNews.setCreatedTime(createTime); | |||
| } | |||
| if (json.get("update_time") != null && !json.get("update_time").isJsonNull()) { | |||
| Date updateTime = new Date(GsonHelper.getAsLong(json.get("update_time"))); | |||
| wxMpMaterialNews.setUpdatedTime(updateTime); | |||
| } | |||
| return wxMpMaterialNews; | |||
| } | |||
| } | |||
| @@ -175,7 +175,7 @@ public class WxMpMaterialServiceImplTest { | |||
| } | |||
| } | |||
| @Test(dependsOnMethods = {"testAddNews"}) | |||
| @Test(dependsOnMethods = {"testAddNews","testUploadMaterial"}) | |||
| public void testGetNewsInfo() throws WxErrorException { | |||
| WxMpMaterialNews wxMpMaterialNewsSingle = this.wxService | |||
| .getMaterialService().materialNewsInfo(this.singleNewsMediaId); | |||
| @@ -183,6 +183,9 @@ public class WxMpMaterialServiceImplTest { | |||
| .getMaterialService().materialNewsInfo(this.multiNewsMediaId); | |||
| assertNotNull(wxMpMaterialNewsSingle); | |||
| assertNotNull(wxMpMaterialNewsMultiple); | |||
| System.out.println(wxMpMaterialNewsSingle); | |||
| System.out.println(wxMpMaterialNewsMultiple); | |||
| } | |||
| @Test(dependsOnMethods = {"testGetNewsInfo"}) | |||