mirror of https://github.com/ZhongFuCheng3y/austin
commit
542421aa1d
@ -0,0 +1,42 @@
|
||||
package com.java3y.austin.common.dto.account;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author jwq
|
||||
* 支付宝小程序订阅消息账号配置
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AlipayMiniProgramAccount {
|
||||
|
||||
/**
|
||||
* 应用私钥
|
||||
*/
|
||||
private String privateKey;
|
||||
|
||||
/**
|
||||
* 支付宝公钥
|
||||
*/
|
||||
private String alipayPublicKey;
|
||||
|
||||
/**
|
||||
* 支付宝小程序的AppID
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 订阅模版Id
|
||||
*/
|
||||
private String userTemplateId;
|
||||
|
||||
/**
|
||||
* 点击跳转到的小程序页面
|
||||
*/
|
||||
private String page;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.java3y.austin.common.dto.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author jwq
|
||||
* 支付宝小程序订阅消息内容
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AlipayMiniProgramContentModel extends ContentModel{
|
||||
|
||||
/**
|
||||
* 模板消息发送的数据
|
||||
*/
|
||||
Map<String, String> map;
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.java3y.austin.handler.alipay;
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.java3y.austin.handler.domain.alipay.AlipayMiniProgramParam;
|
||||
|
||||
/**
|
||||
* @author jwq
|
||||
* 支付宝小程序发送订阅消息接口
|
||||
*/
|
||||
public interface AlipayMiniProgramAccountService {
|
||||
/**
|
||||
* 发送订阅消息
|
||||
*
|
||||
* @param miniProgramParam 订阅消息参数
|
||||
* @throws AlipayApiException alipay异常
|
||||
*/
|
||||
void send(AlipayMiniProgramParam miniProgramParam) throws AlipayApiException;
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package com.java3y.austin.handler.alipay.impl;
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.AlipayConfig;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.alipay.api.domain.AlipayOpenAppMiniTemplatemessageSendModel;
|
||||
import com.alipay.api.request.AlipayOpenAppMiniTemplatemessageSendRequest;
|
||||
import com.java3y.austin.common.constant.SendAccountConstant;
|
||||
import com.java3y.austin.common.dto.account.AlipayMiniProgramAccount;
|
||||
import com.java3y.austin.handler.alipay.AlipayMiniProgramAccountService;
|
||||
import com.java3y.austin.handler.domain.alipay.AlipayMiniProgramParam;
|
||||
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.Set;
|
||||
|
||||
/**
|
||||
* @author jwq
|
||||
* 支付宝小程序发送订阅消息实现
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AlipayMiniProgramAccountServiceImpl implements AlipayMiniProgramAccountService {
|
||||
|
||||
@Autowired
|
||||
private AccountUtils accountUtils;
|
||||
|
||||
/**
|
||||
* 发送订阅消息
|
||||
*
|
||||
* @param miniProgramParam 订阅消息参数
|
||||
* @throws AlipayApiException alipay异常
|
||||
*/
|
||||
@Override
|
||||
public void send(AlipayMiniProgramParam miniProgramParam) throws AlipayApiException {
|
||||
AlipayMiniProgramAccount miniProgramAccount = accountUtils.getAccount(miniProgramParam.getSendAccount(),
|
||||
SendAccountConstant.ALIPAY_MINI_PROGRAM_ACCOUNT_KEY,
|
||||
SendAccountConstant.ALIPAY_MINI_PROGRAM_PREFIX,
|
||||
AlipayMiniProgramAccount.class);
|
||||
|
||||
AlipayClient client = initService(miniProgramAccount);
|
||||
List<AlipayOpenAppMiniTemplatemessageSendRequest> request = assembleReq(miniProgramParam, miniProgramAccount);
|
||||
for(AlipayOpenAppMiniTemplatemessageSendRequest req : request){
|
||||
client.execute(req);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装模板消息的参数
|
||||
*/
|
||||
private List<AlipayOpenAppMiniTemplatemessageSendRequest> assembleReq(AlipayMiniProgramParam alipayMiniProgramParam, AlipayMiniProgramAccount alipayMiniProgramAccount){
|
||||
Set<String> receiver = alipayMiniProgramParam.getToUserId();
|
||||
List<AlipayOpenAppMiniTemplatemessageSendRequest> requestList = new ArrayList<>(receiver.size());
|
||||
|
||||
for(String toUserId : receiver){
|
||||
AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
|
||||
AlipayOpenAppMiniTemplatemessageSendModel model = new AlipayOpenAppMiniTemplatemessageSendModel();
|
||||
model.setToUserId(toUserId);
|
||||
model.setUserTemplateId(alipayMiniProgramAccount.getUserTemplateId());
|
||||
model.setPage(alipayMiniProgramAccount.getPage());
|
||||
model.setData(alipayMiniProgramParam.getData().toString());
|
||||
request.setBizModel(model);
|
||||
requestList.add(request);
|
||||
}
|
||||
return requestList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化支付宝小程序
|
||||
*/
|
||||
private AlipayClient initService(AlipayMiniProgramAccount alipayMiniProgramAccount) throws AlipayApiException {
|
||||
AlipayConfig alipayConfig = new AlipayConfig();
|
||||
alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
|
||||
alipayConfig.setAppId(alipayMiniProgramAccount.getAppId());
|
||||
alipayConfig.setPrivateKey(alipayMiniProgramAccount.getPrivateKey());
|
||||
alipayConfig.setFormat("json");
|
||||
alipayConfig.setAlipayPublicKey(alipayMiniProgramAccount.getAlipayPublicKey());
|
||||
alipayConfig.setCharset("utf-8");
|
||||
alipayConfig.setSignType("RSA2");
|
||||
return new DefaultAlipayClient(alipayConfig);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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.AlipayMiniProgramContentModel;
|
||||
import com.java3y.austin.common.enums.ChannelType;
|
||||
import com.java3y.austin.handler.alipay.AlipayMiniProgramAccountService;
|
||||
import com.java3y.austin.handler.domain.alipay.AlipayMiniProgramParam;
|
||||
import com.java3y.austin.handler.handler.BaseHandler;
|
||||
import com.java3y.austin.handler.handler.Handler;
|
||||
import com.java3y.austin.support.domain.MessageTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author jwq
|
||||
* 支付宝小程序发送订阅消息
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AlipayMiniProgramAccountHandler extends BaseHandler implements Handler {
|
||||
|
||||
@Autowired
|
||||
private AlipayMiniProgramAccountService alipayMiniProgramAccountService;
|
||||
|
||||
public AlipayMiniProgramAccountHandler() {
|
||||
channelCode = ChannelType.ALIPAY_MINI_PROGRAM.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handler(TaskInfo taskInfo) {
|
||||
AlipayMiniProgramParam miniProgramParam = buildMiniProgramParam(taskInfo);
|
||||
try {
|
||||
alipayMiniProgramAccountService.send(miniProgramParam);
|
||||
}catch (Exception e) {
|
||||
log.error("AlipayMiniProgramAccountHandler#handler fail:{},params:{}",
|
||||
Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过taskInfo构建小程序订阅消息
|
||||
*
|
||||
* @param taskInfo 任务信息
|
||||
* @return AlipayMiniProgramParam
|
||||
*/
|
||||
private AlipayMiniProgramParam buildMiniProgramParam(TaskInfo taskInfo){
|
||||
AlipayMiniProgramParam param = AlipayMiniProgramParam.builder()
|
||||
.toUserId(taskInfo.getReceiver())
|
||||
.messageTemplateId(taskInfo.getMessageTemplateId())
|
||||
.sendAccount(taskInfo.getSendAccount())
|
||||
.build();
|
||||
|
||||
AlipayMiniProgramContentModel contentModel = (AlipayMiniProgramContentModel) taskInfo.getContentModel();
|
||||
param.setData(contentModel.getMap());
|
||||
return param;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recall(MessageTemplate messageTemplate) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue