短信sdk使用
demo
// 定义短信模版枚举,一个项目定义一个,存放所用到的所有模版配置键,不带默认前缀(sms.),在sdk中会带上前缀去获取配置文件中的配置(sms.key)
public enum SmsTemplateCodeEnum implements ITemplateCode {
/** 个人签约认证短信模版 **/
PERSON_VERIFY("templatePersonVerify");
/** 模版配置键 **/
private String templateCodeKey;
SmsTemplateCodeEnum(String templateCodeKey){
this.templateCodeKey = templateCodeKey;
}
@Override
public String getTemplateCodeKey() {
return this.templateCodeKey;
}
}
// 注入service
@Autowired
private SdkSmsService sdkSmsService;
// 方法使用
public void send(){
// 使用枚举方式,调用发送验证码方法
AlitaResult<Boolean> response = sdkSmsService.sendVerificationCode(SmsTemplateCodeEnum.PERSON_VERIFY, phone, verificationCode);
// 使用模版code,调用发送验证码方法
AlitaResult<Boolean> response = sdkSmsService.sendVerificationCode("SMS_******", phone, verificationCode);
}
// 配置文件添加配置,sms前缀
sms.templatePersonVerify=SMS_******
说明
SdkSmsService 提供方法:
// 自定义dto实现ISendDTO接口,短信模版内参数对象自定义;这种适合模版中参数比较多需要单独定义对象接收或者勤快的你
<T> AlitaResult<Boolean> send(ISendDTO<T> iSendDTO);
// 发送通用短信验证码,短信模版中配置的参数只有code;(直接传递模版code)
AlitaResult<Boolean> sendVerificationCode(String templateCode, String phone, String code);
// 同上,可使用枚举对象
AlitaResult<Boolean> sendVerificationCode(ITemplateCode iTemplateCode, String phone, String code);
// 发送简单的短信,短信模版中配置的参数用map接收,短信模版中配置什么,map中key使用对应键;(直接传递模版code)
AlitaResult<Boolean> sendSimple(String templateCode, String phone, Map<String, Object> params);
// 同上,可使用枚举对象
AlitaResult<Boolean> sendSimple(ITemplateCode iTemplateCode, String phone, Map<String, Object> params);
AlitaResult 结果说明
- success-false 未调起第三方或未拿到第三方返回值,视为未调起第三方,有失败原因(msg);如必要参数为空等
- success-true 调用到第三方并拿到返回值,
- data-true 发送成功,
- data-false 发送失败并返回失败原因(msg) ;如手机号已停机等
初始配置
# 短信开关
sms.enable=true
# 服务地址
sms.domain=dysmsapi.aliyuncs.com
# 密钥id
sms.accessKeyId=
# 密钥
sms.accessSecret=
# API支持的RegionID
sms.regionId=default
# API 的版本号
sms.version=2017-05-25
# 签名名称
sms.signName=鑫业态
# 非生产环境真实调用次数限制
sms.numLimit=3