From 1c4fd3c9ea06c01b81425b908c3b042a9a1f35f0 Mon Sep 17 00:00:00 2001 From: 3y Date: Tue, 15 Mar 2022 21:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=EF=BC=88=E6=9C=AA=E5=AE=8C=E6=88=90?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/EnterpriseWeChatContentModel.java | 9 ++++ .../austin/common/enums/ChannelType.java | 1 + austin-handler/pom.xml | 7 +++ .../handler/impl/EnterpriseWeChatHandler.java | 47 +++++++++++++++++++ .../handler/impl/OfficialAccountHandler.java | 6 +-- .../script/EnterpriseWeChatService.java | 22 +++++++++ ...cript.java => OfficialAccountService.java} | 2 +- .../impl/EnterpriseWeChatServiceImpl.java | 45 ++++++++++++++++++ ...t.java => OfficialAccountServiceImpl.java} | 4 +- pom.xml | 9 +++- 10 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 austin-common/src/main/java/com/java3y/austin/common/dto/EnterpriseWeChatContentModel.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/EnterpriseWeChatHandler.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/script/EnterpriseWeChatService.java rename austin-handler/src/main/java/com/java3y/austin/handler/script/{OfficialAccountScript.java => OfficialAccountService.java} (90%) create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/script/impl/EnterpriseWeChatServiceImpl.java rename austin-handler/src/main/java/com/java3y/austin/handler/script/impl/{WxMpTemplateScript.java => OfficialAccountServiceImpl.java} (92%) 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 new file mode 100644 index 0000000..5d83c4d --- /dev/null +++ b/austin-common/src/main/java/com/java3y/austin/common/dto/EnterpriseWeChatContentModel.java @@ -0,0 +1,9 @@ +package com.java3y.austin.common.dto; + +/** + * @author 3y + * 企业微信 + */ +public class EnterpriseWeChatContentModel extends ContentModel { + +} 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 4735ad6..ac08213 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,6 +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"), ; /** diff --git a/austin-handler/pom.xml b/austin-handler/pom.xml index 0c20ac7..6fdac1a 100644 --- a/austin-handler/pom.xml +++ b/austin-handler/pom.xml @@ -46,5 +46,12 @@ com.github.binarywang weixin-java-mp + + + + com.github.binarywang + weixin-java-cp + + \ No newline at end of file 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 new file mode 100644 index 0000000..143a2e4 --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/EnterpriseWeChatHandler.java @@ -0,0 +1,47 @@ +package com.java3y.austin.handler.handler.impl; + +import com.alibaba.fastjson.JSON; +import com.google.common.base.Throwables; +import com.java3y.austin.common.domain.TaskInfo; +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 lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.cp.bean.message.WxCpMessage; +import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @author 3y + * 企业微信推送处理 + */ +@Component +@Slf4j +public class EnterpriseWeChatHandler extends BaseHandler implements Handler { + + @Autowired + private EnterpriseWeChatService enterpriseWeChatService; + + + public EnterpriseWeChatHandler() { + channelCode = ChannelType.ENTERPRISE_WE_CHAT.getCode(); + } + + @Override + public boolean handler(TaskInfo taskInfo) { + WxCpMessage wxCpMessage = new WxCpMessage(); + + try { + WxCpMessageSendResult result = enterpriseWeChatService.send(wxCpMessage); + return true; + } catch (Exception e) { + log.error("EnterpriseWeChatHandler#handler fail:{},params:{}", + Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo)); + } + return false; + } + +} + diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/OfficialAccountHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/OfficialAccountHandler.java index 3554c46..5a2632e 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/OfficialAccountHandler.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/OfficialAccountHandler.java @@ -7,7 +7,7 @@ import com.java3y.austin.common.dto.OfficialAccountsContentModel; 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.OfficialAccountScript; +import com.java3y.austin.handler.script.OfficialAccountService; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; @@ -28,7 +28,7 @@ import java.util.Set; public class OfficialAccountHandler extends BaseHandler implements Handler { @Autowired - private OfficialAccountScript officialAccountScript; + private OfficialAccountService officialAccountService; public OfficialAccountHandler() { @@ -41,7 +41,7 @@ public class OfficialAccountHandler extends BaseHandler implements Handler { List mpTemplateMessages = buildTemplateMsg(taskInfo); // 微信模板消息需要记录响应结果 try { - List messageIds = officialAccountScript.send(mpTemplateMessages); + List messageIds = officialAccountService.send(mpTemplateMessages); log.info("OfficialAccountHandler#handler successfully messageIds:{}", messageIds); return true; } catch (Exception e) { 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 new file mode 100644 index 0000000..14f6e9f --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/script/EnterpriseWeChatService.java @@ -0,0 +1,22 @@ +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/OfficialAccountScript.java b/austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountService.java similarity index 90% rename from austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountScript.java rename to austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountService.java index 47f0762..868e76f 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountScript.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountService.java @@ -7,7 +7,7 @@ import java.util.List; /** * @author zyg */ -public interface OfficialAccountScript { +public interface OfficialAccountService { /** * 发送模板消息 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 new file mode 100644 index 0000000..6931090 --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/EnterpriseWeChatServiceImpl.java @@ -0,0 +1,45 @@ +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-handler/src/main/java/com/java3y/austin/handler/script/impl/WxMpTemplateScript.java b/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/OfficialAccountServiceImpl.java similarity index 92% rename from austin-handler/src/main/java/com/java3y/austin/handler/script/impl/WxMpTemplateScript.java rename to austin-handler/src/main/java/com/java3y/austin/handler/script/impl/OfficialAccountServiceImpl.java index 5a86122..fe69c4b 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/WxMpTemplateScript.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/script/impl/OfficialAccountServiceImpl.java @@ -1,6 +1,6 @@ package com.java3y.austin.handler.script.impl; -import com.java3y.austin.handler.script.OfficialAccountScript; +import com.java3y.austin.handler.script.OfficialAccountService; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; @@ -17,7 +17,7 @@ import java.util.List; */ @Service @Slf4j -public class WxMpTemplateScript implements OfficialAccountScript { +public class OfficialAccountServiceImpl implements OfficialAccountService { @Value("${wx.mp.account.appid}") private String appId; diff --git a/pom.xml b/pom.xml index 0e3f6a0..2f2083e 100644 --- a/pom.xml +++ b/pom.xml @@ -153,7 +153,7 @@ ${flink.version} - + com.github.binarywang weixin-java-mp @@ -167,6 +167,13 @@ 1.0.2 + + + com.github.binarywang + weixin-java-cp + ${weixin-java-mp} + +