Merge branch 'jorgen-zhao-master'

pull/6/head
3y 3 years ago
commit 62d57f9485

@ -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<String, String> map;
/**
* url
*/
String url;
}

@ -41,5 +41,10 @@
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
<!--微信发送模板信息-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
</dependency>
</dependencies>
</project>

@ -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;
/**
*
* <p>
*
* 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<String, String> map;
}

@ -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<WxMpTemplateMessage> mpTemplateMessages = buildTemplateMsg(taskInfo);
// 微信模板消息需要记录响应结果
try {
List<String> 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<WxMpTemplateMessage> buildTemplateMsg(TaskInfo taskInfo) {
// 需是关注公众号的用户的OpenId
Set<String> receiver = taskInfo.getReceiver();
Long messageTemplateId = taskInfo.getMessageTemplateId();
// 微信模板消息可以关联到系统业务,通过接口查询。
String templateId = getRealWxMpTemplateId(messageTemplateId);
List<WxMpTemplateMessage> wxMpTemplateMessages = new ArrayList<>(receiver.size());
OfficialAccountsContentModel contentModel = (OfficialAccountsContentModel) taskInfo.getContentModel();
String url = contentModel.getUrl();
Map<String, String> 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;
}
/**
* idid
*
* @param messageTemplateId id
* @return
*/
private String getRealWxMpTemplateId(Long messageTemplateId) {
return String.valueOf(messageTemplateId);
}
}

@ -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<String> send(List<WxMpTemplateMessage> wxMpTemplateMessages) throws Exception;
}

@ -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<String> send(List<WxMpTemplateMessage> messages) throws Exception {
WxMpService wxMpService = initService();
List<String> 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;
}
}

@ -83,4 +83,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
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"

@ -37,6 +37,7 @@
<maven.compiler.source>${target.java.version}</maven.compiler.source>
<maven.compiler.target>${target.java.version}</maven.compiler.target>
<log4j.version>2.17.1</log4j.version>
<weixin-java-mp>4.1.0</weixin-java-mp>
</properties>
<dependencyManagement>
@ -152,6 +153,13 @@
<version>${flink.version}</version>
</dependency>
<!--微信第三方SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${weixin-java-mp}</version>
</dependency>
</dependencies>
</dependencyManagement>

Loading…
Cancel
Save