注册集成手机号登录,修改密码,修改手机号
This commit is contained in:
@@ -21,10 +21,6 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String path = request.getRequestURI();
|
||||
|
||||
if (isWhitelistedPath(path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String authHeader = request.getHeader(AUTHORIZATION_HEADER);
|
||||
|
||||
if (authHeader == null || !authHeader.startsWith(BEARER_PREFIX)) {
|
||||
@@ -57,16 +53,4 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isWhitelistedPath(String path) {
|
||||
return path.contains("/user/login") ||
|
||||
path.contains("/user/register") ||
|
||||
path.contains("/user/refresh-token") ||
|
||||
path.contains("/user/get/account") ||
|
||||
path.contains("/shop/list/simple") ||
|
||||
path.contains("/admin/auth/login") ||
|
||||
path.contains("/admin/object-storage-file/upload") ||
|
||||
path.contains("/health") ||
|
||||
path.contains("/error");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
"/user/login",
|
||||
"/user/register",
|
||||
"/user/refresh-token",
|
||||
"/user/send-verification-code",
|
||||
"/user/phone-login",
|
||||
"/user/get/account/**",
|
||||
"/shop/list/simple",
|
||||
"/admin/auth/login",
|
||||
|
||||
@@ -4,13 +4,18 @@ import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.BindEmailRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.BindPhoneRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.LoginRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.PhoneLoginRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.RefreshTokenRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.RegisterRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.SendVerificationCodeRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.UpdatePhoneRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.UserDTO;
|
||||
import com.xiaowang.yuminailcore.vo.LoginResponseVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserVO;
|
||||
import com.xiaowang.yuminailcore.service.MinioService;
|
||||
import com.xiaowang.yuminailcore.service.UserService;
|
||||
import com.xiaowang.yuminailcore.service.VerificationCodeService;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -33,6 +38,7 @@ public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final MinioService minioService;
|
||||
private final VerificationCodeService verificationCodeService;
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
@@ -142,6 +148,18 @@ public class UserController {
|
||||
return Result.success(loginResponseVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号验证码登录
|
||||
*
|
||||
* @param phoneLoginRequestDTO 手机号验证码登录请求DTO
|
||||
* @return 登录响应VO
|
||||
*/
|
||||
@PostMapping("/phone-login")
|
||||
public Result<LoginResponseVO> phoneLogin(@RequestBody PhoneLoginRequestDTO phoneLoginRequestDTO) {
|
||||
LoginResponseVO loginResponseVO = userService.phoneLogin(phoneLoginRequestDTO);
|
||||
return Result.success(loginResponseVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
@@ -154,6 +172,21 @@ public class UserController {
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @param sendVerificationCodeRequestDTO 发送验证码请求DTO
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
@PostMapping("/send-verification-code")
|
||||
public Result<Boolean> sendVerificationCode(@RequestBody SendVerificationCodeRequestDTO sendVerificationCodeRequestDTO) {
|
||||
if (!verificationCodeService.canResend(sendVerificationCodeRequestDTO.getPhone())) {
|
||||
throw new RuntimeException("验证码发送过于频繁,请1分钟后再试");
|
||||
}
|
||||
verificationCodeService.sendVerificationCode(sendVerificationCodeRequestDTO.getPhone());
|
||||
return Result.success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新Token
|
||||
*
|
||||
@@ -264,4 +297,37 @@ public class UserController {
|
||||
boolean success = userService.resetPassword(userId, currentUserId);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户密码
|
||||
*
|
||||
* @param request 请求对象
|
||||
* @param passwordDTO 密码DTO
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
@PutMapping("/password")
|
||||
public Result<Boolean> updatePassword(HttpServletRequest request, @RequestBody PasswordDTO passwordDTO) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
boolean success = userService.updatePassword(userId, passwordDTO.getPassword());
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改手机号
|
||||
*
|
||||
* @param request 请求对象
|
||||
* @param updatePhoneDTO 修改手机号DTO
|
||||
* @return 是否修改成功
|
||||
*/
|
||||
@PutMapping("/phone")
|
||||
public Result<Boolean> updatePhone(HttpServletRequest request, @RequestBody UpdatePhoneRequestDTO updatePhoneDTO) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
boolean success = userService.updatePhone(userId, updatePhoneDTO);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class PasswordDTO {
|
||||
private String password;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
/**
|
||||
* 手机号验证码登录请求 DTO
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-03-03
|
||||
*/
|
||||
@Data
|
||||
public class PhoneLoginRequestDTO {
|
||||
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
|
||||
private String phone;
|
||||
|
||||
@NotBlank(message = "验证码不能为空")
|
||||
@Pattern(regexp = "^\\d{6}$", message = "验证码必须是6位数字")
|
||||
private String verificationCode;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdatePhoneRequestDTO {
|
||||
private String oldPhone;
|
||||
private String newPhone;
|
||||
private String verificationCode;
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.xiaowang.yuminailcore.service;
|
||||
|
||||
/**
|
||||
* MessageHub客户端服务接口
|
||||
* 用于调用MessageHub服务发送消息
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-03-03
|
||||
*/
|
||||
public interface MessageHubClientService {
|
||||
|
||||
void sendSmsVerificationCode(String phone, String code);
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.xiaowang.yuminailcore.dto.BindEmailRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.BindPhoneRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.LoginRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.PhoneLoginRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.RegisterRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.UpdatePhoneRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.UserDTO;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.vo.LoginResponseVO;
|
||||
@@ -33,6 +35,8 @@ public interface UserService extends IService<UserDO> {
|
||||
|
||||
LoginResponseVO login(LoginRequestDTO loginRequestDTO);
|
||||
|
||||
LoginResponseVO phoneLogin(PhoneLoginRequestDTO phoneLoginRequestDTO);
|
||||
|
||||
LoginResponseVO adminLogin(LoginRequestDTO loginRequestDTO);
|
||||
|
||||
boolean register(RegisterRequestDTO registerRequestDTO);
|
||||
@@ -54,4 +58,8 @@ public interface UserService extends IService<UserDO> {
|
||||
boolean updateUserStatus(Long id, Integer status);
|
||||
|
||||
boolean updateUserAvatar(Long userId, String avatarUrl);
|
||||
|
||||
boolean updatePassword(Long userId, String password);
|
||||
|
||||
boolean updatePhone(Long userId, UpdatePhoneRequestDTO updatePhoneDTO);
|
||||
}
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.xiaowang.yuminailcore.service.impl;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.xiaowang.yuminailcore.service.MessageHubClientService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.MessageBuilder;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* MessageHub客户端服务实现
|
||||
* 通过RabbitMQ发送消息到MessageHub服务
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-03-03
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MessageHubClientServiceImpl implements MessageHubClientService {
|
||||
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private static final String SMS_QUEUE = "smsQueue";
|
||||
|
||||
@Override
|
||||
public void sendSmsVerificationCode(String phone, String code) {
|
||||
try {
|
||||
Map<String, Object> messageBody = new HashMap<>();
|
||||
messageBody.put("receiver", phone);
|
||||
messageBody.put("channelType", "SMS");
|
||||
messageBody.put("msgType", "VERIFICATION");
|
||||
messageBody.put("templateCode", "SPUG0001");
|
||||
messageBody.put("sourceSystem", "yuminail");
|
||||
messageBody.put("uniqueId", UUID.randomUUID().toString());
|
||||
messageBody.put("sendMethod", "SPUG");
|
||||
|
||||
Map<String, Object> templateParams = new HashMap<>();
|
||||
templateParams.put("name", "屿Nail");
|
||||
templateParams.put("code", code);
|
||||
templateParams.put("number", "5");
|
||||
messageBody.put("templateParams", templateParams);
|
||||
|
||||
String jsonBody = objectMapper.writeValueAsString(messageBody);
|
||||
|
||||
Message message = MessageBuilder
|
||||
.withBody(jsonBody.getBytes())
|
||||
.setMessageId(UUID.randomUUID().toString())
|
||||
.build();
|
||||
|
||||
rabbitTemplate.convertAndSend(SMS_QUEUE, message);
|
||||
|
||||
log.info("短信验证码消息已发送到队列,手机号: {}, 验证码: {}", phone, code);
|
||||
} catch (Exception e) {
|
||||
log.error("发送短信验证码失败", e);
|
||||
throw new RuntimeException("发送短信验证码失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+130
-5
@@ -4,11 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.xiaowang.yuminailcore.dto.BindEmailRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.BindPhoneRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.LoginRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.RegisterRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.UserDTO;
|
||||
import com.xiaowang.yuminailcore.dto.*;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.entity.UserPermissionDO;
|
||||
import com.xiaowang.yuminailcore.entity.PermissionDO;
|
||||
@@ -17,12 +13,14 @@ import com.xiaowang.yuminailcore.mapper.UserPermissionMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.PermissionMapper;
|
||||
import com.xiaowang.yuminailcore.service.ShopService;
|
||||
import com.xiaowang.yuminailcore.service.UserService;
|
||||
import com.xiaowang.yuminailcore.service.VerificationCodeService;
|
||||
import com.xiaowang.yuminailcore.utils.JwtUtil;
|
||||
import com.xiaowang.yuminailcore.vo.LoginResponseVO;
|
||||
import com.xiaowang.yuminailcore.vo.PageResultVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserVO;
|
||||
import com.xiaowang.yuminailcore.vo.ShopVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -50,6 +48,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
private final UserPermissionMapper userPermissionMapper;
|
||||
private final PermissionMapper permissionMapper;
|
||||
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
private final VerificationCodeService verificationCodeService;
|
||||
private final RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
/**
|
||||
* 创建新用户
|
||||
@@ -370,6 +370,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
|
||||
// 检查是否为未设置密码的账号
|
||||
if (ENCODED_UNSET_PASSWORD.equals(userDO.getPassword())) {
|
||||
throw new RuntimeException("请使用手机号验证码登录,并在个人中心设置密码");
|
||||
}
|
||||
|
||||
if (!passwordEncoder.matches(loginRequestDTO.getPassword(), userDO.getPassword())) {
|
||||
throw new RuntimeException("密码错误");
|
||||
}
|
||||
@@ -393,6 +398,68 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
return responseVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginResponseVO phoneLogin(PhoneLoginRequestDTO phoneLoginRequestDTO) {
|
||||
if (!verificationCodeService.verifyCode(phoneLoginRequestDTO.getPhone(), phoneLoginRequestDTO.getVerificationCode())) {
|
||||
throw new RuntimeException("验证码错误或已过期");
|
||||
}
|
||||
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getPhone, phoneLoginRequestDTO.getPhone())
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
);
|
||||
|
||||
if (userDO == null) {
|
||||
userDO = registerByPhone(phoneLoginRequestDTO.getPhone());
|
||||
}
|
||||
|
||||
String accessToken = jwtUtil.generateAccessToken(userDO.getId(), userDO.getAccount());
|
||||
String refreshToken = jwtUtil.generateRefreshToken(userDO.getId(), userDO.getAccount());
|
||||
|
||||
LoginResponseVO responseVO = new LoginResponseVO();
|
||||
responseVO.setAccessToken(accessToken);
|
||||
responseVO.setRefreshToken(refreshToken);
|
||||
|
||||
LoginResponseVO.UserInfo userInfo = new LoginResponseVO.UserInfo();
|
||||
userInfo.setId(userDO.getId());
|
||||
userInfo.setAccount(userDO.getAccount());
|
||||
userInfo.setNickname(userDO.getNickname());
|
||||
userInfo.setType(userDO.getType());
|
||||
userInfo.setShopId(userDO.getShopId());
|
||||
userInfo.setCreateTime(userDO.getCreateTime());
|
||||
responseVO.setUserInfo(userInfo);
|
||||
|
||||
// 设置是否需要设置密码
|
||||
responseVO.setNeedSetPassword(ENCODED_UNSET_PASSWORD.equals(userDO.getPassword()));
|
||||
|
||||
return responseVO;
|
||||
}
|
||||
|
||||
// 未设置密码的特殊标识
|
||||
private static final String UNSET_PASSWORD_MARKER = "__UNSET_PASSWORD__";
|
||||
private static final String ENCODED_UNSET_PASSWORD = "$2a$10$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW"; // 加密后的特殊标识
|
||||
|
||||
private UserDO registerByPhone(String phone) {
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setAccount(phone);
|
||||
userDO.setPhone(phone);
|
||||
userDO.setPhoneVerified(1);
|
||||
userDO.setNickname("用户" + phone.substring(7));
|
||||
userDO.setType(0);
|
||||
userDO.setLevel("普通");
|
||||
userDO.setShopId(0L);
|
||||
userDO.setIsDelete(0);
|
||||
userDO.setStatus(1);
|
||||
userDO.setCreateTime(LocalDateTime.now());
|
||||
userDO.setUpdateTime(LocalDateTime.now());
|
||||
// 设置未设置密码的特殊标识
|
||||
userDO.setPassword(ENCODED_UNSET_PASSWORD);
|
||||
|
||||
userMapper.insert(userDO);
|
||||
return userDO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginResponseVO adminLogin(LoginRequestDTO loginRequestDTO) {
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
@@ -434,6 +501,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
@Override
|
||||
public boolean register(RegisterRequestDTO registerRequestDTO) {
|
||||
if (!verificationCodeService.verifyCode(registerRequestDTO.getPhone(), registerRequestDTO.getVerificationCode())) {
|
||||
throw new RuntimeException("验证码错误或已过期");
|
||||
}
|
||||
|
||||
UserDO existingUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, registerRequestDTO.getAccount())
|
||||
@@ -443,11 +514,23 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
throw new RuntimeException("账号已存在");
|
||||
}
|
||||
|
||||
UserDO existingPhoneUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getPhone, registerRequestDTO.getPhone())
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
);
|
||||
|
||||
if (existingPhoneUser != null) {
|
||||
throw new RuntimeException("该手机号已被注册");
|
||||
}
|
||||
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setShopId(registerRequestDTO.getShopId());
|
||||
userDO.setAccount(registerRequestDTO.getAccount());
|
||||
userDO.setPassword(passwordEncoder.encode(registerRequestDTO.getPassword()));
|
||||
userDO.setNickname(registerRequestDTO.getNickname());
|
||||
userDO.setPhone(registerRequestDTO.getPhone());
|
||||
userDO.setPhoneVerified(1);
|
||||
userDO.setType(Integer.valueOf(0));
|
||||
userDO.setLevel("普通");
|
||||
userDO.setIsDelete(Integer.valueOf(0));
|
||||
@@ -614,4 +697,46 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setUpdateTime(LocalDateTime.now());
|
||||
return userMapper.updateById(userDO) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(Long userId, String password) {
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
if (userDO == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
userDO.setPassword(passwordEncoder.encode(password));
|
||||
userDO.setUpdateTime(LocalDateTime.now());
|
||||
return userMapper.updateById(userDO) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePhone(Long userId, UpdatePhoneRequestDTO updatePhoneDTO) {
|
||||
String oldPhone = updatePhoneDTO.getOldPhone();
|
||||
String newPhone = updatePhoneDTO.getNewPhone();
|
||||
String verificationCode = updatePhoneDTO.getVerificationCode();
|
||||
|
||||
// 验证验证码
|
||||
String redisKey = "verification_code:" + newPhone;
|
||||
String storedCode = redisTemplate.opsForValue().get(redisKey);
|
||||
if (storedCode == null || !storedCode.equals(verificationCode)) {
|
||||
throw new RuntimeException("验证码错误");
|
||||
}
|
||||
|
||||
// 检查新手机号是否已被其他用户使用
|
||||
LambdaQueryWrapper<UserDO> wrapper = Wrappers.lambdaQuery(UserDO.class);
|
||||
wrapper.eq(UserDO::getPhone, newPhone);
|
||||
wrapper.ne(UserDO::getId, userId);
|
||||
if (userMapper.selectOne(wrapper) != null) {
|
||||
throw new RuntimeException("手机号已被使用");
|
||||
}
|
||||
|
||||
// 更新手机号
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
if (userDO == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
userDO.setPhone(newPhone);
|
||||
userDO.setUpdateTime(LocalDateTime.now());
|
||||
return userMapper.updateById(userDO) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.xiaowang.yuminailcore.service.impl;
|
||||
|
||||
import com.xiaowang.yuminailcore.service.MessageHubClientService;
|
||||
import com.xiaowang.yuminailcore.service.VerificationCodeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 验证码服务实现
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-03-03
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class VerificationCodeServiceImpl implements VerificationCodeService {
|
||||
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
private final MessageHubClientService messageHubClientService;
|
||||
|
||||
private static final String CODE_PREFIX = "verification:code:";
|
||||
private static final String SEND_TIME_PREFIX = "verification:send_time:";
|
||||
private static final int CODE_LENGTH = 6;
|
||||
private static final int CODE_EXPIRE_MINUTES = 5;
|
||||
private static final int RESEND_INTERVAL_SECONDS = 60;
|
||||
|
||||
@Override
|
||||
public String generateVerificationCode() {
|
||||
SecureRandom random = new SecureRandom();
|
||||
StringBuilder code = new StringBuilder();
|
||||
for (int i = 0; i < CODE_LENGTH; i++) {
|
||||
code.append(random.nextInt(10));
|
||||
}
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendVerificationCode(String phone) {
|
||||
String code = generateVerificationCode();
|
||||
|
||||
String codeKey = CODE_PREFIX + phone;
|
||||
String sendTimeKey = SEND_TIME_PREFIX + phone;
|
||||
|
||||
redisTemplate.opsForValue().set(codeKey, code, Duration.ofMinutes(CODE_EXPIRE_MINUTES));
|
||||
redisTemplate.opsForValue().set(sendTimeKey, String.valueOf(System.currentTimeMillis()),
|
||||
Duration.ofSeconds(RESEND_INTERVAL_SECONDS));
|
||||
|
||||
messageHubClientService.sendSmsVerificationCode(phone, code);
|
||||
|
||||
log.info("验证码已发送,手机号: {}, 验证码: {}", phone, code);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verifyCode(String phone, String code) {
|
||||
String codeKey = CODE_PREFIX + phone;
|
||||
String storedCode = redisTemplate.opsForValue().get(codeKey);
|
||||
|
||||
if (storedCode == null) {
|
||||
log.warn("验证码不存在或已过期,手机号: {}", phone);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!storedCode.equals(code)) {
|
||||
log.warn("验证码错误,手机号: {}, 输入: {}, 正确: {}", phone, code, storedCode);
|
||||
return false;
|
||||
}
|
||||
|
||||
redisTemplate.delete(codeKey);
|
||||
log.info("验证码验证成功,手机号: {}", phone);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canResend(String phone) {
|
||||
String sendTimeKey = SEND_TIME_PREFIX + phone;
|
||||
String sendTime = redisTemplate.opsForValue().get(sendTimeKey);
|
||||
return sendTime == null;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ public class LoginResponseVO {
|
||||
|
||||
private UserInfo userInfo;
|
||||
|
||||
private boolean needSetPassword; // 是否需要设置密码
|
||||
|
||||
@Data
|
||||
public static class UserInfo {
|
||||
private Long id;
|
||||
|
||||
@@ -40,7 +40,7 @@ spring:
|
||||
password: 122503wcy....
|
||||
# 共享配置文件列表
|
||||
shared-configs:
|
||||
- data-id: rabbitmq.yaml
|
||||
- data-id: rabbit-mq.yaml
|
||||
group: DEFAULT_GROUP
|
||||
refresh: true
|
||||
- data-id: datasource.yaml
|
||||
@@ -52,3 +52,6 @@ spring:
|
||||
- data-id: logging.yaml
|
||||
group: DEFAULT_GROUP
|
||||
refresh: true
|
||||
- data-id: redis.yaml
|
||||
group: DEFAULT_GROUP
|
||||
refresh: true
|
||||
|
||||
Reference in New Issue
Block a user