🎨 #3439 【公众号】模版消息长度限制问题优化

This commit is contained in:
Binary Wang 2024-12-18 23:19:15 +08:00
parent 6840722947
commit 05c112309b
2 changed files with 55 additions and 12 deletions

View File

@ -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 发送模板消息接口部分
* 参考 <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">发送模板消息接口部分</a>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@ -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 <a href=https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#%E7%B1%BB%E7%9B%AE%E6%A8%A1%E6%9D%BF%E6%B6%88%E6%81%AF">模板消息</a>
*/
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);
}

View File

@ -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());
}
}