parent
17ad0282f1
commit
a66f7d6aae
@ -1,37 +0,0 @@
|
||||
package com.java3y.austin.handler.domain.wechat;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author sunql
|
||||
* @date 2022年05月06日 9:56
|
||||
*
|
||||
* 服务号参数
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class WeChatOfficialParam {
|
||||
/**
|
||||
* 业务Id
|
||||
*/
|
||||
private Long messageTemplateId;
|
||||
|
||||
/**
|
||||
* 关注服务号得用户
|
||||
*/
|
||||
private Set<String> openIds;
|
||||
|
||||
/**
|
||||
* 模板消息的信息载体
|
||||
*/
|
||||
private Map<String, String> data;
|
||||
|
||||
/**
|
||||
* 发送账号
|
||||
*/
|
||||
private Integer sendAccount;
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package com.java3y.austin.handler.wechat;
|
||||
|
||||
import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zyg
|
||||
*/
|
||||
public interface OfficialAccountService {
|
||||
|
||||
/**
|
||||
* 发送模板消息
|
||||
*
|
||||
* @param weChatOfficialParam 模板消息参数
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
List<String> send(WeChatOfficialParam weChatOfficialParam) throws Exception;
|
||||
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
package com.java3y.austin.handler.wechat.impl;
|
||||
|
||||
import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
|
||||
import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam;
|
||||
import com.java3y.austin.handler.wechat.OfficialAccountService;
|
||||
import com.java3y.austin.support.utils.AccountUtils;
|
||||
import com.java3y.austin.support.utils.WxServiceUtils;
|
||||
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.WxMpTemplateData;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author zyg
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OfficialAccountServiceImpl implements OfficialAccountService {
|
||||
|
||||
@Autowired
|
||||
private AccountUtils accountUtils;
|
||||
|
||||
@Override
|
||||
public List<String> send(WeChatOfficialParam officialParam) throws Exception {
|
||||
WxMpService wxMpService = WxServiceUtils.wxMpServiceMap.get(officialParam.getSendAccount());
|
||||
WeChatOfficialAccount officialAccount = WxServiceUtils.accountHashMap.get(officialParam.getSendAccount());
|
||||
List<WxMpTemplateMessage> messages = assembleReq(officialParam, officialAccount);
|
||||
List<String> messageIds = new ArrayList<>(messages.size());
|
||||
for (WxMpTemplateMessage wxMpTemplateMessage : messages) {
|
||||
String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
|
||||
messageIds.add(msgId);
|
||||
}
|
||||
return messageIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装发送模板信息参数
|
||||
*/
|
||||
private List<WxMpTemplateMessage> assembleReq(WeChatOfficialParam officialParam, WeChatOfficialAccount officialAccount) {
|
||||
Set<String> receiver = officialParam.getOpenIds();
|
||||
List<WxMpTemplateMessage> wxMpTemplateMessages = new ArrayList<>(receiver.size());
|
||||
|
||||
// 构建微信模板消息
|
||||
for (String openId : receiver) {
|
||||
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
||||
.toUser(openId)
|
||||
.templateId(officialAccount.getTemplateId())
|
||||
.url(officialAccount.getUrl())
|
||||
.data(getWxMpTemplateData(officialParam.getData()))
|
||||
.miniProgram(new WxMpTemplateMessage.MiniProgram(officialAccount.getMiniProgramId(), officialAccount.getPath(), false))
|
||||
.build();
|
||||
wxMpTemplateMessages.add(templateMessage);
|
||||
}
|
||||
return wxMpTemplateMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建模板消息参数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private List<WxMpTemplateData> getWxMpTemplateData(Map<String, String> data) {
|
||||
List<WxMpTemplateData> templateDataList = new ArrayList<>(data.size());
|
||||
data.forEach((k, v) -> templateDataList.add(new WxMpTemplateData(k, v)));
|
||||
return templateDataList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化微信服务号
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public WxMpService initService(WeChatOfficialAccount officialAccount) {
|
||||
WxMpService wxMpService = new WxMpServiceImpl();
|
||||
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
||||
config.setAppId(officialAccount.getAppId());
|
||||
config.setSecret(officialAccount.getSecret());
|
||||
wxMpService.setWxMpConfigStorage(config);
|
||||
return wxMpService;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue