功能:统一账号输入校验并调整首页轮播图
This commit is contained in:
+32
@@ -1,9 +1,14 @@
|
||||
package com.xiaowang.yuminailcore.common;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
*
|
||||
@@ -14,6 +19,33 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
String message = e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.findFirst()
|
||||
.orElse("请求参数不合法");
|
||||
return Result.error(message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BindException.class)
|
||||
public Result<Void> handleBindException(BindException e) {
|
||||
String message = e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.findFirst()
|
||||
.orElse("请求参数不合法");
|
||||
return Result.error(message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public Result<Void> handleConstraintViolationException(ConstraintViolationException e) {
|
||||
String message = e.getConstraintViolations().stream()
|
||||
.map(violation -> violation.getMessage())
|
||||
.findFirst()
|
||||
.orElse("请求参数不合法");
|
||||
return Result.error(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运行时异常
|
||||
*/
|
||||
|
||||
+4
-2
@@ -5,11 +5,13 @@ import com.xiaowang.yuminailcore.dto.LoginRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.RefreshTokenRequestDTO;
|
||||
import com.xiaowang.yuminailcore.dto.SwitchShopRequestDTO;
|
||||
import com.xiaowang.yuminailcore.service.UserService;
|
||||
import com.xiaowang.yuminailcore.utils.AccountValidationUtils;
|
||||
import com.xiaowang.yuminailcore.vo.LoginShopSelectionVO;
|
||||
import com.xiaowang.yuminailcore.vo.LoginResponseVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@RestController
|
||||
@@ -20,14 +22,14 @@ public class AdminAuthController {
|
||||
private final UserService userService;
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result<LoginResponseVO> login(@RequestBody LoginRequestDTO loginRequestDTO) {
|
||||
public Result<LoginResponseVO> login(@Valid @RequestBody LoginRequestDTO loginRequestDTO) {
|
||||
LoginResponseVO loginResponseVO = userService.adminLogin(loginRequestDTO);
|
||||
return Result.success(loginResponseVO);
|
||||
}
|
||||
|
||||
@GetMapping("/login-shops/{account}")
|
||||
public Result<LoginShopSelectionVO> getLoginShops(@PathVariable String account) {
|
||||
return Result.success(userService.getAdminLoginShopSelection(account));
|
||||
return Result.success(userService.getAdminLoginShopSelection(AccountValidationUtils.requireValidAccount(account)));
|
||||
}
|
||||
|
||||
@PostMapping("/switch-shop")
|
||||
|
||||
+3
-2
@@ -17,6 +17,7 @@ import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -48,7 +49,7 @@ public class AdminUserController {
|
||||
|
||||
@PostMapping("/create")
|
||||
@RequirePermission("user:create")
|
||||
public Result<Long> createUser(HttpServletRequest request, @RequestBody UserDTO userDTO) {
|
||||
public Result<Long> createUser(HttpServletRequest request, @Valid @RequestBody UserDTO userDTO) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
adminScopeHelper.ensureAssignableUserType(currentUser, userDTO.getType());
|
||||
if (!adminScopeHelper.isPlatformAdmin(currentUser)) {
|
||||
@@ -62,7 +63,7 @@ public class AdminUserController {
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission("user:edit")
|
||||
public Result<Boolean> updateUser(HttpServletRequest request, @PathVariable Long id, @RequestBody UserDTO userDTO) {
|
||||
public Result<Boolean> updateUser(HttpServletRequest request, @PathVariable Long id, @Valid @RequestBody UserDTO userDTO) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = userService.getUserById(id);
|
||||
if (targetUser == null) {
|
||||
|
||||
+13
-24
@@ -12,11 +12,13 @@ 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 com.xiaowang.yuminailcore.utils.AccountValidationUtils;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.validation.Valid;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -48,7 +50,7 @@ public class UserController {
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
@RequirePermission("user:create")
|
||||
public Result<Long> createUser(HttpServletRequest request, @RequestBody UserDTO userDTO) {
|
||||
public Result<Long> createUser(HttpServletRequest request, @Valid @RequestBody UserDTO userDTO) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
adminScopeHelper.ensureAssignableUserType(currentUser, userDTO.getType());
|
||||
if (!adminScopeHelper.isPlatformAdmin(currentUser)) {
|
||||
@@ -69,7 +71,7 @@ public class UserController {
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission("user:edit")
|
||||
public Result<Boolean> updateUser(HttpServletRequest request, @PathVariable Long id, @RequestBody UserDTO userDTO) {
|
||||
public Result<Boolean> updateUser(HttpServletRequest request, @PathVariable Long id, @Valid @RequestBody UserDTO userDTO) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = requireAccessibleUser(currentUser, id, "鏃犳潈缂栬緫鍏朵粬搴楅摵鐢ㄦ埛");
|
||||
if (userDTO.getType() != null) {
|
||||
@@ -172,7 +174,7 @@ public class UserController {
|
||||
@RequirePermission("user:view")
|
||||
public Result<UserVO> getUserByAccount(HttpServletRequest request, @PathVariable String account) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO userVO = userService.getUserByAccount(account);
|
||||
UserVO userVO = userService.getUserByAccount(AccountValidationUtils.requireValidAccount(account));
|
||||
if (userVO != null) {
|
||||
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, userVO.getId(), "鏃犳潈鏌ョ湅鍏朵粬搴楅摵鐢ㄦ埛");
|
||||
}
|
||||
@@ -181,9 +183,10 @@ public class UserController {
|
||||
|
||||
@GetMapping("/check/account/{account}")
|
||||
public Result<AvailabilityVO> checkAccountAvailability(@PathVariable String account) {
|
||||
String normalizedAccount = AccountValidationUtils.requireValidAccount(account);
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, account)
|
||||
.eq(UserDO::getAccount, normalizedAccount)
|
||||
);
|
||||
boolean available = userDO == null || Integer.valueOf(1).equals(userDO.getIsDelete());
|
||||
String message = available ? "账号可用" : "账号已被注册";
|
||||
@@ -192,9 +195,10 @@ public class UserController {
|
||||
|
||||
@GetMapping("/check/login-account/{account}")
|
||||
public Result<LoginAccountAvailabilityVO> checkLoginAccountAvailability(@PathVariable String account) {
|
||||
String normalizedAccount = AccountValidationUtils.requireValidAccount(account);
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, account)
|
||||
.eq(UserDO::getAccount, normalizedAccount)
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
.last("limit 1")
|
||||
);
|
||||
@@ -208,7 +212,7 @@ public class UserController {
|
||||
|
||||
@GetMapping("/check/login-shops/account/{account}")
|
||||
public Result<LoginShopSelectionVO> getLoginShopsByAccount(@PathVariable String account) {
|
||||
return Result.success(userService.getLoginShopSelectionByAccount(account));
|
||||
return Result.success(userService.getLoginShopSelectionByAccount(AccountValidationUtils.requireValidAccount(account)));
|
||||
}
|
||||
|
||||
@GetMapping("/check/login-shops/phone/{phone}")
|
||||
@@ -235,7 +239,7 @@ public class UserController {
|
||||
* @return 登录响应VO
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result<LoginResponseVO> login(@RequestBody LoginRequestDTO loginRequestDTO) {
|
||||
public Result<LoginResponseVO> login(@Valid @RequestBody LoginRequestDTO loginRequestDTO) {
|
||||
LoginResponseVO loginResponseVO = userService.login(loginRequestDTO);
|
||||
return Result.success(loginResponseVO);
|
||||
}
|
||||
@@ -265,7 +269,7 @@ public class UserController {
|
||||
* @return 是否注册成功
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public Result<Boolean> register(@RequestBody RegisterRequestDTO registerRequestDTO) {
|
||||
public Result<Boolean> register(@Valid @RequestBody RegisterRequestDTO registerRequestDTO) {
|
||||
boolean success = userService.register(registerRequestDTO);
|
||||
return Result.success(success);
|
||||
}
|
||||
@@ -339,7 +343,7 @@ public class UserController {
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
@PutMapping({"/update", "/info"})
|
||||
public Result<Boolean> updateCurrentUserInfo(HttpServletRequest request, @RequestBody UserDTO userDTO) {
|
||||
public Result<Boolean> updateCurrentUserInfo(HttpServletRequest request, @Valid @RequestBody UserDTO userDTO) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
boolean success = userService.updateCurrentUserInfo(userId, userDTO);
|
||||
return Result.success(success);
|
||||
@@ -460,21 +464,6 @@ public class UserController {
|
||||
return Result.error("管理员账号无法注销");
|
||||
}
|
||||
|
||||
// 在删除前先重命名账号和手机号,添加删除时间戳
|
||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||
String newAccount = userDO.getAccount() + "_deleted_" + timestamp;
|
||||
String newPhone = userDO.getPhone() + "_deleted_" + timestamp;
|
||||
|
||||
try {
|
||||
UserDTO updateDTO = new UserDTO();
|
||||
updateDTO.setAccount(newAccount);
|
||||
updateDTO.setPhone(newPhone);
|
||||
userService.updateUser(userId, updateDTO);
|
||||
} catch (Exception e) {
|
||||
System.out.println("重命名账号和手机号失败: " + e.getMessage());
|
||||
// 重命名失败也继续删除流程
|
||||
}
|
||||
|
||||
boolean success = userService.deleteUser(userId);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import com.xiaowang.yuminailcore.utils.AccountValidationUtils;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
/**
|
||||
* 登录请求 DTO
|
||||
* 用于用户账号密码登录
|
||||
@@ -17,6 +21,8 @@ public class LoginRequestDTO {
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@NotBlank(message = "账号不能为空")
|
||||
@Pattern(regexp = AccountValidationUtils.ACCOUNT_REGEX, message = "账号只能包含字母和数字")
|
||||
private String account;
|
||||
|
||||
/**
|
||||
@@ -33,4 +39,4 @@ public class LoginRequestDTO {
|
||||
* 店铺ID
|
||||
*/
|
||||
private Long shopId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import com.xiaowang.yuminailcore.utils.AccountValidationUtils;
|
||||
import lombok.Data;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
@@ -13,6 +14,8 @@ import javax.validation.constraints.Pattern;
|
||||
@Data
|
||||
public class RegisterRequestDTO {
|
||||
|
||||
@NotBlank(message = "账号不能为空")
|
||||
@Pattern(regexp = AccountValidationUtils.ACCOUNT_REGEX, message = "账号只能包含字母和数字")
|
||||
private String account;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@@ -29,4 +32,4 @@ public class RegisterRequestDTO {
|
||||
@NotBlank(message = "验证码不能为空")
|
||||
@Pattern(regexp = "^\\d{6}$", message = "验证码必须是6位数字")
|
||||
private String verificationCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import com.xiaowang.yuminailcore.utils.AccountValidationUtils;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Pattern;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -32,6 +34,7 @@ public class UserDTO {
|
||||
/**
|
||||
* 用户账号(登录用)
|
||||
*/
|
||||
@Pattern(regexp = AccountValidationUtils.ACCOUNT_REGEX, message = "账号只能包含字母和数字")
|
||||
private String account;
|
||||
|
||||
/**
|
||||
|
||||
+19
-10
@@ -19,6 +19,7 @@ import com.xiaowang.yuminailcore.service.VerificationCodeService;
|
||||
import com.xiaowang.yuminailcore.support.PermissionPolicy;
|
||||
import com.xiaowang.yuminailcore.support.RequestShopContext;
|
||||
import com.xiaowang.yuminailcore.support.UserShopBindingSupport;
|
||||
import com.xiaowang.yuminailcore.utils.AccountValidationUtils;
|
||||
import com.xiaowang.yuminailcore.utils.JwtUtil;
|
||||
import com.xiaowang.yuminailcore.vo.LoginShopSelectionVO;
|
||||
import com.xiaowang.yuminailcore.vo.LoginResponseVO;
|
||||
@@ -222,10 +223,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long createUser(UserDTO userDTO) {
|
||||
String validatedAccount = AccountValidationUtils.requireValidAccount(userDTO.getAccount());
|
||||
// 检查账号是否已存在
|
||||
UserDO existingUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, userDTO.getAccount())
|
||||
.eq(UserDO::getAccount, validatedAccount)
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
);
|
||||
if (existingUser != null) {
|
||||
@@ -243,7 +245,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setShopId(validatedShopId);
|
||||
userDO.setNickname(userDTO.getNickname());
|
||||
userDO.setAccount(userDTO.getAccount());
|
||||
userDO.setAccount(validatedAccount);
|
||||
userDO.setPassword(passwordEncoder.encode(userDTO.getPassword()));
|
||||
userDO.setPhone(validatedPhone);
|
||||
userDO.setSignature(userDTO.getSignature());
|
||||
@@ -326,16 +328,17 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
}
|
||||
|
||||
// 更新账号(如果提供且变更)
|
||||
if (userDTO.getAccount() != null && !userDTO.getAccount().equals(userDO.getAccount())) {
|
||||
String normalizedAccount = AccountValidationUtils.validateOptionalAccount(userDTO.getAccount());
|
||||
if (normalizedAccount != null && !normalizedAccount.equals(userDO.getAccount())) {
|
||||
UserDO existingUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, userDTO.getAccount())
|
||||
.eq(UserDO::getAccount, normalizedAccount)
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
);
|
||||
if (existingUser != null) {
|
||||
throw new RuntimeException("账号已存在");
|
||||
}
|
||||
userDO.setAccount(userDTO.getAccount());
|
||||
userDO.setAccount(normalizedAccount);
|
||||
}
|
||||
|
||||
// 更新其他字段(如果提供)
|
||||
@@ -483,9 +486,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
*/
|
||||
@Override
|
||||
public UserVO getUserByAccount(String account) {
|
||||
String normalizedAccount = AccountValidationUtils.requireValidAccount(account);
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, account)
|
||||
.eq(UserDO::getAccount, normalizedAccount)
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
);
|
||||
if (userDO == null || Integer.valueOf(1).equals(userDO.getIsDelete())) {
|
||||
@@ -546,6 +550,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
if (!StringUtils.hasText(normalizedAccount)) {
|
||||
return buildLoginShopSelection(List.of(), false, "请输入账号后再选择店铺");
|
||||
}
|
||||
AccountValidationUtils.ensureValidAccountFormat(normalizedAccount);
|
||||
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
@@ -622,6 +627,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
if (!StringUtils.hasText(normalizedAccount)) {
|
||||
return buildLoginShopSelection(List.of(), false, "请输入账号后再选择店铺");
|
||||
}
|
||||
AccountValidationUtils.ensureValidAccountFormat(normalizedAccount);
|
||||
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
@@ -657,9 +663,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
@Override
|
||||
public LoginResponseVO login(LoginRequestDTO loginRequestDTO) {
|
||||
String normalizedAccount = AccountValidationUtils.requireValidAccount(loginRequestDTO.getAccount());
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, loginRequestDTO.getAccount())
|
||||
.eq(UserDO::getAccount, normalizedAccount)
|
||||
);
|
||||
|
||||
assertUserAvailable(userDO);
|
||||
@@ -735,9 +742,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public LoginResponseVO adminLogin(LoginRequestDTO loginRequestDTO) {
|
||||
String normalizedAccount = AccountValidationUtils.requireValidAccount(loginRequestDTO.getAccount());
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, loginRequestDTO.getAccount())
|
||||
.eq(UserDO::getAccount, normalizedAccount)
|
||||
);
|
||||
|
||||
assertUserAvailable(userDO);
|
||||
@@ -823,13 +831,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
@Override
|
||||
public boolean register(RegisterRequestDTO registerRequestDTO) {
|
||||
String validatedAccount = AccountValidationUtils.requireValidAccount(registerRequestDTO.getAccount());
|
||||
if (!verificationCodeService.verifyCode(registerRequestDTO.getPhone(), registerRequestDTO.getVerificationCode())) {
|
||||
throw new RuntimeException("验证码错误或已过期");
|
||||
}
|
||||
|
||||
UserDO existingUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, registerRequestDTO.getAccount())
|
||||
.eq(UserDO::getAccount, validatedAccount)
|
||||
);
|
||||
|
||||
if (existingUser != null) {
|
||||
@@ -857,7 +866,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setShopId(validatedShopId);
|
||||
userDO.setAccount(registerRequestDTO.getAccount());
|
||||
userDO.setAccount(validatedAccount);
|
||||
userDO.setPassword(passwordEncoder.encode(registerRequestDTO.getPassword()));
|
||||
userDO.setNickname(registerRequestDTO.getNickname());
|
||||
userDO.setPhone(registerRequestDTO.getPhone());
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.xiaowang.yuminailcore.utils;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class AccountValidationUtils {
|
||||
|
||||
public static final String ACCOUNT_REGEX = "^[A-Za-z0-9]+$";
|
||||
private static final Pattern ACCOUNT_PATTERN = Pattern.compile(ACCOUNT_REGEX);
|
||||
|
||||
private AccountValidationUtils() {
|
||||
}
|
||||
|
||||
public static String normalizeAccount(String account) {
|
||||
return account == null ? "" : account.trim();
|
||||
}
|
||||
|
||||
public static void ensureValidAccountFormat(String account) {
|
||||
if (!StringUtils.hasText(account)) {
|
||||
throw new RuntimeException("账号不能为空");
|
||||
}
|
||||
if (!ACCOUNT_PATTERN.matcher(account).matches()) {
|
||||
throw new RuntimeException("账号只能包含字母和数字");
|
||||
}
|
||||
}
|
||||
|
||||
public static String requireValidAccount(String account) {
|
||||
String normalized = normalizeAccount(account);
|
||||
ensureValidAccountFormat(normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
public static String validateOptionalAccount(String account) {
|
||||
if (account == null) {
|
||||
return null;
|
||||
}
|
||||
String normalized = normalizeAccount(account);
|
||||
ensureValidAccountFormat(normalized);
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user