Skip to content

Commit cecdace

Browse files
authored
🆕 #2324【企业微信】添加对新的模版卡片消息的支持
1 parent b45c626 commit cecdace

File tree

10 files changed

+1236
-0
lines changed

10 files changed

+1236
-0
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,37 @@ public static class KefuMsgType {
120120
* 小程序通知消息.
121121
*/
122122
public static final String MINIPROGRAM_NOTICE = "miniprogram_notice";
123+
124+
/**
125+
* 模板卡片消息.
126+
*/
127+
public static final String TEMPLATE_CARD = "template_card";
128+
}
129+
130+
/**
131+
* 企业微信模板卡片消息的卡片类型
132+
*/
133+
public static class TemplateCardType {
134+
/**
135+
* 文本通知型卡片
136+
*/
137+
public static final String TEXT_NOTICE = "text_notice";
138+
/**
139+
* 图文展示型卡片
140+
*/
141+
public static final String NEWS_NOTICE = "news_notice";
142+
/**
143+
* 按钮交互型卡片
144+
*/
145+
public static final String BUTTON_INTERACTION = "button_interaction";
146+
/**
147+
* 投票选择型卡片
148+
*/
149+
public static final String VOTE_INTERACTION = "vote_interaction";
150+
/**
151+
* 多项选择型卡片
152+
*/
153+
public static final String MULTIPLE_INTERACTION = "multiple_interaction";
123154
}
124155

125156
/**

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpMessage.java

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import me.chanjar.weixin.cp.bean.article.NewArticle;
99
import me.chanjar.weixin.cp.bean.messagebuilder.*;
1010
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
11+
import me.chanjar.weixin.cp.bean.templatecard.*;
1112
import org.apache.commons.lang3.StringUtils;
1213

1314
import java.io.Serializable;
@@ -70,6 +71,134 @@ public class WxCpMessage implements Serializable {
7071
private String taskId;
7172
private List<TaskCardButton> taskButtons = new ArrayList<>();
7273

74+
/**
75+
* 模板型卡片特有属性
76+
*/
77+
/**
78+
* 模板卡片类型,文本通知型卡片填写 “text_notice”,
79+
* 图文展示型卡片此处填写 “news_notice”,
80+
* 按钮交互型卡片填写”button_interaction”,
81+
* 投票选择型卡片填写”vote_interaction”,
82+
* 多项选择型卡片填写 “multiple_interaction”
83+
*/
84+
private String card_type;
85+
86+
/**
87+
* 卡片来源样式信息,不需要来源样式可不填写
88+
* 来源图片的url
89+
*/
90+
private String source_icon_url;
91+
/**
92+
* 卡片来源样式信息,不需要来源样式可不填写
93+
* 来源图片的描述,建议不超过20个字
94+
*/
95+
private String source_desc;
96+
97+
/**
98+
* 一级标题,建议不超过36个字
99+
*/
100+
private String main_title_title;
101+
/**
102+
* 标题辅助信息,建议不超过44个字
103+
*/
104+
private String main_title_desc;
105+
106+
/**
107+
* 图文展示型的卡片必须有图片字段。
108+
* 图片的url.
109+
*/
110+
private String card_image_url;
111+
112+
/**
113+
* 图片的宽高比,宽高比要小于2.25,大于1.3,不填该参数默认1.3
114+
*/
115+
private Float card_image_aspect_ratio;
116+
/**
117+
* 关键数据样式
118+
* 关键数据样式的数据内容,建议不超过14个字
119+
*/
120+
private String emphasis_content_title;
121+
/**
122+
* 关键数据样式的数据描述内容,建议不超过22个字
123+
*/
124+
private String emphasis_content_desc;
125+
126+
/**
127+
* 二级普通文本,建议不超过160个字
128+
*/
129+
private String sub_title_text;
130+
131+
/**
132+
* 卡片二级垂直内容,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过4
133+
*/
134+
private List<VerticalContent> vertical_contents;
135+
136+
/**
137+
* 二级标题+文本列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6
138+
*/
139+
private List<HorizontalContent> horizontal_contents;
140+
141+
/**
142+
* 跳转指引样式的列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过3
143+
*/
144+
private List<TemplateCardJump> jumps;
145+
146+
/**
147+
* 整体卡片的点击跳转事件,text_notice必填本字段
148+
* 跳转事件类型,1 代表跳转url,2 代表打开小程序。text_notice卡片模版中该字段取值范围为[1,2]
149+
*/
150+
private Integer card_action_type;
151+
/**
152+
* 跳转事件的url,card_action.type是1时必填
153+
*/
154+
private String card_action_url;
155+
156+
/**
157+
* 跳转事件的小程序的appid,必须是与当前应用关联的小程序,card_action.type是2时必填
158+
*/
159+
private String card_action_appid;
160+
161+
/**
162+
* 跳转事件的小程序的pagepath,card_action.type是2时选填
163+
*/
164+
private String card_action_pagepath;
165+
166+
/**
167+
* 按钮交互型卡片需指定。
168+
* 按钮列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6
169+
*/
170+
private List<TemplateCardButton> buttons;
171+
172+
/**
173+
* 投票选择型卡片需要指定
174+
* 选择题key值,用户提交选项后,会产生回调事件,回调事件会带上该key值表示该题,最长支持1024字节
175+
*/
176+
private String checkbox_question_key;
177+
178+
/**
179+
* 选择题模式,单选:0,多选:1,不填默认0
180+
*/
181+
private Integer checkbox_mode;
182+
183+
/**
184+
* 选项list,选项个数不超过 20 个,最少1个
185+
*/
186+
private List<CheckboxOption> options;
187+
188+
/**
189+
* 提交按钮样式
190+
* 按钮文案,建议不超过10个字,不填默认为提交
191+
*/
192+
private String submit_button_text;
193+
/**
194+
* 提交按钮的key,会产生回调事件将本参数作为EventKey返回,最长支持1024字节
195+
*/
196+
private String submit_button_key;
197+
/**
198+
* 下拉式的选择器列表,multiple_interaction类型的卡片该字段不可为空,一个消息最多支持 3 个选择器
199+
*/
200+
private List<MultipleSelect> selects;
201+
73202
/**
74203
* 获得文本消息builder.
75204
*/
@@ -140,6 +269,13 @@ public static TaskCardBuilder TASKCARD() {
140269
return new TaskCardBuilder();
141270
}
142271

