From befc83b9248f25f19b0c16f4d2dec17efd4d44a7 Mon Sep 17 00:00:00 2001 From: zyg Date: Tue, 22 Feb 2022 22:35:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BE=AE=E4=BF=A1=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=8F=B7=E5=8F=91=E9=80=81=E6=A8=A1=E6=9D=BF=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/OfficialAccountsContentModel.java | 21 +++++ austin-handler/pom.xml | 5 ++ .../handler/domain/OfficialAccountParam.java | 51 +++++++++++ .../handler/OfficialAccountHandler.java | 89 +++++++++++++++++++ .../handler/script/OfficialAccountScript.java | 18 ++++ .../handler/script/WxMpTemplateScript.java | 57 ++++++++++++ .../src/main/resources/application.properties | 8 +- pom.xml | 8 ++ 8 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/domain/OfficialAccountParam.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/handler/OfficialAccountHandler.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountScript.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/script/WxMpTemplateScript.java diff --git a/austin-common/src/main/java/com/java3y/austin/common/dto/OfficialAccountsContentModel.java b/austin-common/src/main/java/com/java3y/austin/common/dto/OfficialAccountsContentModel.java index ee3a80e..a65db9c 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/dto/OfficialAccountsContentModel.java +++ b/austin-common/src/main/java/com/java3y/austin/common/dto/OfficialAccountsContentModel.java @@ -1,8 +1,29 @@ package com.java3y.austin.common.dto; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; + /** * @author 3y */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor public class OfficialAccountsContentModel extends ContentModel { + /** + * 模板消息发送的数据 + */ + Map map; + + /** + * 模板消息跳转的url + */ + String url; + } diff --git a/austin-handler/pom.xml b/austin-handler/pom.xml index e30299d..0c20ac7 100644 --- a/austin-handler/pom.xml +++ b/austin-handler/pom.xml @@ -41,5 +41,10 @@ com.sun.mail javax.mail + + + com.github.binarywang + weixin-java-mp + \ No newline at end of file diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/domain/OfficialAccountParam.java b/austin-handler/src/main/java/com/java3y/austin/handler/domain/OfficialAccountParam.java new file mode 100644 index 0000000..61309ee --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/domain/OfficialAccountParam.java @@ -0,0 +1,51 @@ +package com.java3y.austin.handler.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; + +/** + * 模板消息参数 + *

+ * 参数示例: + * https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class OfficialAccountParam { + + /** + * 服务号关注者的openId + */ + private String openId; + + /** + * 需要使用的模板信息Id + */ + private String templateId; + + /** + * 模板消息跳转的url + */ + private String url; + + /** + * 模板消息跳转小程序的appid + */ + private String miniProgramId; + + /** + * 模板消息跳转小程序的页面路径 + */ + private String path; + + /** + * 模板消息的信息载体 + */ + private Map map; +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/handler/OfficialAccountHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/handler/OfficialAccountHandler.java new file mode 100644 index 0000000..9d7a158 --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/handler/OfficialAccountHandler.java @@ -0,0 +1,89 @@ +package com.java3y.austin.handler.handler; + +import com.alibaba.fastjson.JSON; +import com.google.common.base.Throwables; +import com.java3y.austin.common.domain.TaskInfo; +import com.java3y.austin.common.dto.OfficialAccountsContentModel; +import com.java3y.austin.common.enums.ChannelType; +import com.java3y.austin.handler.script.OfficialAccountScript; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; +import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@Component +@Slf4j +public class OfficialAccountHandler extends BaseHandler implements Handler { + + @Autowired + private OfficialAccountScript officialAccountScript; + + + public OfficialAccountHandler() { + channelCode = ChannelType.OFFICIAL_ACCOUNT.getCode(); + } + + @Override + public boolean handler(TaskInfo taskInfo) { + + List mpTemplateMessages = buildTemplateMsg(taskInfo); + // 微信模板消息需要记录响应结果 + try { + List messageIds = officialAccountScript.send(mpTemplateMessages); + log.info("OfficialAccountHandler#handler successfully messageIds:{}", messageIds); + return true; + } catch (Exception e) { + log.error("OfficialAccountHandler#handler fail:{},params:{}", + Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo)); + } + return false; + } + + /** + * 通过taskInfo构建微信模板消息 + * + * @param taskInfo + * @return + */ + private List buildTemplateMsg(TaskInfo taskInfo) { + // 需是关注公众号的用户的OpenId + Set receiver = taskInfo.getReceiver(); + Long messageTemplateId = taskInfo.getMessageTemplateId(); + // 微信模板消息可以关联到系统业务,通过接口查询。 + String templateId = getRealWxMpTemplateId(messageTemplateId); + List wxMpTemplateMessages = new ArrayList<>(receiver.size()); + OfficialAccountsContentModel contentModel = (OfficialAccountsContentModel) taskInfo.getContentModel(); + String url = contentModel.getUrl(); + Map param = contentModel.getMap(); + + // 构建微信模板消息 + for (String openId : receiver) { + WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() + .toUser(openId) + .templateId(templateId) + .url(url) + .build(); + // WxMpTemplateData 对应模板消息 键 -- 值 -- color + param.forEach((k, v) -> templateMessage.addData(new WxMpTemplateData(k, v))); + wxMpTemplateMessages.add(templateMessage); + } + return wxMpTemplateMessages; + } + + /** + * 根据模板id获取真实的模板id + * + * @param messageTemplateId 系统业务模板id + * @return + */ + private String getRealWxMpTemplateId(Long messageTemplateId) { + return String.valueOf(messageTemplateId); + } +} + 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/OfficialAccountScript.java new file mode 100644 index 0000000..9f3933a --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/script/OfficialAccountScript.java @@ -0,0 +1,18 @@ +package com.java3y.austin.handler.script; + +import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; + +import java.util.List; + +public interface OfficialAccountScript { + + /** + * 发送模板消息 + * + * @param wxMpTemplateMessages 模板消息列表 + * @return + * @throws Exception + */ + List send(List wxMpTemplateMessages) throws Exception; + +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/script/WxMpTemplateScript.java b/austin-handler/src/main/java/com/java3y/austin/handler/script/WxMpTemplateScript.java new file mode 100644 index 0000000..56d4c63 --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/script/WxMpTemplateScript.java @@ -0,0 +1,57 @@ +package com.java3y.austin.handler.script; + +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; +import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; +import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * + */ +@Service +@Slf4j +public class WxMpTemplateScript implements OfficialAccountScript { + + @Value("${wx.mp.account.appid}") + private String appId; + @Value("${wx.mp.account.secret}") + private String secret; + @Value("${wx.mp.account.token}") + private String token; + @Value("${wx.mp.account.aesKey}") + private String aesKey; + + + @Override + public List send(List messages) throws Exception { + WxMpService wxMpService = initService(); + List messageIds = new ArrayList<>(messages.size()); + for (WxMpTemplateMessage wxMpTemplateMessage : messages) { + String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage); + messageIds.add(msgId); + } + return messageIds; + } + + /** + * 初始化微信服务号 + * + * @return + */ + public WxMpService initService() { + WxMpService wxMpService = new WxMpServiceImpl(); + WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); + config.setAppId(appId); + config.setSecret(secret); + config.setToken(token); + config.setAesKey(aesKey); + wxMpService.setWxMpConfigStorage(config); + return wxMpService; + } +} diff --git a/austin-web/src/main/resources/application.properties b/austin-web/src/main/resources/application.properties index 9e42b49..10b71b3 100644 --- a/austin-web/src/main/resources/application.properties +++ b/austin-web/src/main/resources/application.properties @@ -65,4 +65,10 @@ management.endpoint.health.show-details=always management.endpoint.metrics.enabled=true management.endpoint.prometheus.enabled=true management.endpoints.web.exposure.include=* -management.metrics.export.prometheus.enabled=true \ No newline at end of file +management.metrics.export.prometheus.enabled=true + +##################### wx mp config ##################### +wx.mp.account.appid="appid" +wx.mp.account.secret="secret" +wx.mp.account.token="token" +wx.mp.account.aesKey="aesKey" diff --git a/pom.xml b/pom.xml index 48ee4a4..2c840b5 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,7 @@ ${target.java.version} ${target.java.version} 2.17.1 + 4.1.0 @@ -152,6 +153,13 @@ ${flink.version} + + + com.github.binarywang + weixin-java-mp + ${weixin-java-mp} + +