From e5a7b8474ff5ec0f0909b3c78af8a5b116fc1373 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 29 Jul 2022 00:56:17 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E7=9F=AD=E4=BF=A1=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E7=9A=84=E6=AF=8F=E6=97=A5=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=9D=A1=E6=95=B0=E4=B8=8D=E6=AD=A3=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../framework/common/util/date/DateUtils.java | 15 +++++++++++++++ .../system/service/sms/SmsCodeServiceImpl.java | 12 +++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 269a48f5b..891fa10da 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ ps:核心功能已经实现,正在对接微信小程序中... | 框架 | 说明 | 版本 | 学习指南 | |---------------------------------------------------------------------------------------------|-----------------------|-----------|----------------------------------------------------------------| -| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.6.9 | [文档](https://github.com/YunaiV/SpringBoot-Labs) | +| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.6.10 | [文档](https://github.com/YunaiV/SpringBoot-Labs) | | [MySQL](https://www.mysql.com/cn/) | 数据库服务器 | 5.7 | | | [Druid](https://github.com/alibaba/druid) | JDBC 连接池、监控组件 | 1.2.11 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) | | [MyBatis Plus](https://mp.baomidou.com/) | MyBatis 增强工具包 | 3.5.2 | [文档](http://www.iocoder.cn/Spring-Boot/MyBatis/?yudao) | diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java index d508c6ced..97fe67f61 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java @@ -1,5 +1,7 @@ package cn.iocoder.yudao.framework.common.util.date; +import cn.hutool.core.date.DateUtil; + import java.time.Duration; import java.util.Calendar; import java.util.Date; @@ -120,4 +122,17 @@ public class DateUtils { return c.getTime(); } + /** + * 是否今天 + * + * @param date 日期 + * @return 是否 + */ + public static boolean isToday(Date date) { + if (date == null) { + return false; + } + return DateUtil.isSameDay(date, new Date()); + } + } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImpl.java index 63aa4b8a3..1424cef91 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImpl.java +++ b/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; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.MapUtil; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; +import cn.iocoder.yudao.framework.common.util.date.DateUtils; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeCheckReqDTO; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; @@ -52,21 +53,22 @@ public class SmsCodeServiceImpl implements SmsCodeService { // 校验是否可以发送验证码,不用筛选场景 SmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, null,null); if (lastSmsCode != null) { - if (lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。 - throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY); - } if (System.currentTimeMillis() - lastSmsCode.getCreateTime().getTime() < smsCodeProperties.getSendFrequency().toMillis()) { // 发送过于频繁 throw ServiceExceptionUtil.exception(SMS_CODE_SEND_TOO_FAST); } + if (DateUtils.isToday(lastSmsCode.getCreateTime()) && // 必须是今天,才能计算超过当天的上限 + lastSmsCode.getTodayIndex() >= smsCodeProperties.getSendMaximumQuantityPerDay()) { // 超过当天发送的上限。 + throw ServiceExceptionUtil.exception(SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY); + } // TODO 芋艿:提升,每个 IP 每天可发送数量 // TODO 芋艿:提升,每个 IP 每小时可发送数量 } // 创建验证码记录 String code = String.valueOf(randomInt(smsCodeProperties.getBeginCode(), smsCodeProperties.getEndCode() + 1)); - SmsCodeDO newSmsCode = SmsCodeDO.builder().mobile(mobile).code(code) - .scene(scene).todayIndex(lastSmsCode != null ? lastSmsCode.getTodayIndex() + 1 : 1) + SmsCodeDO newSmsCode = SmsCodeDO.builder().mobile(mobile).code(code).scene(scene) + .todayIndex(lastSmsCode != null && DateUtils.isToday(lastSmsCode.getCreateTime()) ? lastSmsCode.getTodayIndex() + 1 : 1) .createIp(ip).used(false).build(); smsCodeMapper.insert(newSmsCode); return code;