mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-12-22 03:19:58 +08:00
添加群发消息单元测试
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package chanjarster.weixin.util.json;
|
||||
|
||||
import chanjarster.weixin.bean.WxCustomMessage;
|
||||
import chanjarster.weixin.bean.WxMassMessage;
|
||||
import chanjarster.weixin.bean.WxMassGroupMessage;
|
||||
import chanjarster.weixin.bean.WxMassNews;
|
||||
import chanjarster.weixin.bean.WxMassOpenIdsMessage;
|
||||
import chanjarster.weixin.bean.WxMenu;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
@@ -17,7 +18,8 @@ public class WxGsonBuilder {
|
||||
INSTANCE.registerTypeAdapter(WxCustomMessage.class, new WxCustomMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMassNews.class, new WxMassNewsGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMassMessage.class, new WxMassMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMassGroupMessage.class, new WxMassMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMassOpenIdsMessage.class, new WxMassOpenIdsMessageGsonAdapter());
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
|
||||
@@ -11,12 +11,10 @@ package chanjarster.weixin.util.json;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import chanjarster.weixin.api.WxConsts;
|
||||
import chanjarster.weixin.bean.WxMassMessage;
|
||||
import chanjarster.weixin.bean.WxMassGroupMessage;
|
||||
|
||||
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;
|
||||
|
||||
@@ -25,47 +23,39 @@ import com.google.gson.JsonSerializer;
|
||||
* @author qianjia
|
||||
*
|
||||
*/
|
||||
public class WxMassMessageGsonAdapter implements JsonSerializer<WxMassMessage> {
|
||||
public class WxMassMessageGsonAdapter implements JsonSerializer<WxMassGroupMessage> {
|
||||
|
||||
public JsonElement serialize(WxMassMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
public JsonElement serialize(WxMassGroupMessage 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);
|
||||
}
|
||||
JsonObject filter = new JsonObject();
|
||||
filter.addProperty("group_id", message.getGroup_id());
|
||||
messageJson.add("filter", filter);
|
||||
|
||||
if (WxConsts.MASS_MSG_NEWS.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add("mpnews", sub);
|
||||
messageJson.add(WxConsts.MASS_MSG_NEWS, sub);
|
||||
}
|
||||
if (WxConsts.MASS_MSG_TEXT.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("content", message.getContent());
|
||||
messageJson.add("text", sub);
|
||||
messageJson.add(WxConsts.MASS_MSG_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);
|
||||
messageJson.add(WxConsts.MASS_MSG_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);
|
||||
messageJson.add(WxConsts.MASS_MSG_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.add(WxConsts.MASS_MSG_VIDEO, sub);
|
||||
}
|
||||
messageJson.addProperty("msgtype", message.getMsgtype());
|
||||
return messageJson;
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.WxMassOpenIdsMessage;
|
||||
|
||||
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 WxMassOpenIdsMessageGsonAdapter implements JsonSerializer<WxMassOpenIdsMessage> {
|
||||
|
||||
public JsonElement serialize(WxMassOpenIdsMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
|
||||
JsonArray touser = new JsonArray();
|
||||
for (String openId : message.getTouser()) {
|
||||
touser.add(new JsonPrimitive(openId));
|
||||
}
|
||||
messageJson.add("touser", touser);
|
||||
|
||||
if (WxConsts.MASS_MSG_NEWS.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add(WxConsts.MASS_MSG_NEWS, sub);
|
||||
}
|
||||
if (WxConsts.MASS_MSG_TEXT.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("content", message.getContent());
|
||||
messageJson.add(WxConsts.MASS_MSG_TEXT, sub);
|
||||
}
|
||||
if (WxConsts.MASS_MSG_VOICE.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add(WxConsts.MASS_MSG_VOICE, sub);
|
||||
}
|
||||
if (WxConsts.MASS_MSG_IMAGE.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add(WxConsts.MASS_MSG_IMAGE, sub);
|
||||
}
|
||||
if (WxConsts.MASS_MSG_VIDEO.equals(message.getMsgtype())) {
|
||||
JsonObject sub = new JsonObject();
|
||||
sub.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add(WxConsts.MASS_MSG_VIDEO, sub);
|
||||
}
|
||||
messageJson.addProperty("msgtype", message.getMsgtype());
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user