service接口
BaseResult sendMessage(String phoneNumber, String code);
impl
public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
@Override
@SneakyThrows
public BaseResult sendMessage(String phoneNumber, String code) {
Client client = createClient(accessKeyId, accessKeySecret);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName("阿里云短信测试")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers(phoneNumber)
.setTemplateParam("{\"code\":\"" + code + "\"}");
RuntimeOptions runtime = new RuntimeOptions();
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
SendSmsResponseBody body = sendSmsResponse.getBody();
String code1 = body.getCode();
if ("OK".equals(code1)) {
return new BaseResult(200, body.getCode(), body.getMessage());
} else {
return new BaseResult(500, body.getCode(), body.getMessage());
}
}