Config
SmsClient
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class SmsClient {
@Autowired
private SmsProperties smsProperties;
@Bean
public Client createClient() throws Exception{
Config aliyunConfig = new Config()
.setAccessKeyId(smsProperties.getAccessKeyId())
.setAccessKeySecret(smsProperties.getAccessKeySecret())
.setEndpoint(smsProperties.getEndpoint());
return new Client(aliyunConfig);
}
}
yaml
leyou:
sms:
accessKeyId:
accessKeySecret:
endpoint:
signName:
templateCode:
code:
Properties
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "meng.sms")
public class SmsProperties {
@ApiModelProperty(value = "Ali yun AccessKey")
private String accessKeyId;
@ApiModelProperty(value = "Ali yun AccessKeySecret")
private String accessKeySecret;
@ApiModelProperty(value = "Ali yun SMS 服务域名")
private String endpoint;
@ApiModelProperty(value = "短信服务签名")
private String signName;
@ApiModelProperty(value = "短信服务模板编码")
private String templateCode;
@ApiModelProperty(value = "短信模板中验证码的占位符")
private String code;
}
Utils(逐步完善)
发送短信(非群发)
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.meng.common.constant.SmsConstants;
import com.meng.common.exception.constant.ExceptionEnum;
import com.meng.common.exception.entity.LeyouException;
import com.meng.sms.config.SmsClient;
import com.meng.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SmsUtils {
@Autowired
private SmsClient smsClient;
@Autowired
private SmsProperties smsProperties;
public void sendSmsMessage(String phoneNumber, String smsCode) {
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phoneNumber)
.setSignName(smsProperties.getSignName())
.setTemplateCode(smsProperties.getTemplateCode())
.setTemplateParam("{\"" + smsProperties.getCode() + "\": \"" + smsCode + "\"}");
RuntimeOptions runtimeOptions = new RuntimeOptions();
try {
SendSmsResponse sendSmsResponse = smsClient.createClient().sendSmsWithOptions(sendSmsRequest, runtimeOptions);
SendSmsResponseBody sendSmsResponseBody = sendSmsResponse.getBody();
if (sendSmsResponseBody.getCode().equals(SmsConstants.SMS_RESPONSE_STATUS)) {
log.info("阿里云短信发送成功(非群发), Message: {}", sendSmsResponseBody.getMessage());
}else {
log.error("阿里云短信发送失败(非群发), Message: {}", sendSmsResponseBody.getMessage());
}
} catch (Exception e) {
log.error("阿里云短信发送失败(非群发), 原因: {}",e.getMessage());
throw new LeyouException(ExceptionEnum.SEND_MESSAGE_ERROR);
}
}
}