实现支付宝小程序订阅消息发送

pull/62/head
Psycho 9 months ago
parent b64a2258f6
commit 047b6a4525

@ -30,7 +30,7 @@ public class SendChanelUrlConstant {
/**
* url
*/
public static final String ALI_MINI_PROGRAM_GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
public static final String ALI_MINI_PROGRAM_GATEWAY_URL = "https://openapi.alipay.com/gateway.do";
/**
* url
*/

@ -16,10 +16,19 @@ import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
public class AlipayMiniProgramContentModel extends ContentModel {
/**
*
*/
private Map<String, String> map;
private Map<String, String> miniProgramParam;
/**
* Id
*/
private String templateId;
/**
*
*/
private String page;
}

@ -27,7 +27,8 @@ public class SendMessageAction implements BusinessProcess<TaskInfo> {
// 微信小程序&服务号只支持单人推送,为了后续逻辑统一处理,于是在这做了单发处理
if (ChannelType.MINI_PROGRAM.getCode().equals(taskInfo.getSendChannel())
|| ChannelType.OFFICIAL_ACCOUNT.getCode().equals(taskInfo.getSendChannel())) {
|| ChannelType.OFFICIAL_ACCOUNT.getCode().equals(taskInfo.getSendChannel())
|| ChannelType.ALIPAY_MINI_PROGRAM.getCode().equals(taskInfo.getSendChannel())) {
for (String receiver : taskInfo.getReceiver()) {
TaskInfo taskClone = ObjectUtil.cloneByStream(taskInfo);
taskClone.setReceiver(Sets.newHashSet(receiver));

@ -1,5 +1,7 @@
package com.java3y.austin.handler.handler.impl;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayOpenAppMiniTemplatemessageSendModel;
@ -19,9 +21,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author jwq
@ -40,11 +41,12 @@ public class AlipayMiniProgramAccountHandler extends BaseHandler implements Hand
@Override
public boolean handler(TaskInfo taskInfo) {
AlipayMiniProgramParam miniProgramParam = buildMiniProgramParam(taskInfo);
AlipayMiniProgramContentModel contentModel= (AlipayMiniProgramContentModel) taskInfo.getContentModel();
try {
AlipayMiniProgramAccount miniProgramAccount = accountUtils.getAccountById(miniProgramParam.getSendAccount(), AlipayMiniProgramAccount.class);
AlipayMiniProgramAccount miniProgramAccount = accountUtils.getAccountById(taskInfo.getSendAccount(), AlipayMiniProgramAccount.class);
AlipayClient client = AlipayClientSingleton.getSingleton(miniProgramAccount);
List<AlipayOpenAppMiniTemplatemessageSendRequest> request = assembleReq(miniProgramParam, miniProgramAccount);
List<AlipayOpenAppMiniTemplatemessageSendRequest> request = assembleReq(taskInfo.getReceiver(), contentModel);
for (AlipayOpenAppMiniTemplatemessageSendRequest req : request) {
client.execute(req);
}
@ -59,35 +61,43 @@ public class AlipayMiniProgramAccountHandler extends BaseHandler implements Hand
/**
* taskInfo
*
* @param taskInfo
* @param
* @return AlipayMiniProgramParam
*/
private AlipayMiniProgramParam buildMiniProgramParam(TaskInfo taskInfo) {
AlipayMiniProgramParam param = AlipayMiniProgramParam.builder()
.toUserId(taskInfo.getReceiver())
.messageTemplateId(taskInfo.getMessageTemplateId())
.sendAccount(taskInfo.getSendAccount())
.build();
private String getAlipayMiniProgramParam(Map<String, String> data) {
Map<String, Map<String, String>> newMap = data.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> {
Map<String, String> valueMap = new HashMap<>();
valueMap.put("value", entry.getValue());
return valueMap;
}
));
return JSONUtil.toJsonStr(newMap);
AlipayMiniProgramContentModel contentModel = (AlipayMiniProgramContentModel) taskInfo.getContentModel();
param.setData(contentModel.getMap());
return param;
}
/**
*
*/
private List<AlipayOpenAppMiniTemplatemessageSendRequest> assembleReq(AlipayMiniProgramParam alipayMiniProgramParam, AlipayMiniProgramAccount alipayMiniProgramAccount) {
Set<String> receiver = alipayMiniProgramParam.getToUserId();
private List<AlipayOpenAppMiniTemplatemessageSendRequest> assembleReq(Set<String> receiver, AlipayMiniProgramContentModel alipayMiniProgramContentModel) {
List<AlipayOpenAppMiniTemplatemessageSendRequest> requestList = new ArrayList<>(receiver.size());
for (String toUserId : receiver) {
AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
AlipayOpenAppMiniTemplatemessageSendModel model = new AlipayOpenAppMiniTemplatemessageSendModel();
//兼容新旧用户ID
if(toUserId.startsWith("2088")) {
model.setToUserId(toUserId);
} else {
model.setToOpenId(toUserId);
}
model.setToUserId(toUserId);
model.setUserTemplateId(alipayMiniProgramAccount.getUserTemplateId());
model.setPage(alipayMiniProgramAccount.getPage());
model.setData(alipayMiniProgramParam.getData().toString());
model.setUserTemplateId(alipayMiniProgramContentModel.getTemplateId());
model.setPage(alipayMiniProgramContentModel.getPage());
model.setData(getAlipayMiniProgramParam(alipayMiniProgramContentModel.getMiniProgramParam()));
request.setBizModel(model);
requestList.add(request);
}

@ -0,0 +1,156 @@
package com.java3y.austin.web.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.http.HttpUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.MerchantMsgTemplateVO;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.SendChanelUrlConstant;
import com.java3y.austin.common.dto.account.AlipayMiniProgramAccount;
import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.handler.config.AlipayClientSingleton;
import com.java3y.austin.support.utils.AccountUtils;
import com.java3y.austin.web.annotation.AustinAspect;
import com.java3y.austin.web.annotation.AustinResult;
import com.java3y.austin.web.exception.CommonException;
import com.java3y.austin.web.utils.Convert4Amis;
import com.java3y.austin.web.vo.amis.CommonAmisVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alipay.api.request.AlipayOpenMiniMessageTemplateBatchqueryRequest;
import com.alipay.api.response.AlipayOpenMiniMessageTemplateBatchqueryResponse;
import com.alipay.api.domain.AlipayOpenMiniMessageTemplateBatchqueryModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
*
*
* @author 3y
*/
@Slf4j
@AustinAspect
@RestController
@RequestMapping("/alipayMiniProgram")
@Api("支付宝小程序")
public class AlipayMiniProgramController {
@Autowired
private AccountUtils accountUtils;
@GetMapping("/template/list")
@ApiOperation("/根据账号Id获取模板列表")
@AustinResult
public List<CommonAmisVo> queryList(Integer id) {
try {
List<CommonAmisVo> result = new ArrayList<>();
AlipayMiniProgramAccount miniProgramAccount = accountUtils.getAccountById(id, AlipayMiniProgramAccount.class);
AlipayClient client = AlipayClientSingleton.getSingleton(miniProgramAccount);
// 构造请求参数以调用接口
AlipayOpenMiniMessageTemplateBatchqueryRequest request = new AlipayOpenMiniMessageTemplateBatchqueryRequest();
AlipayOpenMiniMessageTemplateBatchqueryModel model = new AlipayOpenMiniMessageTemplateBatchqueryModel();
// 设置子板状态列表
List<String> statusList = new ArrayList<String>();
statusList.add("STARTED");
model.setStatusList(statusList);
// 设置消息业务类型
model.setBizType("sub_msg");
// 设置分页页号
model.setPageNum(1L);
// 设置分页大小
model.setPageSize("1");
request.setBizModel(model);
AlipayOpenMiniMessageTemplateBatchqueryResponse response = client.execute(request);
List<MerchantMsgTemplateVO> templateList = response.getTemplateList();
for (MerchantMsgTemplateVO templateInfo : templateList) {
CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(templateInfo.getName()).value(templateInfo.getTemplateId()).build();
result.add(commonAmisVo);
}
return result;
} catch (Exception e) {
log.error("MiniProgramController#queryList fail:{}", Throwables.getStackTraceAsString(e));
throw new CommonException(RespStatusEnum.SERVICE_ERROR);
}
}
/**
* IdID
*
* @return
*/
@PostMapping("/detailTemplate")
@ApiOperation("/根据账号Id和模板ID获取模板列表")
@AustinResult
public CommonAmisVo queryDetailList(Integer id, String alipayTemplateId) {
if (Objects.isNull(id) || Objects.isNull(alipayTemplateId)) {
log.info("id || wxTemplateId null! id:{},wxTemplateId:{}", id, alipayTemplateId);
return CommonAmisVo.builder().build();
}
try {
AlipayMiniProgramAccount miniProgramAccount = accountUtils.getAccountById(id, AlipayMiniProgramAccount.class);
AlipayClient client = AlipayClientSingleton.getSingleton(miniProgramAccount);
// 构造请求参数以调用接口
AlipayOpenMiniMessageTemplateBatchqueryRequest request = new AlipayOpenMiniMessageTemplateBatchqueryRequest();
AlipayOpenMiniMessageTemplateBatchqueryModel model = new AlipayOpenMiniMessageTemplateBatchqueryModel();
// 设置子板状态列表
List<String> statusList = new ArrayList<String>();
statusList.add("STARTED");
model.setStatusList(statusList);
// 设置消息业务类型
model.setBizType("sub_msg");
// 设置分页页号
model.setPageNum(1L);
// 设置分页大小
model.setPageSize("1");
request.setBizModel(model);
AlipayOpenMiniMessageTemplateBatchqueryResponse response = client.execute(request);
List<MerchantMsgTemplateVO> templateList = response.getTemplateList();
return Convert4Amis.getAlipayTemplateParam(alipayTemplateId, templateList);
} catch (Exception e) {
log.error("MiniProgramController#queryDetailList fail:{}", Throwables.getStackTraceAsString(e));
throw new CommonException(RespStatusEnum.SERVICE_ERROR);
}
}
/**
*
* <p>
* 使
*
* @return
*/
@GetMapping("/sync/openid")
@ApiOperation("登录凭证校验")
public String syncOpenId(String code, String appId, String secret) {
String url = SendChanelUrlConstant.WE_CHAT_MINI_PROGRAM_OPENID_SYNC
.replace("<APPID>", appId).replace("<CODE>", code).replace("<SECRET>", secret);
return HttpUtil.get(url);
}
}

@ -9,6 +9,7 @@ import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.MerchantMsgTemplateVO;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.common.enums.EnumUtil;
import com.java3y.austin.common.enums.SmsStatus;
@ -325,6 +326,45 @@ public class Convert4Amis {
}
/**
* amis
* <p>
*
*
* @param alipayTemplateId
* @param templateList
* @return
*/
public static CommonAmisVo getAlipayTemplateParam(String alipayTemplateId, List<MerchantMsgTemplateVO> templateList) {
CommonAmisVo officialAccountParam = null;
for (MerchantMsgTemplateVO templateInfo : templateList) {
if (alipayTemplateId.equals(templateInfo.getTemplateId())) {
String[] data = templateInfo.getKeywordDesc().split(StrUtil.COMMA);
officialAccountParam = CommonAmisVo.builder()
.type("input-table")
.name("miniProgramParam")
.addable(true)
.editable(true)
.needConfirm(false)
.build();
List<CommonAmisVo.ColumnsDTO> columnsDtoS = new ArrayList<>();
//使用i作为变量循环
for (int i=0;i<data.length;i++) {
String name ="keyword"+String.valueOf(i+1);
String label = data[i];
CommonAmisVo.ColumnsDTO columnsDTO = CommonAmisVo.ColumnsDTO.builder()
.name(name).type("input-text").required(true).quickEdit(true).label(label).build();
columnsDtoS.add(columnsDTO);
}
officialAccountParam.setColumns(columnsDtoS);
}
}
return officialAccountParam;
}
/**
* amis
* <p>

@ -201,7 +201,7 @@
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.33.39.ALL</version>
<version>4.39.19.ALL</version>
</dependency>
<!--阿里云 钉钉 SDK-->

Loading…
Cancel
Save