|
|
@@ -11,6 +11,7 @@ import com.izouma.zhirongip.config.Constants;
|
|
|
import com.izouma.zhirongip.domain.SmsRecord;
|
|
|
import com.izouma.zhirongip.exception.BusinessException;
|
|
|
import com.izouma.zhirongip.repo.SmsRecordRepo;
|
|
|
+import com.mascloud.sdkclient.Client;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -19,6 +20,7 @@ import org.springframework.boot.configurationprocessor.json.JSONException;
|
|
|
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneOffset;
|
|
|
|
|
|
@@ -32,38 +34,33 @@ public class AliSmsService implements SmsService {
|
|
|
@Autowired
|
|
|
private SmsRecordRepo smsRecordRepo;
|
|
|
|
|
|
+ private Client client;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ client = Client.getInstance();
|
|
|
+ boolean success = client.login("http://112.35.4.197:15000", "sms", "3edc#EDC", "江苏移动信息系统集成有限公司(政企)");
|
|
|
+ if (!success) {
|
|
|
+ log.error("sms login failed");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String sendVerify(String phone) {
|
|
|
smsRecordRepo.findLastByPhoneAndExpiresAtAfterAndExpiredFalse(phone, LocalDateTime.now()).ifPresent(record -> {
|
|
|
if (record.getCreatedAt().plusMinutes(1L).isAfter(LocalDateTime.now())) {
|
|
|
- long sec = record.getCreatedAt().plusMinutes(1L).toInstant(ZoneOffset.UTC).getEpochSecond() - LocalDateTime.now().toInstant(ZoneOffset.UTC).getEpochSecond() + 1;
|
|
|
+ long sec = record.getCreatedAt().plusMinutes(1L).toInstant(ZoneOffset.UTC)
|
|
|
+ .getEpochSecond() - LocalDateTime.now().toInstant(ZoneOffset.UTC).getEpochSecond() + 1;
|
|
|
throw new BusinessException("请" + sec + "秒后再试");
|
|
|
}
|
|
|
});
|
|
|
|
|
|
String code = RandomStringUtils.randomNumeric(4);
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
|
|
- IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
|
|
- CommonRequest request = new CommonRequest();
|
|
|
- request.setMethod(MethodType.POST);
|
|
|
- request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
- request.setVersion("2017-05-25");
|
|
|
- request.setAction("SendSms");
|
|
|
- request.putQueryParameter("PhoneNumbers", phone);
|
|
|
- request.putQueryParameter("SignName", Constants.SMS_SIGN_NAME);
|
|
|
- request.putQueryParameter("TemplateCode", Constants.SMS_TEMPLATE_CODE_GENERIC);
|
|
|
- request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
|
|
|
try {
|
|
|
- CommonResponse response = client.getCommonResponse(request);
|
|
|
- if (response.getHttpStatus() != 200) {
|
|
|
- throw new BusinessException("发送失败,请稍后再试", response.getHttpStatus() + "," + response.getData());
|
|
|
- }
|
|
|
- log.info("send sms response {}", response.getData());
|
|
|
- JSONObject jsonObject = new JSONObject(response.getData());
|
|
|
- if (!"ok".equalsIgnoreCase(jsonObject.getString("Code"))) {
|
|
|
- throw new BusinessException("发送失败,请稍后再试", jsonObject.getString("Message"));
|
|
|
- }
|
|
|
+ int rtCode = client.sendDSMS(new String[]{phone}, "您的验证码是" + code + ",5分钟内有效", "", 1, "ubfdvkj65", null, false);
|
|
|
+ log.info("send sms response {}", rtCode);
|
|
|
+
|
|
|
smsRecordRepo.expire(phone);
|
|
|
String sessionId = RandomStringUtils.randomAlphabetic(10);
|
|
|
smsRecordRepo.save(SmsRecord.builder()
|
|
|
@@ -74,7 +71,7 @@ public class AliSmsService implements SmsService {
|
|
|
.expired(false)
|
|
|
.build());
|
|
|
return sessionId;
|
|
|
- } catch (ClientException | JSONException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
throw new BusinessException("发送失败,请稍后再试", e.getMessage());
|
|
|
}
|
|
|
@@ -82,7 +79,8 @@ public class AliSmsService implements SmsService {
|
|
|
|
|
|
@Override
|
|
|
public void verify(String phone, String code) throws SmsVerifyException {
|
|
|
- SmsRecord smsRecord = smsRecordRepo.findLastByPhoneAndExpiresAtAfterAndExpiredFalse(phone, LocalDateTime.now()).orElseThrow(new SmsVerifyException("验证码错误"));
|
|
|
+ SmsRecord smsRecord = smsRecordRepo.findLastByPhoneAndExpiresAtAfterAndExpiredFalse(phone, LocalDateTime.now())
|
|
|
+ .orElseThrow(new SmsVerifyException("验证码错误"));
|
|
|
if (!smsRecord.getCode().equalsIgnoreCase(code)) {
|
|
|
throw new BusinessException("验证码错误");
|
|
|
}
|