mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-30 21:53:55 +08:00
优化发送短信失败的情况,回退发送次数
--ALiYunSmsService.java 使用redis加1原子操作 --CaptchaServiceImpl.java 使用redis减1原子操作
This commit is contained in:
+2
-3
@@ -119,10 +119,9 @@ public class CaptchaServiceImpl implements CaptchaService {
|
|||||||
|
|
||||||
// 更新今日发送次数
|
// 更新今日发送次数
|
||||||
if (todayCount == 0) {
|
if (todayCount == 0) {
|
||||||
// 如果是今天第一次发送,设置24小时过期
|
redisUtils.increment(todayCountKey,RedisUtils.DEFAULT_EXPIRE);
|
||||||
redisUtils.set(todayCountKey, 1, RedisUtils.DEFAULT_EXPIRE);
|
|
||||||
} else {
|
} else {
|
||||||
redisUtils.set(todayCountKey, todayCount + 1, RedisUtils.DEFAULT_EXPIRE);
|
redisUtils.increment(todayCountKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送验证码短信
|
// 发送验证码短信
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import xiaozhi.common.constant.Constant;
|
import xiaozhi.common.constant.Constant;
|
||||||
import xiaozhi.common.exception.RenException;
|
import xiaozhi.common.exception.RenException;
|
||||||
|
import xiaozhi.common.redis.RedisKeys;
|
||||||
|
import xiaozhi.common.redis.RedisUtils;
|
||||||
import xiaozhi.modules.sms.service.SmsService;
|
import xiaozhi.modules.sms.service.SmsService;
|
||||||
|
import xiaozhi.modules.sms.service.common.SmsUtils;
|
||||||
import xiaozhi.modules.sys.service.SysParamsService;
|
import xiaozhi.modules.sys.service.SysParamsService;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -18,9 +21,12 @@ import xiaozhi.modules.sys.service.SysParamsService;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class ALiYunSmsService implements SmsService {
|
public class ALiYunSmsService implements SmsService {
|
||||||
private final SysParamsService sysParamsService;
|
private final SysParamsService sysParamsService;
|
||||||
|
private final SmsUtils smsUtils;
|
||||||
|
private final RedisUtils redisUtils;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendVerificationCodeSms(String phone, String VerificationCode) {
|
public void sendVerificationCodeSms(String phone, String VerificationCode) {
|
||||||
|
smsUtils.isPhoneEligibleForSmsSend(phone,true);
|
||||||
Client client = createClient();
|
Client client = createClient();
|
||||||
String SignName = sysParamsService.getValue(Constant.SysMSMParam
|
String SignName = sysParamsService.getValue(Constant.SysMSMParam
|
||||||
.ALIYUN_SMS_SIGN_NAME.getValue(),true);
|
.ALIYUN_SMS_SIGN_NAME.getValue(),true);
|
||||||
@@ -37,6 +43,9 @@ public class ALiYunSmsService implements SmsService {
|
|||||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||||
log.info("发送短信响应的requestID: {}", sendSmsResponse.getBody().getRequestId());
|
log.info("发送短信响应的requestID: {}", sendSmsResponse.getBody().getRequestId());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// 如果发送失败了退还这次发送数
|
||||||
|
String todayCountKey = RedisKeys.getSMSTodayCountKey(phone);
|
||||||
|
redisUtils.delete(todayCountKey);
|
||||||
// 错误 message
|
// 错误 message
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new RenException("短信发送失败");
|
throw new RenException("短信发送失败");
|
||||||
|
|||||||
Reference in New Issue
Block a user