Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【公众号】群发消息支持多图片群发 #2851

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public static class MassMsgType {
public static final String TEXT = "text";
public static final String VOICE = "voice";
public static final String IMAGE = "image";
public static final String IMAGES = "images";
public static final String MPVIDEO = "mpvideo";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public class WxMpMassOpenIdsMessage implements Serializable {
private String msgType;
private String content;
private String mediaId;
/**
* 图片列表
*/
private List<String> mediaIds;
/**
* 文章被判定为转载时,是否继续进行群发操作。
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
* 按标签群发的消息.
Expand Down Expand Up @@ -34,6 +35,10 @@ public class WxMpMassTagMessage implements Serializable {
private String msgType;
private String content;
private String mediaId;
/**
* 图片列表
*/
private List<String> mediaIds;
/**
* 是否群发给所有用户.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.chanjar.weixin.mp.util.json;

import com.google.gson.*;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.List;

/**
* @author someone
Expand Down Expand Up @@ -39,8 +41,17 @@ public JsonElement serialize(WxMpMassOpenIdsMessage message, Type typeOfSrc, Jso
}
if (WxConsts.MassMsgType.IMAGE.equals(message.getMsgType())) {
JsonObject sub = new JsonObject();
sub.addProperty("media_id", message.getMediaId());
messageJson.add(WxConsts.MassMsgType.IMAGE, sub);
List<String> mediaIds = message.getMediaIds();
if (mediaIds != null && !mediaIds.isEmpty() ) {
JsonArray json = new JsonArray();
mediaIds.forEach(json::add);
sub.add("media_ids", json);
messageJson.add(WxConsts.MassMsgType.IMAGES, sub);
} else {
String mediaId = message.getMediaId();
sub.addProperty("media_id", mediaId);
messageJson.add(WxConsts.MassMsgType.IMAGE, sub);
}
}
if (WxConsts.MassMsgType.MPVIDEO.equals(message.getMsgType())) {
JsonObject sub = new JsonObject();
Expand All @@ -64,4 +75,7 @@ public JsonElement serialize(WxMpMassOpenIdsMessage message, Type typeOfSrc, Jso
return messageJson;
}

public static void main(String[] args) {

}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package me.chanjar.weixin.mp.util.json;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.*;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.List;

/**
* 群发消息json转换适配器.
Expand Down Expand Up @@ -47,8 +46,17 @@ public JsonElement serialize(WxMpMassTagMessage message, Type typeOfSrc, JsonSer
}
if (WxConsts.MassMsgType.IMAGE.equals(message.getMsgType())) {
JsonObject sub = new JsonObject();
sub.addProperty("media_id", message.getMediaId());
messageJson.add(WxConsts.MassMsgType.IMAGE, sub);
List<String> mediaIds = message.getMediaIds();
if (mediaIds != null && !mediaIds.isEmpty() ) {
JsonArray json = new JsonArray();
mediaIds.forEach(json::add);
sub.add("media_ids", json);
messageJson.add(WxConsts.MassMsgType.IMAGES, sub);
} else {
String mediaId = message.getMediaId();
sub.addProperty("media_id", mediaId);
messageJson.add(WxConsts.MassMsgType.IMAGE, sub);
}
}
if (WxConsts.MassMsgType.MPVIDEO.equals(message.getMsgType())) {
JsonObject sub = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.chanjar.weixin.mp.api.impl;

import com.google.inject.Inject;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
Expand All @@ -21,6 +22,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.testng.Assert.assertNotNull;

Expand All @@ -38,7 +41,7 @@ public class WxMpMassMessageServiceImplTest {
@Test
public void testTextMassOpenIdsMessageSend() throws WxErrorException {
// 发送群发消息
TestConfigStorage configProvider = (TestConfigStorage) this.wxService .getWxMpConfigStorage();
TestConfigStorage configProvider = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MassMsgType.TEXT);
massMessage.setContent("测试群发消息\n欢迎欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
Expand All @@ -63,6 +66,30 @@ public void testMediaMassOpenIdsMessageSend(String massMsgType, String mediaId)
assertNotNull(massResult.getMsgId());
}

@Test
public void testImagesMassOpenIdsMessageSend() throws WxErrorException {
// 发送群发消息
List<String> massMsg = new ArrayList<>();
for (int i = 0; i < 2; i++) {
try (InputStream inputStream = ClassLoader
.getSystemResourceAsStream(i + ".jpeg")) {
WxMediaUploadResult uploadMediaRes = this.wxService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, inputStream);
assertNotNull(uploadMediaRes);
assertNotNull(uploadMediaRes.getMediaId());
massMsg.add(uploadMediaRes.getMediaId());
} catch (IOException e) {
e.printStackTrace();
}
}
WxMpMassTagMessage massMessage = new WxMpMassTagMessage();
massMessage.setMsgType(WxConsts.MassMsgType.IMAGE);
massMessage.setMediaIds(new ArrayList<>(massMsg));
massMessage.setSendAll(true);
WxMpMassSendResult massResult = this.wxService.getMassMessageService().massGroupMessageSend(massMessage);
assertNotNull(massResult);
assertNotNull(massResult.getMsgId());
}

@Test
public void testTextMassGroupMessageSend() throws WxErrorException {
WxMpMassTagMessage massMessage = new WxMpMassTagMessage();
Expand All @@ -77,7 +104,7 @@ public void testTextMassGroupMessageSend() throws WxErrorException {
}

@Test(dataProvider = "massMessages")
public void testMediaMassGroupMessageSend(String massMsgType, String mediaId) throws WxErrorException {
public void testMediaMassGroupMessageSend(String massMsgType, String mediaId) throws WxErrorException {
WxMpMassTagMessage massMessage = new WxMpMassTagMessage();
massMessage.setMsgType(massMsgType);
massMessage.setMediaId(mediaId);
Expand Down Expand Up @@ -123,7 +150,8 @@ public Object[][] massMessages() throws WxErrorException, IOException {
.mediaUpload(WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, inputStream);
assertNotNull(uploadMediaRes);
assertNotNull(uploadMediaRes.getMediaId());
messages[1] = new Object[]{WxConsts.MassMsgType.IMAGE, uploadMediaRes.getMediaId()};
messages[1] = new Object[]{WxConsts.MassMsgType.IMAGE, uploadMediaRes.getMediaId()
};
}

/*
Expand Down
Binary file added weixin-java-mp/src/test/resources/0.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added weixin-java-mp/src/test/resources/1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.