针对阿里云、华为云、腾讯云和我们的用户中心colibri,为了使上层开发与具体应用解耦,所以提供SDK适配各个云厂商,供上层服务调用。可以通过简单的切换配置文件,达到切换不同厂商的功能。
sms-core 是为了适配多云的短信服务而开发的 SDK,用于支持产品在多个云的环境部署运行。产品可以通过简单的修改配置,不用修改代码就能完成在多云之间的平滑部署迁移。
基于sms-core提供的便捷,为了在使用上可以更加方便,我们提供了sms-spring-boot-starter,其内部是基于sms-core的实现,为上层提供了自动配置的功能,也就是starter包。
<!-- scg maven仓库 -->
<repository>
<id>scg-private</id>
<name>maven-scg-private</name>
<url>http://packages.hsifue.cn/artifactory/maven-scg-private/</url>
</repository>
<dependency>
<groupId>com.glodon.cloud</groupId>
<artifactId>sms-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>
阿里云:
glodon:
sms:
# 客户端类别,取值:ali/huawei/tencent/colibri
client-type: ali
access-key: xxxxxx
access-secret: xxxxxx
# 签名名称
sign-name: 噫吁唏艺术馆
# 模板参数相关配置
template-params:
# 模板参数名,可自定义
- template-name: 展览预约审核通过短信模板
# 模板参数id
template-id: SMS_187940161
# 模板参数名称列表
template-param-name:
- member
- membername
- phonenum
- ExhibitionName
- time
- num
# 同上
- template-name: 审核拒绝短信通知模板-新
template-id: SMS_187930179
# 如果该模板没有参数,此处只配置 template-param-name: 即可
template-param-name:
华为云:
glodon:
sms:
# 客户端类别
client-type: huawei
access-key: xxxxxx
access-secret: xxxxxx
# 华为云需要配置 channelId,具体可在开通华为云短信服务时查看
channelId: xxxxxx
# 同 阿里云 配置
template-params:
- template-name: xxxxxx
template-id: xxxxxx
template-param-name:
- xxx
- xxx
- xxx
腾讯云:
glodon:
sms:
# 客户端类别
client-type: tencent
access-key: xxxxxx
access-secret: xxxxxx
sign-name: 猿人公众号
# 腾讯云需要配置 smsSdkAppid, 具体可在开通腾讯云短信服务时查看(可在短信控制台,应用管理-应用列表中查看)
sms-sdk-appid: 1400493559
# 同 阿里云 配置
template-params:
- template-name: 猿人短信验证码
template-id: 886885
template-param-name:
- template-name: 预约失败通知
template-id: 886887
template-param-name:
用户中心:
glodon:
sms:
client-type: colibri
access-key: xxxxxx
access-secret: xxxxxx
# 同 阿里云 配置
template-params:
- template-name: 304永利集团用户中心短信服务无参数
template-id: GlodonSmsTest1
template-param-name:
- template-name: 304永利集团用户中心短信服务测试模板
template-id: GlodonSmsTest
template-param-name:
- userName
- projectName
- actionLink
配置完成后,就可以在代码中注入smsClient来使用。
@Resource
private SmsClient smsClient;
// 阿里云平台短信服务示例
// 发送带模板参数的短信
@Test
public void smsWithParamTest() {
String templateName = "展览预约审核通过短信模板";
List<String> templateParamValues = new ArrayList<String>(){{
add("小明");
add("小明");
add("18210449300");
add("浏览");
add("2020-7-31");
add("5");
}};
// 下发号码
Set<String> phoneNumbers = new HashSet<String>() {{
add("182XXXXXXXX");
}};
// 构建发送短信消息体,阿里云只需要请求参数和下发号码
SendSmsRequest sendSmsRequest = new SendSmsRequest.Builder()
.templateName(templateName)
.templateParamValues(templateParamValues)
.phoneNumbers(phoneNumbers)
.build();
SendSmsResponse<?> sendSmsResponse = smsClient.sendSms(sendSmsRequest);
System.out.println(sendSmsResponse.toString());
Assert.assertEquals("SUCCESS", sendSmsResponse.getSendResult());
if ("SUCCESS".equals(sendSmsResponse.getSendResult())) {
// 表示调用成功
System.out.println("sendSms接口调用成功");
} else {
// 表示调用失败
System.out.println("sendSms接口调用失败,错误码:" + sendSmsResponse.getHttpStatusCode());
}
}
// 发送不带模板参数的短信
@Test
public void smsWithoutParamTest() {
String templateName = "审核拒绝短信通知模板-新";
// 下发号码
Set<String> phoneNumbers = new HashSet<String>() {{
add("182XXXXXXXX");
}};
// 构建发送短信消息体,阿里云只需要请求参数和下发号码
SendSmsRequest sendSmsRequest = new SendSmsRequest.Builder()
.templateName(templateName)
.templateParamValues(null)
.phoneNumbers(phoneNumbers)
.build();
SendSmsResponse<?> sendSmsResponse = smsClient.sendSms(sendSmsRequest);
System.out.println(sendSmsResponse.toString());
Assert.assertEquals("SUCCESS", sendSmsResponse.getSendResult());
if ("SUCCESS".equals(sendSmsResponse.getSendResult())) {
// 表示调用成功
System.out.println("sendSms接口调用成功");
} else {
// 表示调用失败
System.out.println("sendSms接口调用失败,错误码:" + sendSmsResponse.getHttpStatusCode());
}
}