mirror of https://github.com/ZhongFuCheng3y/austin
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;
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据模板id获取真实的模板id
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue