From 98fac3ddb8ecd5cc8912229b2894713ce57a1b70 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Wed, 27 Mar 2024 21:41:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E8=AE=BE=E7=BD=AE=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E8=AE=BE=E7=BD=AE=E8=BF=87=E6=9C=9F=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=B8=BA10=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bunny <1319900154@qq.com> --- .../java/com/atguigu/constant/MessageConstant.java | 2 ++ .../com/atguigu/user/service/impl/SmsServiceImpl.java | 11 +++++++---- .../atguigu/user/service/module/SmsServiceModule.java | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java b/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java index 647013f..ee5256c 100644 --- a/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java +++ b/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java @@ -23,4 +23,6 @@ public class MessageConstant { public static final String SAVE_DTO_IS_NULL = "添加参数不能为空"; public static final String UPDATE_DTO_IS_NULL = "修改参数不能为空"; public static final String FIND_ID_IS_NOT_EMPTY = "查询ID不能为空"; + public static final String MESSAGE_CODE_NOT_PASS = "短信验证码未过期"; + public static final String MESSAGE_CODE_UNAUTHORIZED = "短信验证码未授权,请联系管理员"; } diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/SmsServiceImpl.java b/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/SmsServiceImpl.java index aedaa76..b9aa7a3 100644 --- a/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/SmsServiceImpl.java +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/SmsServiceImpl.java @@ -1,5 +1,7 @@ package com.atguigu.user.service.impl; +import com.atguigu.constant.MessageConstant; +import com.atguigu.exception.BunnyException; import com.atguigu.user.service.SmsService; import com.atguigu.user.service.module.SmsServiceModule; import org.apache.commons.lang3.RandomStringUtils; @@ -22,13 +24,14 @@ public class SmsServiceImpl implements SmsService { @Override public void sendValidateCode(String phone) throws Exception { Object isExist = redisTemplate.opsForValue().get(phone); + if (isExist != null) { + throw new BunnyException(MessageConstant.MESSAGE_CODE_NOT_PASS); + } // 1. 生成验证码 String code = RandomStringUtils.randomNumeric(4); // 2. 生成的验证码放到Redis中,设置过期时间 - redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES); + redisTemplate.opsForValue().set(phone, code, 10, TimeUnit.MINUTES); // 3. 向手机发送短信 - if (isExist == null) { - SmsServiceModule.sendMessage(phone, code); - } + SmsServiceModule.sendMessage(phone, code); } } diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/service/module/SmsServiceModule.java b/spzx-service/service-user/src/main/java/com/atguigu/user/service/module/SmsServiceModule.java index b202b53..c3add4b 100644 --- a/spzx-service/service-user/src/main/java/com/atguigu/user/service/module/SmsServiceModule.java +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/service/module/SmsServiceModule.java @@ -1,5 +1,6 @@ package com.atguigu.user.service.module; +import com.atguigu.constant.MessageConstant; import com.atguigu.exception.BunnyException; import com.atguigu.utils.HttpUtils; import lombok.extern.slf4j.Slf4j; @@ -35,7 +36,7 @@ public class SmsServiceModule { log.info("请求完成响应:{}", response); if (response.getStatusLine().getStatusCode() > 300) { - throw new BunnyException("短信接码未授权"); + throw new BunnyException(MessageConstant.MESSAGE_CODE_UNAUTHORIZED); } } }