remove
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package org.springblade.modules.resource.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.aliyun.dypnsapi20170525.Client;
|
||||
import com.aliyun.dypnsapi20170525.models.CheckSmsVerifyCodeRequest;
|
||||
import com.aliyun.dypnsapi20170525.models.CheckSmsVerifyCodeResponse;
|
||||
import com.aliyun.dypnsapi20170525.models.SendSmsVerifyCodeRequest;
|
||||
import com.aliyun.dypnsapi20170525.models.SendSmsVerifyCodeResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AliyunDynpsService {
|
||||
|
||||
private Client smsClient;
|
||||
|
||||
private static final String AccessKeyId = "LTAI5tPYSrdSipANposndbrt";
|
||||
private static final String AccessKeySecret = "ldVngFWpwPtTQgKzbJK3GrZoRmPIX2";
|
||||
private static final String EndPoint = "dypnsapi.aliyuncs.com";
|
||||
private static final String RegionId = "cn-hangzhou";
|
||||
private static final String SignName = "速通互联验证平台";
|
||||
private static final String TemplateCode = "100001";
|
||||
private static final String Code = """
|
||||
{
|
||||
"code": "##code##",
|
||||
"min": "15"
|
||||
}
|
||||
""";
|
||||
|
||||
@PostConstruct
|
||||
public void initSmsConfig() {
|
||||
Config config = new Config()
|
||||
.setAccessKeyId(AccessKeyId)
|
||||
.setAccessKeySecret(AccessKeySecret)
|
||||
.setEndpoint(EndPoint)
|
||||
.setRegionId(RegionId);
|
||||
|
||||
try {
|
||||
smsClient = new Client(config);
|
||||
} catch (Exception e) {
|
||||
log.error("初始化阿里云短信服务配置出错 : [{}]", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean sendSms(String phone) {
|
||||
|
||||
SendSmsVerifyCodeRequest sendSmsVerifyCodeRequest = new SendSmsVerifyCodeRequest()
|
||||
.setSignName(SignName)
|
||||
.setTemplateCode(TemplateCode)
|
||||
.setTemplateParam(Code)
|
||||
.setPhoneNumber(phone);
|
||||
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
SendSmsVerifyCodeResponse resp = smsClient.sendSmsVerifyCodeWithOptions(sendSmsVerifyCodeRequest, runtime);
|
||||
log.info("发送短信验证码返回数据: [{}]", JSONObject.toJSONString(resp));
|
||||
String bodyCode = resp.getBody().getCode();
|
||||
if (!bodyCode.equals(SmsCodeEnum.SUCCESS.getCode())) {
|
||||
log.error("发送短信验证码失败: [{}]", SmsCodeEnum.getByCode(bodyCode).getMessage());
|
||||
throw new RuntimeException(SmsCodeEnum.getByCode(bodyCode).getMessage());
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
|
||||
|
||||
} catch (TeaException te) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("发送短信验证码错误 message: [{}]", te.getMessage());
|
||||
// 诊断地址
|
||||
log.error("发送短信验证码诊断地址: [{}]", te.getData().get("Recommend"));
|
||||
|
||||
throw new RuntimeException(SmsCodeEnum.getByCode(te.code).getMessage());
|
||||
|
||||
} catch (Exception e) {
|
||||
TeaException error = new TeaException(e.getMessage(), e);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("发送短信验证码TeaException错误 message: [{}]", error.getMessage());
|
||||
// 诊断地址
|
||||
throw new RuntimeException(SmsCodeEnum.getByCode(error.code).getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean verifySms(String phone, String code) {
|
||||
|
||||
CheckSmsVerifyCodeRequest sendSmsVerifyCodeRequest = new com.aliyun.dypnsapi20170525.models.CheckSmsVerifyCodeRequest();
|
||||
sendSmsVerifyCodeRequest.setPhoneNumber(phone);
|
||||
sendSmsVerifyCodeRequest.setVerifyCode(code);
|
||||
|
||||
log.info("验证短信验证码请求数据: [{}]", JSONObject.toJSONString(sendSmsVerifyCodeRequest));
|
||||
|
||||
try {
|
||||
CheckSmsVerifyCodeResponse response = smsClient.checkSmsVerifyCode(sendSmsVerifyCodeRequest);
|
||||
log.info("验证短信验证码返回数据: [{}]", JSONObject.toJSONString(response));
|
||||
String bodyCode = response.getBody().getCode();
|
||||
if (!bodyCode.equals(SmsCodeEnum.SUCCESS.getCode())) {
|
||||
log.error("验证短信验证码失败: [{}]", response.getBody().getMessage());
|
||||
throw new RuntimeException(response.getBody().getMessage());
|
||||
}
|
||||
|
||||
return Boolean.TRUE;
|
||||
|
||||
} catch (TeaException te) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("验证短信验证码错误 message: [{}]", te.getMessage());
|
||||
// 诊断地址
|
||||
log.error("验证短信验证码地址: [{}]", te.getData().get("Recommend"));
|
||||
throw new RuntimeException("短信验证码验证失败");
|
||||
} catch (Exception e) {
|
||||
TeaException error = new TeaException(e.getMessage(), e);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("验证短信验证码TeaException错误 message: [{}]", error.getMessage());
|
||||
// 诊断地址
|
||||
log.error("验证短信验证码TeaException诊断地址: [{}]", error.getData().get("Recommend"));
|
||||
throw new RuntimeException("短信验证码验证失败");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.springblade.modules.resource.utils;
|
||||
|
||||
public enum SmsCodeEnum {
|
||||
|
||||
|
||||
SUCCESS("OK", "发送成功"),
|
||||
ILLEGAL_MOBILE("ILLEGAL_MOBILE", "手机格式不正确"),
|
||||
BUSINESS_LIMIT_CONTROL("BUSINESS_LIMIT_CONTROL", "超出每日发送次数"),
|
||||
INVALID_PARAMETERS("INVALID_PARAMETERS", "违法参数攻击"),
|
||||
UNKNOWN("UNKNOWN", "短信发送失败"),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String message;
|
||||
|
||||
SmsCodeEnum(String code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public static SmsCodeEnum getByCode(String code) {
|
||||
for (SmsCodeEnum item : SmsCodeEnum.values()) {
|
||||
if (item.getCode().equals(code)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user