From dced4fc7b9e9cf0f7e67bf015964e39f0e48ba67 Mon Sep 17 00:00:00 2001 From: jwq Date: Wed, 21 Sep 2022 10:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E4=BB=98=E5=AE=9D?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/constant/SendAccountConstant.java | 6 ++ .../dto/account/AlipayMiniProgramAccount.java | 42 +++++++++ .../model/AlipayMiniProgramContentModel.java | 25 ++++++ .../austin/common/enums/ChannelType.java | 1 + austin-handler/pom.xml | 5 ++ .../AlipayMiniProgramAccountService.java | 18 ++++ .../AlipayMiniProgramAccountServiceImpl.java | 89 +++++++++++++++++++ .../domain/alipay/AlipayMiniProgramParam.java | 43 +++++++++ .../impl/AlipayMiniProgramAccountHandler.java | 67 ++++++++++++++ .../austin/support/utils/AccountUtils.java | 2 + pom.xml | 7 ++ 11 files changed, 305 insertions(+) create mode 100644 austin-common/src/main/java/com/java3y/austin/common/dto/account/AlipayMiniProgramAccount.java create mode 100644 austin-common/src/main/java/com/java3y/austin/common/dto/model/AlipayMiniProgramContentModel.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/alipay/AlipayMiniProgramAccountService.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/alipay/impl/AlipayMiniProgramAccountServiceImpl.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/domain/alipay/AlipayMiniProgramParam.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/AlipayMiniProgramAccountHandler.java diff --git a/austin-common/src/main/java/com/java3y/austin/common/constant/SendAccountConstant.java b/austin-common/src/main/java/com/java3y/austin/common/constant/SendAccountConstant.java index baedd24..675c81f 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/constant/SendAccountConstant.java +++ b/austin-common/src/main/java/com/java3y/austin/common/constant/SendAccountConstant.java @@ -78,6 +78,12 @@ public class SendAccountConstant { public static final String WECHAT_MINI_PROGRAM_ACCOUNT_KEY = "miniProgramAccount"; public static final String WECHAT_MINI_PROGRAM_PREFIX = "mini_program_"; + /** + * 支付宝小程序 应用消息 账号 + */ + public static final String ALIPAY_MINI_PROGRAM_ACCOUNT_KEY = "alipayMiniProgramAccount"; + public static final String ALIPAY_MINI_PROGRAM_PREFIX = "alipay_mini_program_"; + /** * 短信 账号 */ diff --git a/austin-common/src/main/java/com/java3y/austin/common/dto/account/AlipayMiniProgramAccount.java b/austin-common/src/main/java/com/java3y/austin/common/dto/account/AlipayMiniProgramAccount.java new file mode 100644 index 0000000..9dfa247 --- /dev/null +++ b/austin-common/src/main/java/com/java3y/austin/common/dto/account/AlipayMiniProgramAccount.java @@ -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; +} diff --git a/austin-common/src/main/java/com/java3y/austin/common/dto/model/AlipayMiniProgramContentModel.java b/austin-common/src/main/java/com/java3y/austin/common/dto/model/AlipayMiniProgramContentModel.java new file mode 100644 index 0000000..5ee780f --- /dev/null +++ b/austin-common/src/main/java/com/java3y/austin/common/dto/model/AlipayMiniProgramContentModel.java @@ -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 map; + +} diff --git a/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java b/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java index 965b857..c81882d 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java +++ b/austin-common/src/main/java/com/java3y/austin/common/enums/ChannelType.java @@ -28,6 +28,7 @@ public enum ChannelType { DING_DING_WORK_NOTICE(90, "dingDingWorkNotice(钉钉工作通知)", DingDingWorkContentModel.class, "ding_ding_work_notice"), ENTERPRISE_WE_CHAT_ROBOT(100, "enterpriseWeChat(企业微信机器人)", EnterpriseWeChatRobotContentModel.class, "enterprise_we_chat_robot"), FEI_SHU_ROBOT(110, "feiShuRoot(飞书机器人)", FeiShuRobotContentModel.class, "fei_shu_robot"), + ALIPAY_MINI_PROGRAM(120,"alipayMiniProgram(支付宝小程序)",AlipayMiniProgramContentModel.class,"alipay_mini_program"), ; /** diff --git a/austin-handler/pom.xml b/austin-handler/pom.xml index 7f542c6..eb6ef31 100644 --- a/austin-handler/pom.xml +++ b/austin-handler/pom.xml @@ -57,6 +57,11 @@ com.github.binarywang weixin-java-cp + + + com.alipay.sdk + alipay-sdk-java + diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/alipay/AlipayMiniProgramAccountService.java b/austin-handler/src/main/java/com/java3y/austin/handler/alipay/AlipayMiniProgramAccountService.java new file mode 100644 index 0000000..6b5c957 --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/alipay/AlipayMiniProgramAccountService.java @@ -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; +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/alipay/impl/AlipayMiniProgramAccountServiceImpl.java b/austin-handler/src/main/java/com/java3y/austin/handler/alipay/impl/AlipayMiniProgramAccountServiceImpl.java new file mode 100644 index 0000000..5d59622 --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/alipay/impl/AlipayMiniProgramAccountServiceImpl.java @@ -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 request = assembleReq(miniProgramParam, miniProgramAccount); + for(AlipayOpenAppMiniTemplatemessageSendRequest req : request){ + client.execute(req); + } + } + + /** + * 组装模板消息的参数 + */ + private List assembleReq(AlipayMiniProgramParam alipayMiniProgramParam, AlipayMiniProgramAccount alipayMiniProgramAccount){ + Set receiver = alipayMiniProgramParam.getToUserId(); + List 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); + } + + +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/domain/alipay/AlipayMiniProgramParam.java b/austin-handler/src/main/java/com/java3y/austin/handler/domain/alipay/AlipayMiniProgramParam.java new file mode 100644 index 0000000..469641c --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/domain/alipay/AlipayMiniProgramParam.java @@ -0,0 +1,43 @@ +package com.java3y.austin.handler.domain.alipay; + +import lombok.Builder; +import lombok.Data; + +import java.util.Map; +import java.util.Set; + +/** + * @author jwq + * 支付宝小程序参数 + */ +@Data +@Builder +public class AlipayMiniProgramParam { + /** + * 业务Id + */ + private Long messageTemplateId; + + /** + * 发送账号 + */ + private Integer sendAccount; + + /** + * 接收者(用户)的 UserId + */ + private Set toUserId; + + /** + * 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } } + */ + private Map data; + +// /** +// * 支付消息模板:需传入用户发生的交易行为的支付宝交易号 trade_no; +// * 表单提交模板:需传入用户在小程序触发表单提交事件获得的表单号; +// * 刷脸消息模板:需传入在IOT刷脸后得到的ftoken等,用于信息发送的校验。 +// * 说明:订阅消息模板无需传入本参数。 +// */ +// private String formId; +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/AlipayMiniProgramAccountHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/AlipayMiniProgramAccountHandler.java new file mode 100644 index 0000000..6b90bae --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/handler/impl/AlipayMiniProgramAccountHandler.java @@ -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) { + + } +} diff --git a/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java b/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java index 7b82971..cf63604 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java +++ b/austin-support/src/main/java/com/java3y/austin/support/utils/AccountUtils.java @@ -22,6 +22,7 @@ public class AccountUtils { private ConfigService config; /** + * local.properties配置格式 * (key:smsAccount)短信参数示例:[{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDhDxxxxxxxx1WljQq","secretKey":"B4hwww39yxxxrrrrgxyi","smsSdkAppId":"1423123125","templateId":"1182097","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}},{"sms_20":{"url":"https://sms.yunpian.com/v2/sms/tpl_batch_send.json","apikey":"caffff8234234231b5cd7","tpl_id":"523333332","supplierId":20,"supplierName":"云片"}}] * (key:emailAccount)邮件参数示例:[{"email_10":{"host":"smtp.qq.com","port":465,"user":"23423423@qq.com","pass":"23423432432423423","from":"234@qq.com","starttlsEnable":true,"auth":true,"sslEnable":true}},{"email_20":{"host":"smtp.163.com","port":465,"user":"22222@163.com","pass":"23432423","from":"234324324234@163.com","starttlsEnable":false,"auth":true,"sslEnable":true}}] * (key:enterpriseWechatAccount)企业微信参数示例:[{"enterprise_wechat_10":{"corpId":"wwf87603333e00069c","corpSecret":"-IFWxS2222QxzPIorNV11144D915DM","agentId":10044442,"token":"rXROB3333Kf6i","aesKey":"MKZtoFxHIM44444M7ieag3r9ZPUsl"}}] @@ -29,6 +30,7 @@ public class AccountUtils { * (key:dingDingWorkNoticeAccount) 钉钉工作消息参数示例:[{"ding_ding_work_notice_10":{"appKey":"dingh6yyyyyyycrlbx","appSecret":"tQpvmkR863333yyyyyHP3QHyyyymy9Ao1yoL1oQX5NsdfsWHvWKbTu","agentId":"1523123123183622"}}] * (key:officialAccount) 微信服务号模板消息参数示例:[{"official_10":{"appId":"wxecb4693d2eef1ea7","secret":"624asdfsa1640d769ba20120821","templateId":"JHUk6eE9T5Ts7asdfsadfiKNDQsk-Q","url":"http://weixin.qq.com/download","miniProgramId":"xiaochengxuappid12345","path":"index?foo=bar"}}] * (key:miniProgramAccount) 微信小程序订阅消息参数示例:[{"mini_program_10":{"appId":"wxecb4693d2eef1ea7","appSecret":"6240870f4d91701640d769ba20120821","templateId":"JHUk6eE9T5TasdfCrQsk-Q","grantType":"client_credential","miniProgramState":"trial","page":"index?foo=bar"}}] + * (key:alipayMiniProgramAccount) 支付宝小程序订阅消息参数实例:[{"alipay_mini_program_10":{"privateKey":"MIIEvQIBADANB......","alipayPublicKey":"MIIBIjANBg...","appId":"2014********7148","userTemplateId":"MDI4YzIxMDE2M2I5YTQzYjUxNWE4MjA4NmU1MTIyYmM=","page":"page/component/index"}}] */ public T getAccount(Integer sendAccount, String apolloKey, String prefix, Class clazz) { String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY); diff --git a/pom.xml b/pom.xml index 4714d02..d6100f8 100644 --- a/pom.xml +++ b/pom.xml @@ -196,6 +196,13 @@ ${weixin-java} + + + com.alipay.sdk + alipay-sdk-java + 4.33.39.ALL + + com.aliyun