pull/11/head
3y 2 years ago
commit 91448dbab1

@ -39,6 +39,17 @@ public class SendAccountConstant {
public static final String ENTERPRISE_WECHAT_ACCOUNT_KEY = "enterpriseWechatAccount";
public static final String ENTERPRISE_WECHAT_PREFIX = "enterprise_wechat_";
/**
*
*/
public static final String WECHAT_OFFICIAL_ACCOUNT_KEY = "officialAccount";
public static final String WECHAT_OFFICIAL__PREFIX = "official_";
/**
*
*/
public static final String WECHAT_MINI_PROGRAM_ACCOUNT_KEY = "miniProgramAccount";
public static final String WECHAT_MINI_PROGRAM_PREFIX = "mini_program_";
/**
*

@ -0,0 +1,44 @@
package com.java3y.austin.common.dto.account;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*
* <p>
*
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
* * @author sunql
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WeChatMiniProgramAccount {
/**
* ID
*/
private String templateId;
/**
* developertrialformal
*/
private String miniProgramState;
/**
* ,index?foo=bar
*/
private String page;
/**
*
*/
private String appId;
private String appSecret;
private String grantType;
}

@ -19,17 +19,7 @@ import java.util.Map;
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WechatOfficialAccount {
/**
* openId
*/
private String openId;
/**
* 使Id
*/
private String templateId;
public class WeChatOfficialAccount {
/**
* url
@ -47,7 +37,9 @@ public class WechatOfficialAccount {
private String path;
/**
*
*
*/
private Map<String, String> map;
private String appId;
private String secret;
private String templateId;
}

@ -1,8 +1,22 @@
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
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MiniProgramContentModel extends ContentModel {
/**
*
*/
Map<String, String> map;
}

@ -46,6 +46,11 @@
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
</dependency>
<!--微信小程序第三方SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
<!--企业微信发送消息-->
<dependency>
@ -54,4 +59,4 @@
</dependency>
</dependencies>
</project>
</project>

@ -0,0 +1,38 @@
package com.java3y.austin.handler.domain.wechat;
import lombok.Builder;
import lombok.Data;
import java.util.Map;
import java.util.Set;
/**
* @author sunql
* @date 20220506 15:56
*
*
*/
@Data
@Builder
public class WeChatMiniProgramParam {
/**
* Id
*/
private Long messageTemplateId;
/**
*
*/
private Integer sendAccount;
/**
* openid
*/
private Set<String> openIds;
/**
* { "key1": { "value": any }, "key2": { "value": any } }
*/
private Map<String, String> data;
}

@ -0,0 +1,37 @@
package com.java3y.austin.handler.domain.wechat;
import lombok.Builder;
import lombok.Data;
import java.util.Map;
import java.util.Set;
/**
* @author sunql
* @date 20220506 9:56
*
*
*/
@Data
@Builder
public class WeChatOfficialParam {
/**
* Id
*/
private Long messageTemplateId;
/**
*
*/
private Set<String> openIds;
/**
*
*/
private Map<String, String> data;
/**
*
*/
private Integer sendAccount;
}

@ -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;
}
}

@ -5,19 +5,15 @@ import com.google.common.base.Throwables;
import com.java3y.austin.common.domain.TaskInfo;
import com.java3y.austin.common.dto.model.OfficialAccountsContentModel;
import com.java3y.austin.common.enums.ChannelType;
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.OfficialAccountService;
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;
/**
* @author zyg
@ -30,18 +26,24 @@ public class OfficialAccountHandler extends BaseHandler implements Handler {
@Autowired
private OfficialAccountService officialAccountService;
public OfficialAccountHandler() {
channelCode = ChannelType.OFFICIAL_ACCOUNT.getCode();
}
@Override
public boolean handler(TaskInfo taskInfo) {
// 构建微信模板消息
OfficialAccountsContentModel contentModel = (OfficialAccountsContentModel) taskInfo.getContentModel();
WeChatOfficialParam officialParam = WeChatOfficialParam.builder()
.openIds(taskInfo.getReceiver())
.messageTemplateId(taskInfo.getMessageTemplateId())
.sendAccount(taskInfo.getSendAccount())
.data(contentModel.getMap())
.build();
List<WxMpTemplateMessage> mpTemplateMessages = buildTemplateMsg(taskInfo);
// 微信模板消息需要记录响应结果
try {
List<String> messageIds = officialAccountService.send(mpTemplateMessages);
List<String> messageIds = officialAccountService.send(officialParam);
log.info("OfficialAccountHandler#handler successfully messageIds:{}", messageIds);
return true;
} catch (Exception e) {
@ -51,45 +53,5 @@ public class OfficialAccountHandler extends BaseHandler implements Handler {
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,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;
}

@ -1,5 +1,6 @@
package com.java3y.austin.handler.script;
import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import java.util.List;
@ -12,10 +13,10 @@ public interface OfficialAccountService {
/**
*
*
* @param wxMpTemplateMessages
* @param weChatOfficialParam
* @return
* @throws Exception
*/
List<String> send(List<WxMpTemplateMessage> wxMpTemplateMessages) throws Exception;
List<String> send(WeChatOfficialParam weChatOfficialParam) 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 20220506 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);
}
}

