🎨 #2873 【企业微信】模板卡片消息支持左图右文样式和按钮型卡片的下拉框样式(企业微信v3.1.18版本引入的新特性)

This commit is contained in:
th3ee9ine 2022-11-06 11:32:01 +08:00 committed by GitHub
parent c26b055ccf
commit c109e81842
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 271 additions and 0 deletions

View File

@ -27,26 +27,99 @@ import static me.chanjar.weixin.common.api.WxConsts.KefuMsgType.*;
public class WxCpMessage implements Serializable {
private static final long serialVersionUID = -2082278303476631708L;
/**
* 指定接收消息的成员成员ID列表多个接收者用|分隔最多支持1000个
* 特殊情况指定为"@all"则向该企业应用的全部成员发送
*/
private String toUser;
/**
* 指定接收消息的部门部门ID列表多个接收者用|分隔最多支持100个
* 当touser为"@all"时忽略本参数
*/
private String toParty;
/**
* 指定接收消息的标签标签ID列表多个接收者用|分隔最多支持100个
* 当touser为"@all"时忽略本参数
*/
private String toTag;
/**
* 企业应用的id整型企业内部开发可在应用的设置页面查看第三方服务商可通过接口 获取企业授权信息 获取该参数值
*/
private Integer agentId;
/**
* 消息类型
* 文本消息: text
* 图片消息: image
* 语音消息: voice
* 视频消息: video
* 文件消息: file
* 文本卡片消息: textcard
* 图文消息: news
* 图文消息: mpnews
* markdown消息: markdown
* 模板卡片消息: template_card
*/
private String msgType;
/**
* 消息内容最长不超过2048个字节超过将截断支持id转译
*/
private String content;
/**
* 媒体文件id可以调用上传临时素材接口获取
*/
private String mediaId;
/**
* 图文消息缩略图的media_id, 可以通过素材管理接口获得此处thumb_media_id即上传接口返回的media_id
*/
private String thumbMediaId;
/**
* 标题不超过128个字节超过会自动截断支持id转译
*/
private String title;
/**
* 描述不超过512个字节超过会自动截断支持id转译
*/
private String description;
private String musicUrl;
private String hqMusicUrl;
/**
* 表示是否是保密消息默认为0注意仅 mpnews 类型的消息支持safe值为2其他消息类型不支持
* 0表示可对外分享
* 1表示不能分享且内容显示水印
* 2表示仅限在企业内分享
*/
private String safe;
/**
* 点击后跳转的链接最长2048字节请确保包含了协议头(http/https)
*/
private String url;
/**
* 按钮文字 默认为详情 不超过4个文字超过自动截断
*/
private String btnTxt;
/**
* 图文消息一个图文消息支持1到8条图文
*/
private List<NewArticle> articles = new ArrayList<>();
/**
* 图文消息一个图文消息支持1到8条图文
*/
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
/**
* 小程序appid必须是与当前应用关联的小程序appid和pagepath必须同时填写填写后会忽略url字段
*/
private String appId;
/**
* 点击消息卡片后的小程序页面最长1024个字节仅限本小程序内的页面该字段不填则消息点击后不跳转
*/
private String page;
/**
* 是否放大第一个content_item
*/
private Boolean emphasisFirstItem;
/**
* 消息内容键值对最多允许10个item
*/
private Map<String, String> contentItems;
/**
@ -111,11 +184,17 @@ public class WxCpMessage implements Serializable {
* 一级标题建议不超过36个字
*/
private String mainTitleTitle;
/**
* 标题辅助信息建议不超过44个字
*/
private String mainTitleDesc;
/**
* 左图右文样式news_notice类型的卡片card_image image_text_area 两者必填一个字段不可都不填
*/
private TemplateCardImageTextArea imageTextArea;
/**
* 图文展示型的卡片必须有图片字段
* 图片的url.
@ -176,6 +255,12 @@ public class WxCpMessage implements Serializable {
*/
private String cardActionPagepath;
/**
* 按钮交互型卡片需指定
* button_selection
*/
private TemplateCardButtonSelection buttonSelection;
/**
* 按钮交互型卡片需指定
* 按钮列表该字段可为空数组但有数据的话需确认对应字段是否必填列表长度不超过6
@ -553,6 +638,10 @@ public class WxCpMessage implements Serializable {
template.add("main_title", mainTitle);
}
if (this.getImageTextArea() != null) {
template.add("image_text_area", this.getImageTextArea().toJson());
}
if (StringUtils.isNotBlank(this.getCardImageUrl()) || this.getCardImageAspectRatio() != null) {
JsonObject cardImage = new JsonObject();
if (StringUtils.isNotBlank(this.getCardImageUrl())) {
@ -629,6 +718,11 @@ public class WxCpMessage implements Serializable {
template.add("card_action", cardAction);
}
TemplateCardButtonSelection buttonSelection = this.getButtonSelection();
if (null != buttonSelection) {
template.add("button_selection", buttonSelection.toJson());
}
List<TemplateCardButton> buttons = this.getButtons();
if (null != buttons && !buttons.isEmpty()) {
JsonArray btnJsonArray = new JsonArray();

View File

@ -99,6 +99,11 @@ public class TemplateCardBuilder extends BaseBuilder<TemplateCardBuilder> {
*/
private List<TemplateCardJump> jumps;
/**
* 左图右文样式news_notice类型的卡片card_image image_text_area 两者必填一个字段不可都不填
*/
private TemplateCardImageTextArea imageTextArea;
/**
* 整体卡片的点击跳转事件text_notice必填本字段
* 跳转事件类型1 代表跳转url2 代表打开小程序text_notice卡片模版中该字段取值范围为[1,2]
@ -124,6 +129,12 @@ public class TemplateCardBuilder extends BaseBuilder<TemplateCardBuilder> {
*/
private String taskId;
/**
* 按钮交互型卡片需指定
* button_selection
*/
private TemplateCardButtonSelection buttonSelection;
/**
* 按钮交互型卡片需指定
* 按钮列表该字段可为空数组但有数据的话需确认对应字段是否必填列表长度不超过6
@ -350,6 +361,17 @@ public class TemplateCardBuilder extends BaseBuilder<TemplateCardBuilder> {
return this;
}
/**
* image_text_area template card builder.
*
* @param imageTextArea the card image_text_area
* @return the template card builder
*/
public TemplateCardBuilder imageTextArea(TemplateCardImageTextArea imageTextArea) {
this.imageTextArea = imageTextArea;
return this;
}
/**
* Card action type template card builder.
*
@ -505,6 +527,7 @@ public class TemplateCardBuilder extends BaseBuilder<TemplateCardBuilder> {
m.setActionMenuActionList(this.actionMenuActionList);
m.setMainTitleTitle(this.mainTitleTitle);
m.setMainTitleDesc(this.mainTitleDesc);
m.setImageTextArea(this.imageTextArea);
m.setCardImageUrl(this.cardImageUrl);
m.setCardImageAspectRatio(this.cardImageAspectRatio);
m.setEmphasisContentTitle(this.emphasisContentTitle);
@ -518,6 +541,7 @@ public class TemplateCardBuilder extends BaseBuilder<TemplateCardBuilder> {
m.setCardActionPagepath(this.cardActionPagepath);
m.setCardActionUrl(this.cardActionUrl);
m.setTaskId(this.taskId);
m.setButtonSelection(this.buttonSelection);
m.setButtons(this.buttons);
m.setCheckboxMode(this.checkboxMode);
m.setCheckboxQuestionKey(this.checkboxQuestionKey);

View File

@ -0,0 +1,58 @@
package me.chanjar.weixin.cp.bean.templatecard;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
import java.util.List;
/**
* @author chenjie03
* @version 1.0
* @date 2022/11/4 11:54
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TemplateCardButtonSelection implements Serializable {
private String questionKey;
private String title;
private String selectedId;
private List<TemplateCardButtonSelectionOption> optionList;
/**
* To json json object.
*
* @return the json object
*/
public JsonObject toJson() {
JsonObject btnObject = new JsonObject();
if (StringUtils.isNotBlank(this.questionKey)) {
btnObject.addProperty("question_key", this.questionKey);
}
if (StringUtils.isNotBlank(this.title)) {
btnObject.addProperty("title", this.title);
}
if (StringUtils.isNotBlank(this.selectedId)) {
btnObject.addProperty("selected_id", this.selectedId);
}
if (this.optionList != null && this.optionList.size() > 0) {
JsonArray optionJsonArray = new JsonArray();
for (TemplateCardButtonSelectionOption jump : this.getOptionList()) {
JsonObject tempObject = jump.toJson();
optionJsonArray.add(tempObject);
}
btnObject.add("option_list", optionJsonArray);
}
return btnObject;
}
}

View File

@ -0,0 +1,42 @@
package me.chanjar.weixin.cp.bean.templatecard;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
/**
* @author chenjie03
* @version 1.0
* @date 2022/11/4 11:57
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TemplateCardButtonSelectionOption implements Serializable {
private String id;
private String text;
/**
* To json json object.
*
* @return the json object
*/
public JsonObject toJson() {
JsonObject btnObject = new JsonObject();
if (StringUtils.isNotBlank(this.id)) {
btnObject.addProperty("id", this.id);
}
if (StringUtils.isNotBlank(this.text)) {
btnObject.addProperty("text", this.text);
}
return btnObject;
}
}

View File

@ -0,0 +1,53 @@
package me.chanjar.weixin.cp.bean.templatecard;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
/**
* @author chenjie03
* @version 1.0
* @date 2022/11/4 12:12
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TemplateCardImageTextArea implements Serializable {
private Integer type;
private String url;
private String title;
private String desc;
private String imageUrl;
/**
* To json json object.
*
* @return the json object
*/
public JsonObject toJson() {
JsonObject btnObject = new JsonObject();
if (null != this.type) {
btnObject.addProperty("type", this.type);
}
if (StringUtils.isNotBlank(this.url)) {
btnObject.addProperty("url", this.url);
}
if (StringUtils.isNotBlank(this.title)) {
btnObject.addProperty("title", this.title);
}
if (StringUtils.isNotBlank(this.desc)) {
btnObject.addProperty("desc", this.desc);
}
if (StringUtils.isNotBlank(this.imageUrl)) {
btnObject.addProperty("image_url", this.imageUrl);
}
return btnObject;
}
}