添加群发支持

This commit is contained in:
Daniel Qian
2014-08-25 11:01:09 +08:00
parent 71215bbe79
commit f326d7e72e
20 changed files with 682 additions and 60 deletions

View File

@@ -11,7 +11,7 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import chanjarster.weixin.bean.result.WxError;
import chanjarster.weixin.bean.result.WxUploadResult;
import chanjarster.weixin.bean.result.WxMediaUploadResult;
import chanjarster.weixin.exception.WxErrorException;
/**
@@ -19,10 +19,10 @@ import chanjarster.weixin.exception.WxErrorException;
* @author chanjarster
*
*/
public class MediaUploadRequestExecutor implements RequestExecutor<WxUploadResult, File> {
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> {
@Override
public WxUploadResult execute(String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (file != null) {
HttpEntity entity = MultipartEntityBuilder
@@ -38,7 +38,7 @@ public class MediaUploadRequestExecutor implements RequestExecutor<WxUploadResul
if (error.getErrcode() != 0) {
throw new WxErrorException(error);
}
return WxUploadResult.fromJson(responseContent);
return WxMediaUploadResult.fromJson(responseContent);
}
}

View File

@@ -32,25 +32,25 @@ public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessag
messageJson.addProperty("touser", message.getTouser());
messageJson.addProperty("msgtype", message.getMsgtype());
if (WxConsts.MSG_TEXT.equals(message.getMsgtype())) {
if (WxConsts.CUSTOM_MSG_TEXT.equals(message.getMsgtype())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
messageJson.add("text", text);
}
if (WxConsts.MSG_IMAGE.equals(message.getMsgtype())) {
if (WxConsts.CUSTOM_MSG_IMAGE.equals(message.getMsgtype())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMedia_id());
messageJson.add("image", image);
}
if (WxConsts.MSG_VOICE.equals(message.getMsgtype())) {
if (WxConsts.CUSTOM_MSG_VOICE.equals(message.getMsgtype())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", message.getMedia_id());
messageJson.add("voice", voice);
}
if (WxConsts.MSG_VIDEO.equals(message.getMsgtype())) {
if (WxConsts.CUSTOM_MSG_VIDEO.equals(message.getMsgtype())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", message.getMedia_id());
video.addProperty("thumb_media_id", message.getThumb_media_id());
@@ -59,7 +59,7 @@ public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessag
messageJson.add("video", video);
}
if (WxConsts.MSG_MUSIC.equals(message.getMsgtype())) {
if (WxConsts.CUSTOM_MSG_MUSIC.equals(message.getMsgtype())) {
JsonObject music = new JsonObject();
music.addProperty("title", message.getTitle());
music.addProperty("description", message.getDescription());
@@ -69,7 +69,7 @@ public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessag
messageJson.add("music", music);
}
if (WxConsts.MSG_NEWS.equals(message.getMsgtype())) {
if (WxConsts.CUSTOM_MSG_NEWS.equals(message.getMsgtype())) {
JsonArray articleJsonArray = new JsonArray();
for (WxArticle article : message.getArticles()) {
JsonObject articleJson = new JsonObject();

View File

@@ -1,6 +1,8 @@
package chanjarster.weixin.util.json;
import chanjarster.weixin.bean.WxCustomMessage;
import chanjarster.weixin.bean.WxMassMessage;
import chanjarster.weixin.bean.WxMassNews;
import chanjarster.weixin.bean.WxMenu;
import com.google.gson.Gson;
@@ -14,6 +16,8 @@ public class WxGsonBuilder {
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCustomMessage.class, new WxCustomMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter());
INSTANCE.registerTypeAdapter(WxMassNews.class, new WxMassNewsGsonAdapter());
INSTANCE.registerTypeAdapter(WxMassMessage.class, new WxMassMessageGsonAdapter());
}
public static Gson create() {

View File

@@ -0,0 +1,74 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package chanjarster.weixin.util.json;
import java.lang.reflect.Type;
import chanjarster.weixin.api.WxConsts;
import chanjarster.weixin.bean.WxMassMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
*
* @author qianjia
*
*/
public class WxMassMessageGsonAdapter implements JsonSerializer<WxMassMessage> {
public JsonElement serialize(WxMassMessage message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
if (message.getTouser().size() > 0) {
JsonArray sub = new JsonArray();
for(String openId : message.getTouser()) {
sub.add(new JsonPrimitive(openId));
}
messageJson.add("touser", sub);
} else {
JsonObject sub = new JsonObject();
sub.addProperty("group_id", message.getGroup_id());
messageJson.add("filter", sub);
}
if (WxConsts.MASS_MSG_NEWS.equals(message.getMsgtype())) {
JsonObject sub = new JsonObject();
sub.addProperty("media_id", message.getMedia_id());
messageJson.add("mpnews", sub);
}
if (WxConsts.MASS_MSG_TEXT.equals(message.getMsgtype())) {
JsonObject sub = new JsonObject();
sub.addProperty("content", message.getContent());
messageJson.add("text", sub);
}
if (WxConsts.MASS_MSG_VOICE.equals(message.getMsgtype())) {
JsonObject sub = new JsonObject();
sub.addProperty("media_id", message.getMedia_id());
messageJson.add("voice", sub);
}
if (WxConsts.MASS_MSG_IMAGE.equals(message.getMsgtype())) {
JsonObject sub = new JsonObject();
sub.addProperty("media_id", message.getMedia_id());
messageJson.add("image", sub);
}
if (WxConsts.MASS_MSG_VIDEO.equals(message.getMsgtype())) {
JsonObject sub = new JsonObject();
sub.addProperty("media_id", message.getMedia_id());
messageJson.add("mpvideo", sub);
}
messageJson.addProperty("msgtype", message.getMsgtype());
return messageJson;
}
}

View File

@@ -0,0 +1,56 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package chanjarster.weixin.util.json;
import java.lang.reflect.Type;
import chanjarster.weixin.bean.WxMassNews;
import chanjarster.weixin.bean.WxMassNews.WxMassNewsArticle;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
*
* @author qianjia
*
*/
public class WxMassNewsGsonAdapter implements JsonSerializer<WxMassNews> {
public JsonElement serialize(WxMassNews message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject newsJson = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (WxMassNewsArticle article : message.getArticles()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("thumb_media_id", article.getThumb_media_id());
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("content", article.getContent());
if (null != article.getAuthor()) {
articleJson.addProperty("author", article.getAuthor());
}
if (null != article.getContent_source_url()) {
articleJson.addProperty("content_source_url", article.getContent_source_url());
}
if (null != article.getDigest()) {
articleJson.addProperty("digest", article.getDigest());
}
articleJson.addProperty("show_cover_pic", article.isShow_cover_pic() ? "1" : "0");
articleJsonArray.add(articleJson);
}
newsJson.add("articles", articleJsonArray);
return newsJson;
}
}