mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-04 20:57:47 +08:00
优化模版消息代码,添加默认构造方法
This commit is contained in:
parent
cae504b6bd
commit
ce281bea32
@ -1,24 +1,28 @@
|
||||
package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 模板消息.
|
||||
* 参考 https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#接口说明 模板消息部分
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
@Data
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class WxMaTemplateMessage implements Serializable {
|
||||
private static final long serialVersionUID = 5063374783759519418L;
|
||||
|
||||
/**
|
||||
* 接收者(用户)的 openid.
|
||||
* <pre>
|
||||
* 参数:touser
|
||||
* 是否必填: 是
|
||||
@ -28,6 +32,7 @@ public class WxMaTemplateMessage implements Serializable {
|
||||
private String toUser;
|
||||
|
||||
/**
|
||||
* 所需下发的模板消息的id.
|
||||
* <pre>
|
||||
* 参数:template_id
|
||||
* 是否必填: 是
|
||||
@ -37,6 +42,7 @@ public class WxMaTemplateMessage implements Serializable {
|
||||
private String templateId;
|
||||
|
||||
/**
|
||||
* 点击模板卡片后的跳转页面,仅限本小程序内的页面.
|
||||
* <pre>
|
||||
* 参数:page
|
||||
* 是否必填: 否
|
||||
@ -46,6 +52,7 @@ public class WxMaTemplateMessage implements Serializable {
|
||||
private String page;
|
||||
|
||||
/**
|
||||
* 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id.
|
||||
* <pre>
|
||||
* 参数:form_id
|
||||
* 是否必填: 是
|
||||
@ -55,16 +62,17 @@ public class WxMaTemplateMessage implements Serializable {
|
||||
private String formId;
|
||||
|
||||
/**
|
||||
* 模板内容,不填则下发空模板.
|
||||
* <pre>
|
||||
* 参数:data
|
||||
* 是否必填: 是
|
||||
* 描述: 模板内容,不填则下发空模板
|
||||
* </pre>
|
||||
*/
|
||||
@Builder.Default
|
||||
private final List<Data> data = new ArrayList<>();
|
||||
private List<Data> data;
|
||||
|
||||
/**
|
||||
* 模板内容字体的颜色,不填默认黑色.
|
||||
* <pre>
|
||||
* 参数:color
|
||||
* 是否必填: 否
|
||||
@ -74,6 +82,7 @@ public class WxMaTemplateMessage implements Serializable {
|
||||
private String color;
|
||||
|
||||
/**
|
||||
* 模板需要放大的关键词,不填则默认无放大.
|
||||
* <pre>
|
||||
* 参数:emphasis_keyword
|
||||
* 是否必填: 否
|
||||
@ -82,6 +91,15 @@ public class WxMaTemplateMessage implements Serializable {
|
||||
*/
|
||||
private String emphasisKeyword;
|
||||
|
||||
public WxMaTemplateMessage addData(Data datum) {
|
||||
if (this.data == null) {
|
||||
this.data = new ArrayList<>();
|
||||
}
|
||||
this.data.add(datum);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return WxMaGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ public class WxMaMsgServiceImplTest {
|
||||
.templateId(config.getTemplateId())
|
||||
.emphasisKeyword("keyword1.DATA")
|
||||
.build();
|
||||
|
||||
//templateMessage.addData( new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"));
|
||||
this.wxService.getMsgService().sendTemplateMsg(templateMessage);
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package me.chanjar.weixin.mp.bean.template;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.*;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -9,46 +8,56 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 模板消息.
|
||||
* 参考 http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN 发送模板消息接口部分
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
@Data
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class WxMpTemplateMessage implements Serializable {
|
||||
private static final long serialVersionUID = 5063374783759519418L;
|
||||
|
||||
/**
|
||||
* 接收者openid
|
||||
* 接收者openid.
|
||||
*/
|
||||
private String toUser;
|
||||
|
||||
/**
|
||||
* 模板ID
|
||||
* 模板ID.
|
||||
*/
|
||||
private String templateId;
|
||||
|
||||
/**
|
||||
* 模板跳转链接.
|
||||
* <pre>
|
||||
* 跳小程序所需数据,不需跳小程序可不用传该数据
|
||||
* url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。
|
||||
* 开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。
|
||||
* </pre>
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 模板跳转链接
|
||||
* 跳小程序所需数据,不需跳小程序可不用传该数据.
|
||||
*
|
||||
* @see #url
|
||||
*/
|
||||
private MiniProgram miniProgram;
|
||||
|
||||
/**
|
||||
* 模板数据
|
||||
* 模板数据.
|
||||
*/
|
||||
@Builder.Default
|
||||
private final List<WxMpTemplateData> data = new ArrayList<>();
|
||||
private List<WxMpTemplateData> data;
|
||||
|
||||
public void addWxMpTemplateData(WxMpTemplateData datum) {
|
||||
public WxMpTemplateMessage addData(WxMpTemplateData datum) {
|
||||
if (this.data == null) {
|
||||
this.data = new ArrayList<>();
|
||||
}
|
||||
this.data.add(datum);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
@ -56,17 +65,13 @@ public class WxMpTemplateMessage implements Serializable {
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class MiniProgram implements Serializable {
|
||||
private static final long serialVersionUID = -7945254706501974849L;
|
||||
|
||||
private String appid;
|
||||
private String pagePath;
|
||||
|
||||
public MiniProgram(String appid, String pagePath) {
|
||||
this.appid = appid;
|
||||
this.pagePath = pagePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,8 +10,9 @@ import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.testng.*;
|
||||
import org.testng.annotations.*;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
@ -37,12 +38,12 @@ public class WxMpTemplateMsgServiceImplTest {
|
||||
.getWxMpConfigStorage();
|
||||
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
||||
.toUser(configStorage.getOpenid())
|
||||
.templateId(configStorage.getTemplateId()).build();
|
||||
templateMessage.addWxMpTemplateData(
|
||||
new WxMpTemplateData("first", dateFormat.format(new Date()), "#FF00FF"));
|
||||
templateMessage.addWxMpTemplateData(
|
||||
new WxMpTemplateData("remark", RandomStringUtils.randomAlphanumeric(100), "#FF00FF"));
|
||||
templateMessage.setUrl(" ");
|
||||
.templateId(configStorage.getTemplateId())
|
||||
.url(" ")
|
||||
.build();
|
||||
|
||||
templateMessage.addData(new WxMpTemplateData("first", dateFormat.format(new Date()), "#FF00FF"))
|
||||
.addData(new WxMpTemplateData("remark", RandomStringUtils.randomAlphanumeric(100), "#FF00FF"));
|
||||
String msgId = this.wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
||||
Assert.assertNotNull(msgId);
|
||||
System.out.println(msgId);
|
||||
|
@ -1,8 +1,8 @@
|
||||
package me.chanjar.weixin.mp.bean.template;
|
||||
|
||||
import org.testng.annotations.*;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.AssertJUnit.*;
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@ -13,7 +13,7 @@ import static org.testng.AssertJUnit.*;
|
||||
*/
|
||||
public class WxMpTemplateMessageTest {
|
||||
@Test
|
||||
public void testToJson() throws Exception {
|
||||
public void testToJson() {
|
||||
WxMpTemplateMessage tm = WxMpTemplateMessage.builder()
|
||||
.toUser("OPENID")
|
||||
.templateId("ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY")
|
||||
@ -21,9 +21,9 @@ public class WxMpTemplateMessageTest {
|
||||
.url("http://weixin.qq.com/download")
|
||||
.build();
|
||||
|
||||
tm.addWxMpTemplateData(
|
||||
tm.addData(
|
||||
new WxMpTemplateData("first", "haahah", "#FF00FF"));
|
||||
tm.addWxMpTemplateData(
|
||||
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\"}}}");
|
||||
|
Loading…
Reference in New Issue
Block a user