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); } } }