|
|
@@ -1,5 +1,6 @@
|
|
|
package com.izouma.zhirongip.service.sms;
|
|
|
|
|
|
+
|
|
|
import com.aliyuncs.CommonRequest;
|
|
|
import com.aliyuncs.CommonResponse;
|
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
|
@@ -11,19 +12,16 @@ 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.apache.commons.lang.RandomStringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
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;
|
|
|
-import java.util.UUID;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@@ -35,45 +33,49 @@ 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", "2wsx@WSX", "江苏移动信息系统集成有限公司(政企)");
|
|
|
- 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 {
|
|
|
- int rtCode = client.sendTSMS(new String[]{phone}, "2c7838661d09499a9d8e045cbd44ecd5",
|
|
|
- new String[]{code}, "", 5, "ubfdvkj65", UUID.randomUUID().toString());
|
|
|
- log.info("send sms response {}", rtCode);
|
|
|
-
|
|
|
+ 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"));
|
|
|
+ }
|
|
|
smsRecordRepo.expire(phone);
|
|
|
String sessionId = RandomStringUtils.randomAlphabetic(10);
|
|
|
smsRecordRepo.save(SmsRecord.builder()
|
|
|
- .sessionId(sessionId)
|
|
|
- .phone(phone)
|
|
|
- .code(code)
|
|
|
- .expiresAt(LocalDateTime.now().plusMinutes(5))
|
|
|
- .expired(false)
|
|
|
- .build());
|
|
|
+ .sessionId(sessionId)
|
|
|
+ .phone(phone)
|
|
|
+ .code(code)
|
|
|
+ .expiresAt(LocalDateTime.now().plusMinutes(5))
|
|
|
+ .expired(false)
|
|
|
+ .build());
|
|
|
return sessionId;
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (ClientException | JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
throw new BusinessException("发送失败,请稍后再试", e.getMessage());
|
|
|
}
|
|
|
@@ -81,8 +83,7 @@ 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("验证码错误");
|
|
|
}
|