From b57d512a703bdec0277ff6d6d343fe91e77fbecc Mon Sep 17 00:00:00 2001 From: 3y Date: Wed, 16 Mar 2022 22:13:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=20=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=EF=BC=88=E6=96=87=E6=A1=88=EF=BC=89=20?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/EnterpriseWeChatContentModel.java | 29 +++++++ .../austin/common/enums/ChannelType.java | 2 +- .../java3y/austin/common/enums/IdType.java | 6 +- .../common/enums/WechatMessageType.java | 32 ++++++++ .../handler/impl/EnterpriseWeChatHandler.java | 76 +++++++++++++++++-- .../script/EnterpriseWeChatService.java | 22 ------ .../impl/EnterpriseWeChatServiceImpl.java | 45 ----------- .../austin/support/utils/AccountUtils.java | 1 + sql/austin.sql | 4 +- 9 files changed, 139 insertions(+), 78 deletions(-) create mode 100644 austin-common/src/main/java/com/java3y/austin/common/enums/WechatMessageType.java delete mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/script/EnterpriseWeChatService.java delete mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/script/impl/EnterpriseWeChatServiceImpl.java diff --git a/austin-common/src/main/java/com/java3y/austin/common/dto/EnterpriseWeChatContentModel.java b/austin-common/src/main/java/com/java3y/austin/common/dto/EnterpriseWeChatContentModel.java index 5d83c4d..28d9e39 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/dto/EnterpriseWeChatContentModel.java +++ b/austin-common/src/main/java/com/java3y/austin/common/dto/EnterpriseWeChatContentModel.java @@ -1,9 +1,38 @@ package com.java3y.austin.common.dto; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + /** * @author 3y * 企业微信 */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor public class EnterpriseWeChatContentModel extends ContentModel { + /** + * 下发企业微信消息的类型 + */ + private String messageType; + + /** + * 文本消息 - 文案 + */ + private String content; + + /** + * 图片媒体文件id + */ + private String mediaId; + + /** + * 其他消息类型: https://developer.work.weixin.qq.com/document/path/90372#%E6%96%87%E6%9C%AC%E6%B6%88%E6%81%AF + */ + + } diff --git a/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java b/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java index ac08213..98433ba 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java +++ b/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java @@ -23,7 +23,7 @@ public enum ChannelType { EMAIL(40, "email(邮件)", EmailContentModel.class, "email"), OFFICIAL_ACCOUNT(50, "OfficialAccounts(服务号)", OfficialAccountsContentModel.class, "official_accounts"), MINI_PROGRAM(60, "miniProgram(小程序)", MiniProgramContentModel.class, "mini_program"), - ENTERPRISE_WE_CHAT(70, "EnterpriseWeChat(企业微信)", MiniProgramContentModel.class, "enterprise_we_chat"), + ENTERPRISE_WE_CHAT(70, "EnterpriseWeChat(企业微信)", EnterpriseWeChatContentModel.class, "enterprise_we_chat"), ; /** diff --git a/austin-common/src/main/java/com/java3y/austin/common/enums/IdType.java b/austin-common/src/main/java/com/java3y/austin/common/enums/IdType.java index 64e1d2d..1e96710 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/enums/IdType.java +++ b/austin-common/src/main/java/com/java3y/austin/common/enums/IdType.java @@ -7,6 +7,7 @@ import lombok.ToString; /** * 发送ID类型枚举 + * * @author 3y */ @Getter @@ -17,8 +18,9 @@ public enum IdType { DID(20, "did"), PHONE(30, "phone"), OPEN_ID(40, "openId"), - EMAIL(50, "email"); - + EMAIL(50, "email"), + ENTERPRISE_USER_ID(60, "enterprise_user_id"), + ; private Integer code; private String description; diff --git a/austin-common/src/main/java/com/java3y/austin/common/enums/WechatMessageType.java b/austin-common/src/main/java/com/java3y/austin/common/enums/WechatMessageType.java new file mode 100644 index 0000000..94d1aae --- /dev/null +++ b/austin-common/src/main/java/com/java3y/austin/common/enums/WechatMessageType.java @@ -0,0 +1,32 @@ +package com.java3y.austin.common.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; + +/** + * 微信下发消息类型枚举 + * + * @author 3y + */ +@Getter +@ToString +@AllArgsConstructor +public enum WechatMessageType { + + TEST(10, "文本"), + VOICE(20, "语音"), + VIDEO(30, "视频"), + NEWS(40, "图文"), + TEXT_CARD(50, "文本卡片"), + FILE(60, "文件"), + MINI_PROGRAM_NOTICE(70, "小程序通知"), + MARKDOWN(80, "markdown"), + TEMPLATE_CARD(90, "模板卡片"), + IMAGE(100, "图片"), + ; + + private Integer code; + private String description; + +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/EnterpriseWeChatHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/EnterpriseWeChatHandler.java index 143a2e4..eb98ca1 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/EnterpriseWeChatHandler.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/EnterpriseWeChatHandler.java @@ -1,15 +1,22 @@ package com.java3y.austin.handler.handler.impl; +import cn.hutool.core.collection.CollUtil; import com.alibaba.fastjson.JSON; import com.google.common.base.Throwables; import com.java3y.austin.common.domain.TaskInfo; +import com.java3y.austin.common.dto.EnterpriseWeChatContentModel; import com.java3y.austin.common.enums.ChannelType; import com.java3y.austin.handler.handler.BaseHandler; import com.java3y.austin.handler.handler.Handler; -import com.java3y.austin.handler.script.EnterpriseWeChatService; +import com.java3y.austin.support.utils.AccountUtils; import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl; +import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.bean.message.WxCpMessage; import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -21,9 +28,20 @@ import org.springframework.stereotype.Component; @Slf4j public class EnterpriseWeChatHandler extends BaseHandler implements Handler { - @Autowired - private EnterpriseWeChatService enterpriseWeChatService; + /** + * 构建WxCpMessage时需要用的常量 + */ + private static final String ALL = "@all"; + private static final String DELIMITER = "|"; + + /** + * 账号信息 + */ + private static final String ENTERPRISE_WECHAT_ACCOUNT_KEY = "enterpriseWechatAccount"; + private static final String PREFIX = "enterprise_wechat_"; + @Autowired + private AccountUtils accountUtils; public EnterpriseWeChatHandler() { channelCode = ChannelType.ENTERPRISE_WE_CHAT.getCode(); @@ -31,10 +49,11 @@ public class EnterpriseWeChatHandler extends BaseHandler implements Handler { @Override public boolean handler(TaskInfo taskInfo) { - WxCpMessage wxCpMessage = new WxCpMessage(); - try { - WxCpMessageSendResult result = enterpriseWeChatService.send(wxCpMessage); + WxCpDefaultConfigImpl accountConfig = accountUtils.getAccount(taskInfo.getSendAccount(), ENTERPRISE_WECHAT_ACCOUNT_KEY, PREFIX, new WxCpDefaultConfigImpl()); + WxCpMessageServiceImpl messageService = new WxCpMessageServiceImpl(initService(accountConfig)); + WxCpMessageSendResult result = messageService.send(buildWxCpMessage(taskInfo, accountConfig.getAgentId())); + buildAnchorState(result); return true; } catch (Exception e) { log.error("EnterpriseWeChatHandler#handler fail:{},params:{}", @@ -43,5 +62,50 @@ public class EnterpriseWeChatHandler extends BaseHandler implements Handler { return false; } + /** + * 打点相关的信息记录 + * + * @param result + */ + private void buildAnchorState(WxCpMessageSendResult result) { + + } + + + /** + * 初始化 WxCpServiceImpl 服务接口 + * + * @param config + * @return + */ + private WxCpService initService(WxCpDefaultConfigImpl config) { + WxCpServiceImpl wxCpService = new WxCpServiceImpl(); + wxCpService.setWxCpConfigStorage(config); + return wxCpService; + } + + /** + * 构建企业微信下发消息的对象 + * + * @param taskInfo + * @param agentId 应用ID + * @return + */ + private WxCpMessage buildWxCpMessage(TaskInfo taskInfo, Integer agentId) { + String userId; + if (ALL.equals(CollUtil.getFirst(taskInfo.getReceiver()))) { + userId = CollUtil.getFirst(taskInfo.getReceiver()); + } else { + userId = StringUtils.join(taskInfo.getReceiver(), DELIMITER); + } + EnterpriseWeChatContentModel enterpriseWeChatContentModel = (EnterpriseWeChatContentModel) taskInfo.getContentModel(); + return WxCpMessage + .TEXT() + .agentId(agentId) + .toUser(userId) + .content(enterpriseWeChatContentModel.getContent()) + .build(); + } + } diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/script/EnterpriseWeChatService.java b/austin-handler/src/main/java/com/java3y/austin/handler/script/EnterpriseWeChatService.java deleted file mode 100644 index 14f6e9f..0000000 --- a/austin-handler/src/main/java/com/java3y/austin/handler/script/EnterpriseWeChatService.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.java3y.austin.handler.script; - -import me.chanjar.weixin.common.error.WxErrorException; -import me.chanjar.weixin.cp.bean.message.WxCpMessage; -import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult; - -/** - * 企业微信推送 - * - * @author 3y - */ -public interface EnterpriseWeChatService { - - /** - * 发送消息(目前只支持userId/@all) - * - * @param wxCpMessage - * @return - * @throws WxErrorException - */ - WxCpMessageSendResult send(WxCpMessage wxCpMessage) throws WxErrorException; -} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/EnterpriseWeChatServiceImpl.java b/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/EnterpriseWeChatServiceImpl.java deleted file mode 100644 index 6931090..0000000 --- a/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/EnterpriseWeChatServiceImpl.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.java3y.austin.handler.script.impl; - -import com.java3y.austin.handler.script.EnterpriseWeChatService; -import lombok.extern.slf4j.Slf4j; -import me.chanjar.weixin.common.error.WxErrorException; -import me.chanjar.weixin.cp.api.WxCpService; -import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl; -import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; -import me.chanjar.weixin.cp.bean.message.WxCpMessage; -import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult; -import me.chanjar.weixin.cp.config.WxCpConfigStorage; -import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; -import org.springframework.stereotype.Service; - -/** - * @author 3y - * @date 2022/3/15 - */ -@Slf4j -@Service -public class EnterpriseWeChatServiceImpl implements EnterpriseWeChatService { - - @Override - public WxCpMessageSendResult send(WxCpMessage wxCpMessage) throws WxErrorException { - WxCpMessageServiceImpl messageService = new WxCpMessageServiceImpl(initService()); - - return messageService.send(wxCpMessage); - } - - private WxCpService initService() { - WxCpServiceImpl wxCpService = new WxCpServiceImpl(); - wxCpService.setWxCpConfigStorage(initConfig()); - return wxCpService; - } - - private WxCpConfigStorage initConfig() { - WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); - config.setCorpId(""); - config.setCorpSecret(""); - config.setAgentId(1); - config.setToken(""); - config.setAesKey(""); - return config; - } -} diff --git a/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java b/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java index b8f96dd..a463614 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java +++ b/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java @@ -23,6 +23,7 @@ public class AccountUtils { /** * (key:smsAccount)短信参数示例:[{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDhDUUDfffffMEqBF1WljQq","secretKey":"B4h39yWnfffff7D2btue7JErDJ8gxyi","smsSdkAppId":"140025","templateId":"11897","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}}] * (key:emailAccount)邮件参数示例:[{"email_10":{"host":"smtp.qq.com","port":465,"user":"403686131@qq.com","pass":"","from":"403686131@qq.com"}}] + * (key:enterpriseWechatAccount)企业微信参数示例:[{"enterprise_wechat_10":{"corpId":"wwf87603333e00069c","corpSecret":"-IFWxS2222QxzPIorNVUQn144444D915DM","agentId":10044442,"token":"rXROB3333Kf6i","aesKey":"MKZtoFxHIM44444M7ieag3r9ZPUsl"}}] */ public T getAccount(Integer sendAccount, String apolloKey, String prefix, T t) { String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY); diff --git a/sql/austin.sql b/sql/austin.sql index 5cc5723..638e4cb 100644 --- a/sql/austin.sql +++ b/sql/austin.sql @@ -13,8 +13,8 @@ CREATE TABLE `message_template` `cron_task_id` bigint(20) COMMENT '定时任务Id (xxl-job-admin返回)', `cron_crowd_path` varchar(500) COMMENT '定时发送人群的文件路径', `expect_push_time` varchar(100) COLLATE utf8mb4_unicode_ci COMMENT '期望发送时间:0:立即发送 定时任务以及周期任务:cron表达式', - `id_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息的发送ID类型:10. userId 20.did 30.手机号 40.openId 50.email', - `send_channel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息发送渠道:10.IM 20.Push 30.短信 40.Email 50.公众号 60.小程序', + `id_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息的发送ID类型:10. userId 20.did 30.手机号 40.openId 50.email 60.企业微信userId', + `send_channel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息发送渠道:10.IM 20.Push 30.短信 40.Email 50.公众号 60.小程序 70.企业微信', `template_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '10.运营类 20.技术类接口调用', `msg_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '10.通知类消息 20.营销类消息 30.验证码类消息', `msg_content` varchar(600) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '消息内容 占位符用{$var}表示',