|
|
@@ -19,8 +19,11 @@ 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.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneOffset;
|
|
|
import java.util.Date;
|
|
|
@@ -41,7 +44,9 @@ public class AliSmsService implements SmsService {
|
|
|
private String sellOutCode;
|
|
|
|
|
|
@Autowired
|
|
|
- private SmsRecordRepo smsRecordRepo;
|
|
|
+ private SmsRecordRepo smsRecordRepo;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
@Override
|
|
|
public String sendVerify(String phone) {
|
|
|
@@ -102,6 +107,10 @@ public class AliSmsService implements SmsService {
|
|
|
|
|
|
@Override
|
|
|
public String verify(String phone, String code) {
|
|
|
+ if (Boolean.FALSE.equals(redisTemplate.opsForValue()
|
|
|
+ .setIfAbsent("verify::" + phone, 1, Duration.ofSeconds(10)))) {
|
|
|
+ throw new BusinessException("频繁操作,请稍后再试");
|
|
|
+ }
|
|
|
SmsRecord smsRecord = smsRecordRepo.findLastByPhoneAndExpiresAtAfterAndExpiredFalse(phone, LocalDateTime.now())
|
|
|
.orElseThrow(new BusinessException("验证码错误"));
|
|
|
if (!smsRecord.getCode().equalsIgnoreCase(code)) {
|
|
|
@@ -151,4 +160,8 @@ public class AliSmsService implements SmsService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Scheduled(cron = "0 0 3 * * ?")
|
|
|
+ void deleteExpired() {
|
|
|
+ smsRecordRepo.deleteByCreatedAtBefore(LocalDateTime.now().minusDays(2));
|
|
|
+ }
|
|
|
}
|