Skip to content

Commit

Permalink
Fix issue binarywang#3006: 【公众号】模板消息发送,缺少非必填字段client_msg_id
Browse files Browse the repository at this point in the history
  • Loading branch information
仲济川 committed May 11, 2023
1 parent 34400c5 commit 66342da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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,13 +1,14 @@
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;

/**
* @author chanjarster
Expand All @@ -19,6 +20,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

0 comments on commit 66342da

Please sign in to comment.