Commit e5a7b8474ff5ec0f0909b3c78af8a5b116fc1373

Authored by YunaiV
1 parent 8a6c48f2

fix:短信验证码的每日发送条数不正确

README.md
... ... @@ -158,7 +158,7 @@ ps:核心功能已经实现,正在对接微信小程序中...
158 158  
159 159 | 框架 | 说明 | 版本 | 学习指南 |
160 160 |---------------------------------------------------------------------------------------------|-----------------------|-----------|----------------------------------------------------------------|
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) |
162 162 | [MySQL](https://www.mysql.com/cn/) | 数据库服务器 | 5.7 | |
163 163 | [Druid](https://github.com/alibaba/druid) | JDBC 连接池、监控组件 | 1.2.11 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) |
164 164 | [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
1 1 package cn.iocoder.yudao.framework.common.util.date;
2 2  
  3 +import cn.hutool.core.date.DateUtil;
  4 +
3 5 import java.time.Duration;
4 6 import java.util.Calendar;
5 7 import java.util.Date;
... ... @@ -120,4 +122,17 @@ public class DateUtils {
120 122 return c.getTime();
121 123 }
122 124  
  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 +
123 138 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImpl.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.sms;
3 3 import cn.hutool.core.lang.Assert;
4 4 import cn.hutool.core.map.MapUtil;
5 5 import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
  6 +import cn.iocoder.yudao.framework.common.util.date.DateUtils;
6 7 import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO;
7 8 import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
8 9 import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
... ... @@ -52,21 +53,22 @@ public class SmsCodeServiceImpl implements SmsCodeService {
52 53 // 校验是否可以发送验证码,不用筛选场景
53 54 SmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, null,null);
54 55 if (lastSmsCode != null) {
55   - if (lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。
56   - throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY);
57   - }
58 56 if (System.currentTimeMillis() - lastSmsCode.getCreateTime().getTime()
59 57 < smsCodeProperties.getSendFrequency().toMillis()) { // 发送过于频繁
60 58 throw ServiceExceptionUtil.exception(SMS_CODE_SEND_TOO_FAST);
61 59 }
  60 + if (DateUtils.isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限
  61 + lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。
  62 + throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY);
  63 + }
62 64 // TODO 芋艿:提升,每个 IP 每天可发送数量
63 65 // TODO 芋艿:提升,每个 IP 每小时可发送数量
64 66 }
65 67  
66 68 // 创建验证码记录
67 69 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)
70 72 .createIp(ip).used(false).build();
71 73 smsCodeMapper.insert(newSmsCode);
72 74 return code;
... ...