feat(新增): 短信验证码设置枚举类,设置过期时间为10分钟

Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
bunny 2024-03-27 21:41:16 +08:00
parent b41fbf27e1
commit 98fac3ddb8
3 changed files with 11 additions and 5 deletions

View File

@ -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 = "短信验证码未授权,请联系管理员";
}

View File

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

View File

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