mirror of https://github.com/ZhongFuCheng3y/austin
parent
874c1a66e6
commit
4dd3b84ad6
@ -1,8 +1,22 @@
|
|||||||
package com.java3y.austin.common.dto.model;
|
package com.java3y.austin.common.dto.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 3y
|
* @author 3y
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class MiniProgramContentModel extends ContentModel {
|
public class MiniProgramContentModel extends ContentModel {
|
||||||
|
/**
|
||||||
|
* 模板消息发送的数据
|
||||||
|
*/
|
||||||
|
Map<String, String> map;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,67 @@
|
|||||||
|
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.dto.model.MiniProgramContentModel;
|
||||||
|
import com.java3y.austin.common.dto.model.OfficialAccountsContentModel;
|
||||||
|
import com.java3y.austin.common.enums.ChannelType;
|
||||||
|
import com.java3y.austin.handler.domain.wechat.WeChatMiniProgramParam;
|
||||||
|
import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam;
|
||||||
|
import com.java3y.austin.handler.handler.BaseHandler;
|
||||||
|
import com.java3y.austin.handler.handler.Handler;
|
||||||
|
import com.java3y.austin.handler.script.MiniProgramAccountService;
|
||||||
|
import com.java3y.austin.handler.script.OfficialAccountService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author sunql
|
||||||
|
* 微信小程序发送订阅消息
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class MiniProgramAccountHandler extends BaseHandler implements Handler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MiniProgramAccountService miniProgramAccountService;
|
||||||
|
|
||||||
|
public MiniProgramAccountHandler() {
|
||||||
|
channelCode = ChannelType.MINI_PROGRAM.getCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean handler(TaskInfo taskInfo) {
|
||||||
|
WeChatMiniProgramParam miniProgramParam = buildMiniProgramParam(taskInfo);
|
||||||
|
try {
|
||||||
|
miniProgramAccountService.send(miniProgramParam);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("MiniProgramAccountHandler#handler fail:{},params:{}",
|
||||||
|
Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过taskInfo构建小程序订阅消息
|
||||||
|
*
|
||||||
|
* @param taskInfo
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private WeChatMiniProgramParam buildMiniProgramParam(TaskInfo taskInfo) {
|
||||||
|
// 小程序订阅消息可以关联到系统业务,通过接口查询。
|
||||||
|
WeChatMiniProgramParam miniProgramParam = WeChatMiniProgramParam.builder()
|
||||||
|
.openIds(taskInfo.getReceiver())
|
||||||
|
.messageTemplateId(taskInfo.getMessageTemplateId())
|
||||||
|
.sendAccount(taskInfo.getSendAccount())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
MiniProgramContentModel contentModel = (MiniProgramContentModel) taskInfo.getContentModel();
|
||||||
|
miniProgramParam.setData(contentModel.getMap());
|
||||||
|
return miniProgramParam;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.java3y.austin.handler.script;
|
||||||
|
|
||||||
|
import com.java3y.austin.handler.domain.wechat.WeChatMiniProgramParam;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author sunql
|
||||||
|
*/
|
||||||
|
public interface MiniProgramAccountService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送订阅消息
|
||||||
|
*
|
||||||
|
* @param miniProgramParam 订阅消息参数
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
void send(WeChatMiniProgramParam miniProgramParam) throws Exception;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
package com.java3y.austin.handler.script.impl;
|
||||||
|
|
||||||
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||||
|
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
|
||||||
|
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||||
|
import cn.binarywang.wx.miniapp.api.impl.WxMaSubscribeServiceImpl;
|
||||||
|
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
|
||||||
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||||
|
import com.java3y.austin.common.constant.SendAccountConstant;
|
||||||
|
import com.java3y.austin.common.dto.account.WeChatMiniProgramAccount;
|
||||||
|
import com.java3y.austin.handler.domain.wechat.WeChatMiniProgramParam;
|
||||||
|
import com.java3y.austin.handler.script.MiniProgramAccountService;
|
||||||
|
import com.java3y.austin.support.utils.AccountUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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 sunql
|
||||||
|
* @date 2022年05月06日 16:41
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class MiniProgramAccountServiceImpl implements MiniProgramAccountService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AccountUtils accountUtils;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(WeChatMiniProgramParam miniProgramParam) throws Exception {
|
||||||
|
WeChatMiniProgramAccount miniProgramAccount = accountUtils.getAccount(miniProgramParam.getSendAccount(),
|
||||||
|
SendAccountConstant.WECHAT_MINI_PROGRAM_ACCOUNT_KEY,
|
||||||
|
SendAccountConstant.WECHAT_MINI_PROGRAM_PREFIX,
|
||||||
|
WeChatMiniProgramAccount.builder().build());
|
||||||
|
|
||||||
|
WxMaSubscribeService wxMaSubscribeService = initService(miniProgramAccount);
|
||||||
|
List<WxMaSubscribeMessage> subscribeMessageList = assembleReq(miniProgramParam, miniProgramAccount);
|
||||||
|
for (WxMaSubscribeMessage subscribeMessage : subscribeMessageList) {
|
||||||
|
wxMaSubscribeService.sendSubscribeMsg(subscribeMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组装发送模板信息参数
|
||||||
|
*/
|
||||||
|
private List<WxMaSubscribeMessage> assembleReq(WeChatMiniProgramParam miniProgramParam, WeChatMiniProgramAccount miniProgramAccount) {
|
||||||
|
Set<String> receiver = miniProgramParam.getOpenIds();
|
||||||
|
List<WxMaSubscribeMessage> messageList = new ArrayList<>(receiver.size());
|
||||||
|
|
||||||
|
// 构建微信小程序订阅消息
|
||||||
|
for (String openId : receiver) {
|
||||||
|
WxMaSubscribeMessage subscribeMessage = WxMaSubscribeMessage.builder()
|
||||||
|
.toUser(openId)
|
||||||
|
.data(getWxMTemplateData(miniProgramParam.getData()))
|
||||||
|
.miniprogramState(miniProgramAccount.getMiniProgramState())
|
||||||
|
.templateId(miniProgramAccount.getTemplateId())
|
||||||
|
.page(miniProgramAccount.getPage())
|
||||||
|
.build();
|
||||||
|
messageList.add(subscribeMessage);
|
||||||
|
}
|
||||||
|
return messageList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建订阅消息参数
|
||||||
|
*
|
||||||
|
* @returnp
|
||||||
|
*/
|
||||||
|
private List<WxMaSubscribeMessage.MsgData> getWxMTemplateData(Map<String, String> data) {
|
||||||
|
List<WxMaSubscribeMessage.MsgData> templateDataList = new ArrayList<>(data.size());
|
||||||
|
data.forEach((k, v) -> templateDataList.add(new WxMaSubscribeMessage.MsgData(k, v)));
|
||||||
|
return templateDataList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化微信小程序
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private WxMaSubscribeServiceImpl initService(WeChatMiniProgramAccount miniProgramAccount) {
|
||||||
|
WxMaService wxMaService = new WxMaServiceImpl();
|
||||||
|
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
|
||||||
|
wxMaConfig.setAppid(miniProgramAccount.getAppId());
|
||||||
|
wxMaConfig.setSecret(miniProgramAccount.getAppSecret());
|
||||||
|
wxMaService.setWxMaConfig(wxMaConfig);
|
||||||
|
return new WxMaSubscribeServiceImpl(wxMaService);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue