Skip to content

Commit 2cb8331

Browse files
committed
🎨 【企业微信】重构规范化模板卡片消息部分字段命名,并补充card_image的支持
1 parent 1df2dda commit 2cb8331

File tree

3 files changed

+226
-205
lines changed

3 files changed

+226
-205
lines changed

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

Lines changed: 80 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -71,72 +71,70 @@ public class WxCpMessage implements Serializable {
7171
private String taskId;
7272
private List<TaskCardButton> taskButtons = new ArrayList<>();
7373

74-
/**
75-
* 模板型卡片特有属性
76-
*/
74+
// 模板型卡片特有属性
7775
/**
7876
* 模板卡片类型,文本通知型卡片填写 “text_notice”,
7977
* 图文展示型卡片此处填写 “news_notice”,
8078
* 按钮交互型卡片填写”button_interaction”,
8179
* 投票选择型卡片填写”vote_interaction”,
8280
* 多项选择型卡片填写 “multiple_interaction”
8381
*/
84-
private String card_type;
82+
private String cardType;
8583

8684
/**
8785
* 卡片来源样式信息,不需要来源样式可不填写
8886
* 来源图片的url
8987
*/
90-
private String source_icon_url;
88+
private String sourceIconUrl;
9189
/**
9290
* 卡片来源样式信息,不需要来源样式可不填写
9391
* 来源图片的描述,建议不超过20个字
9492
*/
95-
private String source_desc;
93+
private String sourceDesc;
9694

9795
/**
9896
* 一级标题,建议不超过36个字
9997
*/
100-
private String main_title_title;
98+
private String mainTitleTitle;
10199
/**
102100
* 标题辅助信息,建议不超过44个字
103101
*/
104-
private String main_title_desc;
102+
private String mainTitleDesc;
105103

106104
/**
107105
* 图文展示型的卡片必须有图片字段。
108106
* 图片的url.
109107
*/
110-
private String card_image_url;
108+
private String cardImageUrl;
111109

112110
/**
113111
* 图片的宽高比,宽高比要小于2.25,大于1.3,不填该参数默认1.3
114112
*/
115-
private Float card_image_aspect_ratio;
113+
private Float cardImageAspectRatio;
116114
/**
117115
* 关键数据样式
118116
* 关键数据样式的数据内容,建议不超过14个字
119117
*/
120-
private String emphasis_content_title;
118+
private String emphasisContentTitle;
121119
/**
122120
* 关键数据样式的数据描述内容,建议不超过22个字
123121
*/
124-
private String emphasis_content_desc;
122+
private String emphasisContentDesc;
125123

126124
/**
127125
* 二级普通文本,建议不超过160个字
128126
*/
129-
private String sub_title_text;
127+
private String subTitleText;
130128

131129
/**
132130
* 卡片二级垂直内容,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过4
133131
*/
134-
private List<VerticalContent> vertical_contents;
132+
private List<VerticalContent> verticalContents;
135133

136134
/**
137135
* 二级标题+文本列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6
138136
*/
139-
private List<HorizontalContent> horizontal_contents;
137+
private List<HorizontalContent> horizontalContents;
140138

141139
/**
142140
* 跳转指引样式的列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过3
@@ -147,21 +145,21 @@ public class WxCpMessage implements Serializable {
147145
* 整体卡片的点击跳转事件,text_notice必填本字段
148146
* 跳转事件类型,1 代表跳转url,2 代表打开小程序。text_notice卡片模版中该字段取值范围为[1,2]
149147
*/
150-
private Integer card_action_type;
148+
private Integer cardActionType;
151149
/**
152150
* 跳转事件的url,card_action.type是1时必填
153151
*/
154-
private String card_action_url;
152+
private String cardActionUrl;
155153

156154
/**
157155
* 跳转事件的小程序的appid,必须是与当前应用关联的小程序,card_action.type是2时必填
158156
*/
159-
private String card_action_appid;
157+
private String cardActionAppid;
160158

161159
/**
162160
* 跳转事件的小程序的pagepath,card_action.type是2时选填
163161
*/
164-
private String card_action_pagepath;
162+
private String cardActionPagepath;
165163

166164
/**
167165
* 按钮交互型卡片需指定。
@@ -173,12 +171,12 @@ public class WxCpMessage implements Serializable {
173171
* 投票选择型卡片需要指定
174172
* 选择题key值,用户提交选项后,会产生回调事件,回调事件会带上该key值表示该题,最长支持1024字节
175173
*/
176-
private String checkbox_question_key;
174+
private String checkboxQuestionKey;
177175

178176
/**
179177
* 选择题模式,单选:0,多选:1,不填默认0
180178
*/
181-
private Integer checkbox_mode;
179+
private Integer checkboxMode;
182180

183181
/**
184182
* 选项list,选项个数不超过 20 个,最少1个
@@ -189,11 +187,11 @@ public class WxCpMessage implements Serializable {
189187
* 提交按钮样式
190188
* 按钮文案,建议不超过10个字,不填默认为提交
191189
*/
192-
private String submit_button_text;
190+
private String submitButtonText;
193191
/**
194192
* 提交按钮的key,会产生回调事件将本参数作为EventKey返回,最长支持1024字节
195193
*/
196-
private String submit_button_key;
194+
private String submitButtonKey;
197195
/**
198196
* 下拉式的选择器列表,multiple_interaction类型的卡片该字段不可为空,一个消息最多支持 3 个选择器
199197
*/
@@ -472,72 +470,83 @@ private void handleMsgType(JsonObject messageJson) {
472470
}
473471
case TEMPLATE_CARD: {
474472
JsonObject template = new JsonObject();
475-
template.addProperty("card_type", this.getCard_type());
473+
template.addProperty("card_type", this.getCardType());
476474

477-
if (StringUtils.isNotBlank(this.getSource_icon_url()) || StringUtils.isNotBlank(this.getSource_desc())) {
475+
if (StringUtils.isNotBlank(this.getSourceIconUrl()) || StringUtils.isNotBlank(this.getSourceDesc())) {
478476
JsonObject source = new JsonObject();
479-
if (StringUtils.isNotBlank(this.getSource_icon_url())) {
480-
source.addProperty("icon_url", this.getSource_icon_url());
477+
if (StringUtils.isNotBlank(this.getSourceIconUrl())) {
478+
source.addProperty("icon_url", this.getSourceIconUrl());
481479
}
482-
if (StringUtils.isNotBlank(this.getSource_desc())) {
483-
source.addProperty("desc", this.getSource_desc());
480+
if (StringUtils.isNotBlank(this.getSourceDesc())) {
481+
source.addProperty("desc", this.getSourceDesc());
484482
}
485483
template.add("source", source);
486484
}
487485

488-
if (StringUtils.isNotBlank(this.getMain_title_title()) || StringUtils.isNotBlank(this.getMain_title_desc())) {
489-
JsonObject main_title = new JsonObject();
490-
if (StringUtils.isNotBlank(this.getMain_title_title())) {
491-
main_title.addProperty("title", this.getMain_title_title());
486+
if (StringUtils.isNotBlank(this.getMainTitleTitle()) || StringUtils.isNotBlank(this.getMainTitleDesc())) {
487+
JsonObject mainTitle = new JsonObject();
488+
if (StringUtils.isNotBlank(this.getMainTitleTitle())) {
489+
mainTitle.addProperty("title", this.getMainTitleTitle());
490+
}
491+
if (StringUtils.isNotBlank(this.getMainTitleDesc())) {
492+
mainTitle.addProperty("desc", this.getMainTitleDesc());
493+
}
494+
template.add("main_title", mainTitle);
495+
}
496+
497+
if (StringUtils.isNotBlank(this.getCardImageUrl()) || this.getCardImageAspectRatio() != null) {
498+
JsonObject cardImage = new JsonObject();
499+
if (StringUtils.isNotBlank(this.getCardImageUrl())) {
500+
cardImage.addProperty("url", this.getCardImageUrl());
492501
}
493-
if (StringUtils.isNotBlank(this.getMain_title_desc())) {
494-
main_title.addProperty("desc", this.getMain_title_desc());
502+
if (null != this.getCardImageAspectRatio()) {
503+
cardImage.addProperty("aspect_ratio", this.getCardImageAspectRatio());
495504
}
496-
template.add("main_title", main_title);
505+
template.add("card_image", cardImage);
497506
}
498507

499-
if (StringUtils.isNotBlank(this.getEmphasis_content_title()) || StringUtils.isNotBlank(this.getEmphasis_content_desc())) {
500-
JsonObject emphasis_content = new JsonObject();
501-
if (StringUtils.isNotBlank(this.getEmphasis_content_title())) {
502-
emphasis_content.addProperty("title", this.getEmphasis_content_title());
508+
if (StringUtils.isNotBlank(this.getEmphasisContentTitle()) || StringUtils.isNotBlank(this.getEmphasisContentDesc())) {
509+
JsonObject emphasisContent = new JsonObject();
510+
if (StringUtils.isNotBlank(this.getEmphasisContentTitle())) {
511+
emphasisContent.addProperty("title", this.getEmphasisContentTitle());
503512
}
504-
if (StringUtils.isNotBlank(this.getEmphasis_content_desc())) {
505-
emphasis_content.addProperty("desc", this.getEmphasis_content_desc());
513+
if (StringUtils.isNotBlank(this.getEmphasisContentDesc())) {
514+
emphasisContent.addProperty("desc", this.getEmphasisContentDesc());
506515
}
507-
template.add("emphasis_content", emphasis_content);
516+
template.add("emphasis_content", emphasisContent);
508517
}
509518

510519

511-
if (StringUtils.isNotBlank(this.getSub_title_text())) {
512-
template.addProperty("sub_title_text", this.getSub_title_text());
520+
if (StringUtils.isNotBlank(this.getSubTitleText())) {
521+
template.addProperty("sub_title_text", this.getSubTitleText());
513522
}
514523

515524
if (StringUtils.isNotBlank(this.getTaskId())) {
516525
template.addProperty("task_id", this.getTaskId());
517526
}
518527

519-
List<VerticalContent> verticalContents = this.getVertical_contents();
520-
if (null != verticalContents && verticalContents.size() > 0) {
528+
List<VerticalContent> verticalContents = this.getVerticalContents();
529+
if (null != verticalContents && !verticalContents.isEmpty()) {
521530
JsonArray vContentJsonArray = new JsonArray();
522-
for (VerticalContent vContent : this.getVertical_contents()) {
531+
for (VerticalContent vContent : this.getVerticalContents()) {
523532
JsonObject tempObject = vContent.toJson();
524533
vContentJsonArray.add(tempObject);
525534
}
526535
template.add("vertical_content_list", vContentJsonArray);
527536
}
528537

529-
List<HorizontalContent> horizontalContents = this.getHorizontal_contents();
530-
if (null != horizontalContents && horizontalContents.size() > 0) {
538+
List<HorizontalContent> horizontalContents = this.getHorizontalContents();
539+
if (null != horizontalContents && !horizontalContents.isEmpty()) {
531540
JsonArray hContentJsonArray = new JsonArray();
532-
for (HorizontalContent hContent : this.getHorizontal_contents()) {
541+
for (HorizontalContent hContent : this.getHorizontalContents()) {
533542
JsonObject tempObject = hContent.toJson();
534543
hContentJsonArray.add(tempObject);
535544
}
536545
template.add("horizontal_content_list", hContentJsonArray);
537546
}
538547

539548
List<TemplateCardJump> jumps = this.getJumps();
540-
if (null != jumps && jumps.size() > 0) {
549+
if (null != jumps && !jumps.isEmpty()) {
541550
JsonArray jumpJsonArray = new JsonArray();
542551
for (TemplateCardJump jump : this.getJumps()) {
543552
JsonObject tempObject = jump.toJson();
@@ -546,23 +555,23 @@ private void handleMsgType(JsonObject messageJson) {
546555
template.add("jump_list", jumpJsonArray);
547556
}
548557

549-
if (null != this.getCard_action_type()) {
558+
if (null != this.getCardActionType()) {
550559
JsonObject cardAction = new JsonObject();
551-
cardAction.addProperty("type", this.getCard_action_type());
552-
if (StringUtils.isNotBlank(this.getCard_action_url())) {
553-
cardAction.addProperty("url", this.getCard_action_url());
560+
cardAction.addProperty("type", this.getCardActionType());
561+
if (StringUtils.isNotBlank(this.getCardActionUrl())) {
562+
cardAction.addProperty("url", this.getCardActionUrl());
554563
}
555-
if (StringUtils.isNotBlank(this.getCard_action_appid())) {
556-
cardAction.addProperty("appid", this.getCard_action_appid());
564+
if (StringUtils.isNotBlank(this.getCardActionAppid())) {
565+
cardAction.addProperty("appid", this.getCardActionAppid());
557566
}
558-
if (StringUtils.isNotBlank(this.getCard_action_pagepath())) {
559-
cardAction.addProperty("pagepath", this.getCard_action_pagepath());
567+
if (StringUtils.isNotBlank(this.getCardActionPagepath())) {
568+
cardAction.addProperty("pagepath", this.getCardActionPagepath());
560569
}
561570
template.add("card_action", cardAction);
562571
}
563572

564573
List<TemplateCardButton> buttons = this.getButtons();
565-
if (null != buttons && buttons.size() > 0) {
574+
if (null != buttons && !buttons.isEmpty()) {
566575
JsonArray btnJsonArray = new JsonArray();
567576
for (TemplateCardButton btn : this.getButtons()) {
568577
JsonObject tempObject = btn.toJson();
@@ -572,11 +581,11 @@ private void handleMsgType(JsonObject messageJson) {
572581
}
573582

574583
// checkbox
575-
if (StringUtils.isNotBlank(this.getCheckbox_question_key())) {
584+
if (StringUtils.isNotBlank(this.getCheckboxQuestionKey())) {
576585
JsonObject checkBox = new JsonObject();
577-
checkBox.addProperty("question_key", this.getCheckbox_question_key());
578-
if (null != this.getCheckbox_mode()) {
579-
checkBox.addProperty("mode", this.getCheckbox_mode());
586+
checkBox.addProperty("question_key", this.getCheckboxQuestionKey());
587+
if (null != this.getCheckboxMode()) {
588+
checkBox.addProperty("mode", this.getCheckboxMode());
580589
}
581590
JsonArray optionArray = new JsonArray();
582591
for (CheckboxOption option : this.getOptions()) {
@@ -589,20 +598,20 @@ private void handleMsgType(JsonObject messageJson) {
589598
}
590599

591600
// submit_button
592-
if (StringUtils.isNotBlank(this.getSubmit_button_text()) || StringUtils.isNotBlank(this.getSubmit_button_key())) {
601+
if (StringUtils.isNotBlank(this.getSubmitButtonText()) || StringUtils.isNotBlank(this.getSubmitButtonKey())) {
593602
JsonObject submit_button = new JsonObject();
594-
if (StringUtils.isNotBlank(this.getSubmit_button_text())) {
595-
submit_button.addProperty("text", this.getSubmit_button_text());
603+
if (StringUtils.isNotBlank(this.getSubmitButtonText())) {
604+
submit_button.addProperty("text", this.getSubmitButtonText());
596605
}
597-
if (StringUtils.isNotBlank(this.getSubmit_button_key())) {
598-
submit_button.addProperty("key", this.getSubmit_button_key());
606+
if (StringUtils.isNotBlank(this.getSubmitButtonKey())) {
607+
submit_button.addProperty("key", this.getSubmitButtonKey());
599608
}
600609
template.add("submit_button", submit_button);
601610
}
602611

603612
// select_list
604613
List<MultipleSelect> selects = this.getSelects();
605-
if (null != selects && selects.size() > 0) {
614+
if (null != selects && !selects.isEmpty()) {
606615
JsonArray selectJsonArray = new JsonArray();
607616
for (MultipleSelect select : this.getSelects()) {
608617
JsonObject tempObject = select.toJson();

0 commit comments

Comments
 (0)