From a088d6440e687f6a8062cbfd50549f6517218dda Mon Sep 17 00:00:00 2001 From: xueye Date: Fri, 2 Jul 2021 15:01:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E7=9F=AD=E4=BF=A1=E9=80=9A=E7=9F=A5=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notify/sms/aliyun/AliyunSmsNotifier.java | 100 ++++++++++++++++++ .../sms/aliyun/AliyunSmsNotifierProvider.java | 65 ++++++++++++ .../notify/sms/aliyun/AliyunSmsTemplate.java | 60 +++++++++++ .../com/cicdi/notify/sms/AliyunSmsTest.java | 66 ++++++++++++ 4 files changed, 291 insertions(+) create mode 100644 notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifier.java create mode 100644 notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifierProvider.java create mode 100644 notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsTemplate.java create mode 100644 notify-sms/src/test/java/com/cicdi/notify/sms/AliyunSmsTest.java diff --git a/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifier.java b/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifier.java new file mode 100644 index 0000000..2b4f241 --- /dev/null +++ b/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifier.java @@ -0,0 +1,100 @@ +package com.cicdi.notify.sms.aliyun; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.aliyuncs.CommonRequest; +import com.aliyuncs.CommonResponse; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.http.MethodType; +import com.aliyuncs.profile.DefaultProfile; +import com.aliyuncs.profile.IClientProfile; +import com.cicdi.notify.*; +import com.cicdi.notify.sms.SmsProvider; +import com.cicdi.notify.template.TemplateManager; + + +import java.util.Map; +import java.util.Objects; + +public class AliyunSmsNotifier extends AbstractNotifier { + + private final IAcsClient client; + private final int connectTimeout = 1000; + private final int readTimeout = 5000; + + private String notifierId; + + public String getNotifierId() { + return notifierId; + } + + private String domain = "dysmsapi.aliyuncs.com"; + private String regionId = "cn-hangzhou"; + + public AliyunSmsNotifier(NotifierProperties profile, TemplateManager templateManager) { + super(templateManager); + Map config = profile.getConfiguration(); + DefaultProfile defaultProfile = DefaultProfile.getProfile( + this.regionId = (String) Objects.requireNonNull(config.get("regionId"), "regionId不能为空"), + (String) Objects.requireNonNull(config.get("accessKeyId"), "accessKeyId不能为空"), + (String) Objects.requireNonNull(config.get("secret"), "secret不能为空") + ); + this.client = new DefaultAcsClient(defaultProfile); + this.domain = (String) config.getOrDefault("domain", domain); + this.notifierId = profile.getId(); + } + + public AliyunSmsNotifier(IClientProfile profile, TemplateManager templateManager) { + this(new DefaultAcsClient(profile), templateManager); + } + + public AliyunSmsNotifier(IAcsClient client, TemplateManager templateManager) { + super(templateManager); + this.client = client; + } + + @Override + public NotifyType getType() { + return DefaultNotifyType.sms; + } + + @Override + public Provider getProvider() { + return SmsProvider.aliyunSms; + } + + @Override + public void send(AliyunSmsTemplate template, Map context) { + try { + CommonRequest request = new CommonRequest(); + request.setSysMethod(MethodType.POST); + request.setSysDomain(domain); + request.setSysVersion("2017-05-25"); + request.setSysAction("SendSms"); + request.setSysConnectTimeout(connectTimeout); + request.setSysReadTimeout(readTimeout); + request.putQueryParameter("RegionId", regionId); + request.putQueryParameter("PhoneNumbers", template.getPhoneNumber()); + request.putQueryParameter("SignName", template.getSignName()); + request.putQueryParameter("TemplateCode", template.getCode()); + request.putQueryParameter("TemplateParam", template.createTtsParam(context)); + + CommonResponse response = client.getCommonResponse(request); + + JSONObject json = JSON.parseObject(response.getData()); + if (!"ok".equalsIgnoreCase(json.getString("Code"))) { + // TODO 自定义异常 +// return new BusinessException(json.getString("Message"), json.getString("Code")); + throw new Exception("json.getString(\"Message\"), json.getString(\"Code\")"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void close() { + client.shutdown(); + } +} diff --git a/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifierProvider.java b/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifierProvider.java new file mode 100644 index 0000000..614e412 --- /dev/null +++ b/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsNotifierProvider.java @@ -0,0 +1,65 @@ +package com.cicdi.notify.sms.aliyun; + +import com.alibaba.fastjson.JSON; +import com.cicdi.notify.*; +import com.cicdi.notify.sms.SmsProvider; +import com.cicdi.notify.template.TemplateManager; +import com.cicdi.notify.template.TemplateProperties; +import com.cicdi.notify.template.TemplateProvider; + +/** + * 阿里云短信通知服务 + * + */ +public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProvider { + + private final TemplateManager templateManager; + + public AliyunSmsNotifierProvider(TemplateManager templateManager) { + this.templateManager = templateManager; + } + + @Override + public Provider getProvider() { + return SmsProvider.aliyunSms; + } + +// public static final DefaultConfigMetadata templateConfig = new DefaultConfigMetadata("阿里云短信模版", +// "https://help.aliyun.com/document_detail/108086.html") +// .add("signName", "签名", "", new StringType()) +// .add("code", "模版编码", "", new StringType()) +// .add("phoneNumber", "收信人", "", new StringType()); +// +// public static final DefaultConfigMetadata notifierConfig = new DefaultConfigMetadata("阿里云API配置" +// ,"https://help.aliyun.com/document_detail/101300.html") +// .add("regionId", "regionId", "regionId", new StringType()) +// .add("accessKeyId", "accessKeyId", "", new StringType()) +// .add("secret", "secret", "", new StringType()); +// +// @Override +// public ConfigMetadata getTemplateConfigMetadata() { +// return templateConfig; +// } +// +// @Override +// public ConfigMetadata getNotifierConfigMetadata() { +// return notifierConfig; +// } + + @Override + public AliyunSmsTemplate createTemplate(TemplateProperties properties) { + // TODO 验证工具 + //return ValidatorUtils.tryValidate(JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class)); + return JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class); + } + + @Override + public NotifyType getType() { + return DefaultNotifyType.sms; + } + + @Override + public AliyunSmsNotifier createNotifier(NotifierProperties properties) { + return new AliyunSmsNotifier(properties, templateManager); + } +} diff --git a/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsTemplate.java b/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsTemplate.java new file mode 100644 index 0000000..0da8dd4 --- /dev/null +++ b/notify-sms/src/main/java/com/cicdi/notify/sms/aliyun/AliyunSmsTemplate.java @@ -0,0 +1,60 @@ +package com.cicdi.notify.sms.aliyun; + +import com.alibaba.fastjson.JSON; +import com.cicdi.notify.template.Template; + +import java.util.Map; + +/** + * 阿里云短信模版 + * + * @since 1.3 + */ +public class AliyunSmsTemplate implements Template { + + //签名名称 + private String signName; + + //模版编码 + private String code; + + private String phoneNumber; + + private Map param; + + public String createTtsParam(Map ctx) { + return JSON.toJSONString(ctx); + } + + public String getSignName() { + return signName; + } + + public void setSignName(String signName) { + this.signName = signName; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public Map getParam() { + return param; + } + + public void setParam(Map param) { + this.param = param; + } +} diff --git a/notify-sms/src/test/java/com/cicdi/notify/sms/AliyunSmsTest.java b/notify-sms/src/test/java/com/cicdi/notify/sms/AliyunSmsTest.java new file mode 100644 index 0000000..0a2e597 --- /dev/null +++ b/notify-sms/src/test/java/com/cicdi/notify/sms/AliyunSmsTest.java @@ -0,0 +1,66 @@ +package com.cicdi.notify.sms; + +import com.cicdi.notify.*; +import com.cicdi.notify.sms.aliyun.AliyunSmsNotifierProvider; +import com.cicdi.notify.template.AbstractTemplateManager; +import com.cicdi.notify.template.Template; +import com.cicdi.notify.template.TemplateManager; +import com.cicdi.notify.template.TemplateProperties; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xueye + */ +public class AliyunSmsTest { + + @Test + public void test() { + // 通知器配置管理器 + NotifyConfigManager notifyConfigManager = new NotifyConfigManager() { + @Override + public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) { + NotifierProperties notifierProperties = new NotifierProperties(); + notifierProperties.setType(DefaultNotifyType.sms.getId()); + notifierProperties.setProvider(SmsProvider.aliyunSms.getId()); + notifierProperties.setId("12"); + + Map config = new HashMap<>(); + config.put("regionId", ""); + config.put("accessKeyId", ""); + config.put("secret", ""); + + notifierProperties.setConfiguration(config); + + return notifierProperties; + } + }; + + // 模板管理器 + TemplateManager templateManager = new AbstractTemplateManager() { + @Override + protected TemplateProperties getProperties(NotifyType type, String id) { + TemplateProperties templateProperties = new TemplateProperties(); + templateProperties.setType(DefaultNotifyType.sms.getId()); + templateProperties.setProvider(SmsProvider.aliyunSms.getId()); + + templateProperties.setTemplate("{\"signName\":\"Hello\",\"phoneNumber\":[\"xueye404@qq.com\"],\"code\":\"1322\"}"); + + return templateProperties; + } + }; + + NotifierManager notifierManager = new DefaultNotifierManager(notifyConfigManager); + AliyunSmsNotifierProvider aliyunSmsNotifierProvider = new AliyunSmsNotifierProvider(templateManager); + // register + notifierManager.registerProvider(aliyunSmsNotifierProvider); + templateManager.register(aliyunSmsNotifierProvider); + + Notifier