From 05c112309b00a5a1422753e52da3b569fc9e67e2 Mon Sep 17 00:00:00 2001
From: Binary Wang
Date: Wed, 18 Dec 2024 23:19:15 +0800
Subject: [PATCH] =?UTF-8?q?:art:=20#3439=20=E3=80=90=E5=85=AC=E4=BC=97?=
=?UTF-8?q?=E5=8F=B7=E3=80=91=E6=A8=A1=E7=89=88=E6=B6=88=E6=81=AF=E9=95=BF?=
=?UTF-8?q?=E5=BA=A6=E9=99=90=E5=88=B6=E9=97=AE=E9=A2=98=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../mp/bean/template/WxMpTemplateMessage.java | 41 ++++++++++++++-----
.../template/WxMpTemplateMessageTest.java | 26 +++++++++++-
2 files changed, 55 insertions(+), 12 deletions(-)
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java
index a04d8bb89..02211937f 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java
@@ -1,20 +1,16 @@
package me.chanjar.weixin.mp.bean.template;
+import lombok.*;
+import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
+import org.apache.commons.lang3.StringUtils;
+
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
-import lombok.AllArgsConstructor;
-import lombok.Builder;
-import lombok.Data;
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import lombok.Setter;
-import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
-
/**
* 模板消息.
- * 参考 http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN 发送模板消息接口部分
+ * 参考 发送模板消息接口部分
*
* @author Binary Wang
*/
@@ -67,10 +63,35 @@ public class WxMpTemplateMessage implements Serializable {
if (this.data == null) {
this.data = new ArrayList<>();
}
- this.data.add(datum);
+ this.data.add(resetValue(datum));
return this;
}
+ /**
+ * 处理微信模版消息字符串长度问题
+ *
+ * @link 模板消息
+ */
+ private WxMpTemplateData resetValue(WxMpTemplateData datum) {
+ String name = datum.getName();
+ String value = datum.getValue();
+
+ if (StringUtils.startsWith(name, "thing") && value.length() > 20) {
+ value = StringUtils.substring(value, 0, 17) + "...";
+ } else if (StringUtils.startsWith(name, "character_string") && value.length() > 32) {
+ value = StringUtils.substring(value, 0, 29) + "...";
+ } else if (StringUtils.startsWith(name, "phone_number") && value.length() > 17) {
+ value = StringUtils.substring(value, 0, 14) + "...";
+ } else if (StringUtils.startsWith(name, "car_number") && value.length() > 8) {
+ value = StringUtils.substring(value, 0, 5) + "...";
+ } else if (StringUtils.startsWith(name, "const") && value.length() > 20) {
+ value = StringUtils.substring(value, 0, 17) + "...";
+ }
+
+ datum.setValue(value);
+ return datum;
+ }
+
public String toJson() {
return WxMpGsonBuilder.create().toJson(this);
}
diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessageTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessageTest.java
index fa7cd9296..5012be59a 100644
--- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessageTest.java
+++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessageTest.java
@@ -17,7 +17,7 @@ public class WxMpTemplateMessageTest {
WxMpTemplateMessage tm = WxMpTemplateMessage.builder()
.toUser("OPENID")
.templateId("ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY")
- .miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar",true))
+ .miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar", true))
.url("http://weixin.qq.com/download")
.clientMsgId("MSG_000001")
.build();
@@ -26,7 +26,29 @@ public class WxMpTemplateMessageTest {
new WxMpTemplateData("first", "haahah", "#FF00FF"));
tm.addData(
new WxMpTemplateData("remark", "heihei", "#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\"}}}");
+
+ assertEquals("{\"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\"}}}", tm.toJson());
}
+ @Test
+ public void testAddData() {
+ WxMpTemplateMessage tm = WxMpTemplateMessage.builder().build()
+ .addData(new WxMpTemplateData("thing01", "张三李四王麻子张三李四王麻子张三李四王麻子张三李四王麻子"))
+ .addData(new WxMpTemplateData("time01", "2019年10月1日 15:01"))
+ .addData(new WxMpTemplateData("character_string01", "1234567890123456789012345678901234567890"))
+ .addData(new WxMpTemplateData("amount01", "¥100.21"))
+ .addData(new WxMpTemplateData("phone_number01", "+86-0766-668888661111"))
+ .addData(new WxMpTemplateData("car_number01", "粤A8Z888挂9"))
+ .addData(new WxMpTemplateData("const01", "支付状态、排队状态、天气状态、物流状态、用药提醒、还款提醒"));
+
+ assertEquals(7, tm.getData().size());
+
+ assertEquals("张三李四王麻子张三李四王麻子张三李...", tm.getData().get(0).getValue());
+ assertEquals("2019年10月1日 15:01", tm.getData().get(1).getValue());
+ assertEquals("12345678901234567890123456789...", tm.getData().get(2).getValue());
+ assertEquals("¥100.21", tm.getData().get(3).getValue());
+ assertEquals("+86-0766-66888...", tm.getData().get(4).getValue());
+ assertEquals("粤A8Z8...", tm.getData().get(5).getValue());
+ assertEquals("支付状态、排队状态、天气状态、物流...", tm.getData().get(6).getValue());
+ }
}