@ -1,16 +1,23 @@
package com.java3y.austin.handler.script.impl;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.WeChatOfficialAccount;
import com.java3y.austin.handler.domain.wechat.WeChatOfficialParam;
import com.java3y.austin.handler.script.OfficialAccountService;
import com.java3y.austin.support.utils.AccountUtils;
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.Value;
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
@ -19,19 +26,14 @@ import java.util.List;
@Slf4j
public class OfficialAccountServiceImpl implements OfficialAccountService {
@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;
@Autowired
private AccountUtils accountUtils;
@Override
public List<String> send(List<WxMpTemplateMessage> messages) throws Exception {
WxMpService wxMpService = initService();
public List<String> send(WeChatOfficialParam officialParam) throws Exception {
WeChatOfficialAccount officialAccount = accountUtils.getAccount(officialParam.getSendAccount(), SendAccountConstant.WECHAT_OFFICIAL_ACCOUNT_KEY, SendAccountConstant.WECHAT_OFFICIAL__PREFIX, WeChatOfficialAccount.builder().build());
WxMpService wxMpService = initService(officialAccount);
List<WxMpTemplateMessage> messages = assembleReq(officialParam, officialAccount);
List<String> messageIds = new ArrayList<>(messages.size());
for (WxMpTemplateMessage wxMpTemplateMessage : messages) {
String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
@ -40,18 +42,48 @@ public class OfficialAccountServiceImpl implements OfficialAccountService {
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() {
public WxMpService initService(WeChatOfficialAccount officialAccount) {
WxMpService wxMpService = new WxMpServiceImpl();
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId(appId);
config.setSecret(secret);
config.setToken(token);
config.setAesKey(aesKey);
config.setAppId(officialAccount.getAppId());
config.setSecret(officialAccount.getSecret());
wxMpService.setWxMpConfigStorage(config);
return wxMpService;
}

@ -114,7 +114,8 @@ public class AssembleAction implements BusinessProcess<SendTaskModel> {
if (StrUtil.isNotBlank(originValue)) {
String resultValue = ContentHolderUtil.replacePlaceHolder(originValue, variables);
ReflectUtil.setFieldValue(contentModel, field, resultValue);
Object resultObj = JSON.parseObject(resultValue, field.getType());
ReflectUtil.setFieldValue(contentModel, field, resultObj);
}
}

@ -26,6 +26,8 @@ public class AccountUtils {
* (key:enterpriseWechatAccount)[{"enterprise_wechat_10":{"corpId":"wwf87603333e00069c","corpSecret":"-IFWxS2222QxzPIorNVUQn144444D915DM","agentId":10044442,"token":"rXROB3333Kf6i","aesKey":"MKZtoFxHIM44444M7ieag3r9ZPUsl"}}]
* (key:dingDingRobotAccount) [{"ding_ding_robot_10":{"secret":"SEC996d8d9d4768aded74114faae924f229229de444475a1c295d64fedf","webhook":"https://oapi.dingtalk.com/robot/send?access_token=8d03b644ffb6534b203d87333367328b0c3003d164715d2c6c6e56"}}]
* (key:dingDingWorkNoticeAccount) [{"ding_ding_work_notice_10":{"appKey":"dingh6yyyyyyycrlbx","appSecret":"tQpvmkR863333yyyyyHP3QHyyyymy9Ao1yoL1oQX5Nlx_fYLLLlpPJWHvWKbTu","agentId":"152333383622"}}]
* (key:officialAccount) [{"official_10":{"appId":"wxecb4693d2eef1ea7","secret":"6240870f4d91701640d769ba20120821","templateId":"JHUk6eE9T5Ts7a5JO3ZQqkBBrZBGn5C9iIiKNDQsk-Q","url":"http://weixin.qq.com/download","miniProgramId":"xiaochengxuappid12345","path":"index?foo=bar"}}]
* (key:miniProgramAccount) [{"mini_program_10":{"appId":"wxecb4693d2eef1ea7","appSecret":"6240870f4d91701640d769ba20120821","templateId":"JHUk6eE9T5Ts7a5JO3ZQqkBBrZBGn5C9iIiKNDQsk-Q","grantType":"client_credential","miniProgramState":"trial","page":"index?foo=bar"}}]
*/
public <T> T getAccount(Integer sendAccount, String apolloKey, String prefix, Class<T> clazz) {
String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY);

@ -87,10 +87,3 @@ management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.endpoints.web.exposure.include=*
management.metrics.export.prometheus.enabled=true
##################### wx mp config #####################
##################### TODO not test by 3y,wait to apply for OfficialAccount #####################
wx.mp.account.appid="appid"
wx.mp.account.secret="secret"
wx.mp.account.token="token"
wx.mp.account.aesKey="aesKey"

@ -160,6 +160,13 @@
<version>${weixin-java}</version>
</dependency>
<!--微信小程序第三方SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>${weixin-java}</version>
</dependency>
<!--动态线程池引入-->
<dependency>
<groupId>io.github.lyh200</groupId>

Loading…
Cancel
Save