| | |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @RestController |
| | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加") |
| | | public ResponseResult add(@RequestBody User user) { |
| | | // 1. 校验邮箱是否为空 |
| | | String email = user.getUser_email(); |
| | | if (!StringUtils.hasText(email)) { |
| | | return ResponseResult.error("邮箱不能为空"); |
| | | } |
| | | |
| | | // 2. 邮箱格式正则校验(基础版:包含@且@前后有内容) |
| | | String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,}$"; |
| | | boolean isEmailValid = Pattern.matches(emailRegex, email); |
| | | if (!isEmailValid) { |
| | | return ResponseResult.error("邮箱格式无效,请包含@符号且格式正确(例如:xxx@example.com)"); |
| | | } |
| | | List<User> list = userService.list(new LambdaQueryWrapper<User>().eq(User::getUser_email, user.getUser_email())); |
| | | if (list != null && list.size() > 0) { |
| | | return ResponseResult.error("该邮箱已存在"); |
| | |
| | | User user1 = userService.getById(user.getUser_id()); |
| | | if (user1 == null) { |
| | | return ResponseResult.error("该账户不存在"); |
| | | } |
| | | // 1. 校验邮箱是否为空 |
| | | String email = user.getUser_email(); |
| | | if (!StringUtils.hasText(email)) { |
| | | return ResponseResult.error("邮箱不能为空"); |
| | | } |
| | | // 2. 邮箱格式正则校验(基础版:包含@且@前后有内容) |
| | | String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,}$"; |
| | | boolean isEmailValid = Pattern.matches(emailRegex, email); |
| | | if (!isEmailValid) { |
| | | return ResponseResult.error("邮箱格式无效,请包含@符号且格式正确(例如:xxx@example.com)"); |
| | | } |
| | | List<User> list = userService.list(new LambdaQueryWrapper<User>() |
| | | .ne(User::getUser_id, user.getUser_id()) |
| | |
| | | if (excelDataList.isEmpty()) { |
| | | return ResponseResult.error("导入失败,数据不能为空"); |
| | | } |
| | | // 邮箱格式正则表达式 |
| | | String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*\\.[A-Za-z]{2,}$"; |
| | | Pattern emailPattern = Pattern.compile(emailRegex); |
| | | |
| | | for (ExcelDataWithRow<UserExcel> excelData : excelDataList) { |
| | | int rowNum = excelData.getRowNumber(); |
| | | UserExcel excelRowData = excelData.getData(); |
| | |
| | | if (!StringUtils.hasText(excelRowData.getUser_email())) { |
| | | rowErrors.add("邮箱不能为空"); |
| | | } else { |
| | | // 新增:邮箱格式校验 |
| | | if (!emailPattern.matcher(excelRowData.getUser_email()).matches()) { |
| | | rowErrors.add("邮箱格式不正确(需符合 example@domain.com 格式)"); |
| | | } |
| | | if (userEmailMap.containsKey(excelRowData.getUser_email())) { |
| | | rowErrors.add("邮箱已存在(数据库中重复)"); |
| | | } |