Skip to content

Commit

Permalink
chanjarster#252 原有图文素材管理接口增加留言管理所需两个参数:need_open_comment 和 only_fans_…
Browse files Browse the repository at this point in the history
…can_comment
  • Loading branch information
binarywang committed Jul 2, 2017
1 parent 554fd08 commit eaad636
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws
}

@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
Expand All @@ -104,8 +104,8 @@ public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleU
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
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() {
Expand All @@ -28,9 +32,25 @@ public boolean isEmpty() {
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();
}

/**
Expand All @@ -44,6 +64,8 @@ public String toString() {
* 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
Expand Down Expand Up @@ -87,6 +109,18 @@ public static class WxMpMaterialNewsArticle {
*/
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;
}
Expand Down Expand Up @@ -159,6 +193,22 @@ public void setThumbUrl(String thumbUrl) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -33,9 +32,25 @@ public void setUrl(String url) {
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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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;
Expand All @@ -23,6 +25,8 @@
* 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);
}
Expand All @@ -40,6 +44,7 @@ public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorExc
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;

Expand All @@ -41,10 +42,10 @@ public InputStream execute(String uri, String materialId) throws WxErrorExceptio
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
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;

/**
* 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);
}
Expand All @@ -36,6 +39,7 @@ public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorExc
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* Created by ecoolper on 2017/5/5.
Expand All @@ -40,7 +41,7 @@ public InputStream execute(String uri, String materialId) throws WxErrorExceptio
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
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;

/**
* 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);
}
Expand Down Expand Up @@ -44,6 +47,7 @@ public Request authenticate(Route route, Response response) throws IOException {

Response response = client.newCall(request).execute();
String responseContent = response.body().string();
this.logger.debug("响应原始数据:{}", responseContent);

WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* Created by ecoolper on 2017/5/5.
Expand Down Expand Up @@ -50,7 +51,7 @@ public Request authenticate(Route route, Response response) throws IOException {

// 下载媒体文件出错
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;

Expand All @@ -37,6 +38,16 @@ public JsonElement serialize(WxMpMaterialNews.WxMpMaterialNewsArticle article, T
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;
}

Expand Down Expand Up @@ -81,6 +92,16 @@ public WxMpMaterialNews.WxMpMaterialNewsArticle deserialize(JsonElement jsonElem
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> {

Expand Down Expand Up @@ -41,6 +43,17 @@ public WxMpMaterialNews deserialize(JsonElement jsonElement, Type type, JsonDese
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;
}
}
Loading

0 comments on commit eaad636

Please sign in to comment.