parent
bd22db52f2
commit
01d062c14a
@ -0,0 +1,86 @@
|
|||||||
|
package com.zh.lingnuo.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import com.aliyun.tea.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SmsService {
|
||||||
|
|
||||||
|
@Value("${aliyun.sms.accessKeyId}")
|
||||||
|
private String accessKeyId;
|
||||||
|
|
||||||
|
@Value("${aliyun.sms.accessKeySecret}")
|
||||||
|
private String accessKeySecret;
|
||||||
|
|
||||||
|
@Value("${aliyun.sms.region}")
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
@Value("${aliyun.sms.endpoint}")
|
||||||
|
private String endpoint;
|
||||||
|
|
||||||
|
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
|
||||||
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
.setAccessKeyId(accessKeyId)
|
||||||
|
.setAccessKeySecret(accessKeySecret)
|
||||||
|
.setEndpoint(endpoint);
|
||||||
|
return new com.aliyun.dysmsapi20170525.Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FuncRes sms(String phone,String signName,String templateCode, String templateParam) {
|
||||||
|
com.aliyun.dysmsapi20170525.Client client = null;
|
||||||
|
try {
|
||||||
|
client = createClient();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return FuncRes.fail("初始化sms服务失败");
|
||||||
|
}
|
||||||
|
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
||||||
|
.setPhoneNumbers(phone)
|
||||||
|
.setSignName(signName)
|
||||||
|
.setTemplateCode(templateCode)
|
||||||
|
.setTemplateParam(templateParam);
|
||||||
|
// .setTemplateParam("{\"company\":\"1234\",\"usrname\":\"1234\",\"password\":\"1234\"}");
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||||
|
} catch (TeaException error) {
|
||||||
|
// 如有需要,请打印 error
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
return FuncRes.fail(error.message);
|
||||||
|
} catch (Exception _error) {
|
||||||
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
// 如有需要,请打印 error
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
return FuncRes.fail(error.message);
|
||||||
|
}
|
||||||
|
return FuncRes.success("发送成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
public FuncRes smsRequest( com.aliyun.dysmsapi20170525.Client client,String signName,String templateCode,String phone, String templateParam) {
|
||||||
|
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
||||||
|
.setPhoneNumbers(phone)
|
||||||
|
.setSignName(signName)
|
||||||
|
.setTemplateCode(templateCode)
|
||||||
|
.setTemplateParam(templateParam);
|
||||||
|
// .setTemplateParam("{\"company\":\"1234\",\"usrname\":\"1234\",\"password\":\"1234\"}");
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||||
|
} catch (TeaException error) {
|
||||||
|
// 如有需要,请打印 error
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
return FuncRes.fail(error.message);
|
||||||
|
} catch (Exception _error) {
|
||||||
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
// 如有需要,请打印 error
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
return FuncRes.fail(error.message);
|
||||||
|
}
|
||||||
|
return FuncRes.success("发送成功");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue