开通阿里云平台短信
参考文档:https://help.aliyun.com/zh/sms/developer-reference/sdk-product-overview/?spm=a2c4g.11186623.0.0.180ec71f9R78r9
导包
引入阿里云短信SDK
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
工具类
@Component
@Slf4j
public class SendSmsUtil {
//accessKeyId
@Value("${data.sms.accessKeyId}")
public String accessKeyId;
//accessKeySecret
@Value("${data.sms.accessKeySecret}")
public String accessKeySecret;
//签名
@Value("${data.sms.signName}")
public String signName;
//区域 亚太
public static String endpoint = "dysmsapi.aliyuncs.com";
/**
* 使用AK&SK初始化账号Client
*
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.endpoint = endpoint;
return new com.aliyun.dysmsapi20170525.Client(config);
}
public SmsSendLog sendSms(String templateCode, String phone, Map<String, Object> params) throws Exception {
com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName(signName)
.setTemplateCode(templateCode)
.setPhoneNumbers(phone)
.setTemplateParam(JSONUtil.toJsonStr(params));
SmsSendLog smsSendLog = new SmsSendLog();
smsSendLog.setPhone(phone);
smsSendLog.setTemplateCode(templateCode);
smsSendLog.setContent(JSONUtil.toJsonStr(params));
smsSendLog.setTime(DateUtil.getNow());
smsSendLog.setCreateTime(DateUtil.getNow());
try {
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
if (sendSmsResponse.getStatusCode() != null && sendSmsResponse.getStatusCode() == 200) {
smsSendLog.setRequestId(sendSmsResponse.getBody().getRequestId());
log.info("发送成功,code:" + sendSmsResponse.getStatusCode());
} else {
log.info("发送失败,code:" + sendSmsResponse.getStatusCode());
}
smsSendLog.setStatus(sendSmsResponse.getStatusCode());
smsSendLog.setInfo(sendSmsResponse.getBody().message);
} catch (TeaException error) {
// 错误 message
log.error(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
smsSendLog.setRemark(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 错误 message
System.out.println(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
smsSendLog.setRemark(error.message);
} finally {
//插入日志
return smsSendLog;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sms_send_log")
public class SmsSendLog implements Serializable {
private static final long serialVersionUID = 1L;
//主键
@TableId(type = IdType.AUTO)
private Long id;
private String phone;
private String templateCode;
private String content;
private Date time;
private String requestId;
private Integer status;
private String info;
private String remark;
private Date createTime;
}