Skip to content

Commit e5a7b84

Browse files
committed
fix:短信验证码的每日发送条数不正确
1 parent 8a6c48f commit e5a7b84

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

  • yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date
  • yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ ps:核心功能已经实现,正在对接微信小程序中...
158158

159159
| 框架 | 说明 | 版本 | 学习指南 |
160160
|---------------------------------------------------------------------------------------------|-----------------------|-----------|----------------------------------------------------------------|
161-
| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.6.9 | [文档](https://github.com/YunaiV/SpringBoot-Labs) |
161+
| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.6.10 | [文档](https://github.com/YunaiV/SpringBoot-Labs) |
162162
| [MySQL](https://www.mysql.com/cn/) | 数据库服务器 | 5.7 | |
163163
| [Druid](https://github.com/alibaba/druid) | JDBC 连接池、监控组件 | 1.2.11 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) |
164164
| [MyBatis Plus](https://mp.baomidou.com/) | MyBatis 增强工具包 | 3.5.2 | [文档](http://www.iocoder.cn/Spring-Boot/MyBatis/?yudao) |

yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.iocoder.yudao.framework.common.util.date;
22

3+
import cn.hutool.core.date.DateUtil;
4+
35
import java.time.Duration;
46
import java.util.Calendar;
57
import java.util.Date;
@@ -120,4 +122,17 @@ public static Date addDate(Date date, int field, int amount) {
120122
return c.getTime();
121123
}
122124

125+
/**
126+
* 是否今天
127+
*
128+
* @param date 日期
129+
* @return 是否
130+
*/
131+
public static boolean isToday(Date date) {
132+
if (date == null) {
133+
return false;
134+
}
135+
return DateUtil.isSameDay(date, new Date());
136+
}
137+
123138
}

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.lang.Assert;
44
import cn.hutool.core.map.MapUtil;
55
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
6+
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
67
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO;
78
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
89
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
@@ -52,21 +53,22 @@ private String createSmsCode(String mobile, Integer scene, String ip) {
5253
// 校验是否可以发送验证码,不用筛选场景
5354
SmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, null,null);
5455
if (lastSmsCode != null) {
55-
if (lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。
56-
throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY);
57-
}
5856
if (System.currentTimeMillis() - lastSmsCode.getCreateTime().getTime()
5957
< smsCodeProperties.getSendFrequency().toMillis()) { // 发送过于频繁
6058
throw ServiceExceptionUtil.exception(SMS_CODE_SEND_TOO_FAST);
6159
}
60+
if (DateUtils.isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限
61+
lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。
62+
throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY);
63+
}
6264
// TODO 芋艿:提升,每个 IP 每天可发送数量
6365
// TODO 芋艿:提升,每个 IP 每小时可发送数量
6466
}
6567

6668
// 创建验证码记录
6769
String code = String.valueOf(randomInt(smsCodeProperties.getBeginCode(), smsCodeProperties.getEndCode() + 1));
68-
SmsCodeDO newSmsCode = SmsCodeDO.builder().mobile(mobile).code(code)
69-
.scene(scene).todayIndex(lastSmsCode != null ? lastSmsCode.getTodayIndex() + 1 : 1)
70+
SmsCodeDO newSmsCode = SmsCodeDO.builder().mobile(mobile).code(code).scene(scene)
71+
.todayIndex(lastSmsCode != null && DateUtils.isToday(lastSmsCode.getCreateTime()) ? lastSmsCode.getTodayIndex() + 1 : 1)
7072
.createIp(ip).used(false).build();
7173
smsCodeMapper.insert(newSmsCode);
7274
return code;

0 commit comments

Comments
 (0)