🎨 #3006 【公众号】模板消息发送接口增加字段client_msg_id

This commit is contained in:
仲济川 2023-05-11 20:16:42 +08:00 committed by GitHub
parent f8fdc62205
commit 077f828019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View File

@ -52,6 +52,11 @@ public class WxMpTemplateMessage implements Serializable {
*/
private MiniProgram miniProgram;
/**
* 防重入id.
*/
private String clientMsgId;
/**
* 模板数据.
*/

View File

@ -1,15 +1,18 @@
package me.chanjar.weixin.mp.util.json;
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
/**
* 模板消息转Json类型转换器
* @author chanjarster
*/
public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> {
@ -19,6 +22,9 @@ public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTempla
JsonObject messageJson = new JsonObject();
messageJson.addProperty("touser", message.getToUser());
messageJson.addProperty("template_id", message.getTemplateId());
if (StringUtils.isNotBlank(message.getClientMsgId())) {
messageJson.addProperty("client_msg_id", message.getClientMsgId());
}
if (message.getUrl() != null) {
messageJson.addProperty("url", message.getUrl());
}

View File

@ -19,14 +19,14 @@ public class WxMpTemplateMessageTest {
.templateId("ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY")
.miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar",true))
.url("http://weixin.qq.com/download")
.clientMsgId("MSG_000001")
.build();
tm.addData(
new WxMpTemplateData("first", "haahah", "#FF00FF"));
tm.addData(
new WxMpTemplateData("remark", "heihei", "#FF00FF"));
assertEquals(tm.toJson(), "{\"touser\":\"OPENID\",\"template_id\":\"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY\",\"url\":\"http://weixin.qq.com/download\",\"miniprogram\":{\"appid\":\"xiaochengxuappid12345\",\"pagepath\":\"index?foo=bar\"},\"data\":{\"first\":{\"value\":\"haahah\",\"color\":\"#FF00FF\"},\"remark\":{\"value\":\"heihei\",\"color\":\"#FF00FF\"}}}");
assertEquals(tm.toJson(), "{\"touser\":\"OPENID\",\"template_id\":\"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY\",\"client_msg_id\":\"MSG_000001\",\"url\":\"http://weixin.qq.com/download\",\"miniprogram\":{\"appid\":\"xiaochengxuappid12345\",\"path\":\"index?foo=bar\"},\"data\":{\"first\":{\"value\":\"haahah\",\"color\":\"#FF00FF\"},\"remark\":{\"value\":\"heihei\",\"color\":\"#FF00FF\"}}}");
}
}