Skip to content

Fix issue #3006: 【公众号】模板消息发送,缺少非必填字段client_msg_id #3013

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

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -52,6 +52,11 @@ public class WxMpTemplateMessage implements Serializable {
*/
private MiniProgram miniProgram;

/**
* 防重入id.
*/
private String clientMsgId;

/**
* 模板数据.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -19,6 +22,9 @@ public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSe
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public void testToJson() {
.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"));

System.out.println(tm.toJson());
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\"}}}");
}

Expand Down