272+
/**
273+
* 获得任务卡片消息builder.
274+
*/
275+
public static TemplateCardBuilder TEMPLATECARD() {
276+
return new TemplateCardBuilder();
277+
}
278+
143279
/**
144280
* 获得小程序通知消息builder.
145281
*/
@@ -160,6 +296,7 @@ public static MiniProgramNoticeMsgBuilder newMiniProgramNoticeBuilder() {
160296
* {@link KefuMsgType#MARKDOWN}
161297
* {@link KefuMsgType#TASKCARD}
162298
* {@link KefuMsgType#MINIPROGRAM_NOTICE}
299+
* {@link KefuMsgType#TEMPLATE_CARD}
163300
* </pre>
164301
*
165302
* @param msgType 消息类型
@@ -328,6 +465,150 @@ private void handleMsgType(JsonObject messageJson) {
328465
messageJson.add("miniprogram_notice", notice);
329466
break;
330467
}
468+
case TEMPLATE_CARD: {
469+
JsonObject template = new JsonObject();
470+
template.addProperty("card_type", this.getCard_type());
471+
472+
if (StringUtils.isNotBlank(this.getSource_icon_url()) || StringUtils.isNotBlank(this.getSource_desc())) {
473+
JsonObject source = new JsonObject();
474+
if (StringUtils.isNotBlank(this.getSource_icon_url())) {
475+
source.addProperty("icon_url", this.getSource_icon_url());
476+
}
477+
if (StringUtils.isNotBlank(this.getSource_desc())) {
478+
source.addProperty("desc", this.getSource_desc());
479+
}
480+
template.add("source", source);
481+
}
482+
483+
if (StringUtils.isNotBlank(this.getMain_title_title()) || StringUtils.isNotBlank(this.getMain_title_desc())) {
484+
JsonObject main_title = new JsonObject();
485+
if (StringUtils.isNotBlank(this.getMain_title_title())) {
486+
main_title.addProperty("title", this.getMain_title_title());
487+
}
488+
if (StringUtils.isNotBlank(this.getMain_title_desc())) {
489+
main_title.addProperty("desc", this.getMain_title_desc());
490+
}
491+
template.add("main_title", main_title);
492+
}
493+
494+
if (StringUtils.isNotBlank(this.getEmphasis_content_title()) || StringUtils.isNotBlank(this.getEmphasis_content_desc())) {
495+
JsonObject emphasis_content = new JsonObject();
496+
if (StringUtils.isNotBlank(this.getEmphasis_content_title())) {
497+
emphasis_content.addProperty("title", this.getEmphasis_content_title());
498+
}
499+
if (StringUtils.isNotBlank(this.getEmphasis_content_desc())) {
500+
emphasis_content.addProperty("desc", this.getEmphasis_content_desc());
501+
}
502+
template.add("emphasis_content", emphasis_content);
503+
}
504+
505+
506+
if (StringUtils.isNotBlank(this.getSub_title_text())) {
507+
template.addProperty("sub_title_text", this.getSub_title_text());
508+
}
509+
510+
if (StringUtils.isNotBlank(this.getTaskId())) {
511+
template.addProperty("task_id", this.getTaskId());
512+
}
513+
514+
List<VerticalContent> verticalContents = this.getVertical_contents();
515+
if(null != verticalContents && verticalContents.size() > 0) {
516+
JsonArray vContentJsonArray = new JsonArray();
517+
for (VerticalContent vContent : this.getVertical_contents()) {
518+
JsonObject tempObject = vContent.toJson();
519+
vContentJsonArray.add(tempObject);
520+
}
521+
template.add("vertical_content_list", vContentJsonArray);
522+
}
523+
524+
List<HorizontalContent> horizontalContents = this.getHorizontal_contents();
525+
if(null != horizontalContents && horizontalContents.size() > 0) {
526+
JsonArray hContentJsonArray = new JsonArray();
527+
for (HorizontalContent hContent : this.getHorizontal_contents()) {
528+
JsonObject tempObject = hContent.toJson();
529+
hContentJsonArray.add(tempObject);
530+
}
531+
template.add("horizontal_content_list", hContentJsonArray);
532+
}
533+
534+
List<TemplateCardJump> jumps = this.getJumps();
535+
if(null != jumps && jumps.size() > 0) {
536+
JsonArray jumpJsonArray = new JsonArray();
537+
for (TemplateCardJump jump : this.getJumps()) {
538+
JsonObject tempObject = jump.toJson();
539+
jumpJsonArray.add(tempObject);
540+
}
541+
template.add("jump_list", jumpJsonArray);
542+
}
543+
544+
if (null != this.getCard_action_type()) {
545+
JsonObject cardAction = new JsonObject();
546+
cardAction.addProperty("type", this.getCard_action_type());
547+
if (StringUtils.isNotBlank(this.getCard_action_url())) {
548+
cardAction.addProperty("url", this.getCard_action_url());
549+
}
550+
if (StringUtils.isNotBlank(this.getCard_action_appid())) {
551+
cardAction.addProperty("appid", this.getCard_action_appid());
552+
}
553+
if (StringUtils.isNotBlank(this.getCard_action_pagepath())) {
554+
cardAction.addProperty("pagepath", this.getCard_action_pagepath());
555+
}
556+
template.add("card_action", cardAction);
557+
}
558+
559+
List<TemplateCardButton> buttons = this.getButtons();
560+
if(null != buttons && buttons.size() > 0) {
561+
JsonArray btnJsonArray = new JsonArray();
562+
for (TemplateCardButton btn : this.getButtons()) {
563+
JsonObject tempObject = btn.toJson();
564+
btnJsonArray.add(tempObject);
565+
}
566+
template.add("button_list", btnJsonArray);
567+
}
568+
569+
// checkbox
570+
if (StringUtils.isNotBlank(this.getCheckbox_question_key())) {
571+
JsonObject checkBox = new JsonObject();
572+
checkBox.addProperty("question_key", this.getCheckbox_question_key());
573+
if (null != this.getCheckbox_mode()) {
574+
checkBox.addProperty("mode", this.getCheckbox_mode());
575+
}
576+
JsonArray optionArray = new JsonArray();
577+
for (CheckboxOption option : this.getOptions()) {
578+
JsonObject tempObject = option.toJson();
579+
optionArray.add(tempObject);
580+
}
581+
checkBox.add("option_list", optionArray);
582+
583+
template.add("checkbox", checkBox);
584+
}
585+
586+
// submit_button
587+
if (StringUtils.isNotBlank(this.getSubmit_button_text()) || StringUtils.isNotBlank(this.getSubmit_button_key())) {
588+
JsonObject submit_button = new JsonObject();
589+
if (StringUtils.isNotBlank(this.getSubmit_button_text())) {
590+
submit_button.addProperty("text", this.getSubmit_button_text());
591+
}
592+
if (StringUtils.isNotBlank(this.getSubmit_button_key())) {
593+
submit_button.addProperty("key", this.getSubmit_button_key());
594+
}
595+
template.add("submit_button", submit_button);
596+
}
597+
598+
// select_list
599+
List<MultipleSelect> selects = this.getSelects();
600+
if(null != selects && selects.size() > 0) {
601+
JsonArray selectJsonArray = new JsonArray();
602+
for (MultipleSelect select : this.getSelects()) {
603+
JsonObject tempObject = select.toJson();
604+
selectJsonArray.add(tempObject);
605+
}
606+
template.add("select_list", selectJsonArray);
607+
}
608+
609+
messageJson.add("template_card", template);
610+
break;
611+
}
331612
default: {
332613
// do nothing
333614
}

0 commit comments

Comments
 (0)