fix: 修复用户导入功能

1. 修改角色/岗位处理逻辑
2. 添加必填字段校验
This commit is contained in:
huertian
2025-01-09 17:44:06 +08:00
parent 296477bdb0
commit 0fc9b4545c
4 changed files with 31 additions and 82 deletions

View File

@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -35,7 +36,9 @@ public class ImportController {
private DepartmentService departmentService;
@PostMapping("/users")
public ApiResponse<String> importUsers(@RequestParam("file") MultipartFile file) {
public ApiResponse<String> importUsers(
@RequestParam("file") MultipartFile file,
@RequestHeader("Authorization") String authHeader) {
if (file.isEmpty()) {
return new ApiResponse<>(ErrorCode.INVALID_PARAM, "文件不能为空", null);
}
@ -57,7 +60,8 @@ public class ImportController {
log.info("第{}行数据:{}", i + 1, userList.get(i));
}
return userService.batchImportUsers(userList);
String token = authHeader.substring(7);
return userService.batchImportUsers(userList, token);
} catch (Exception e) {
log.error("导入用户数据失败", e);
return new ApiResponse<>(ErrorCode.SYSTEM_ERROR, "导入用户数据失败:" + e.getMessage(), null);

View File

@ -66,6 +66,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
}
final String jwt = authHeader.substring(7);
logger.debug("Received JWT token: {}", jwt);
final String username = jwtService.extractUsername(jwt);
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {

View File

@ -193,7 +193,7 @@ public class UserService implements UserDetailsService {
return ApiResponse.success(user);
}
public ApiResponse<String> batchImportUsers(List<UserImportDTO> userList) {
public ApiResponse<String> batchImportUsers(List<UserImportDTO> userList, String token) {
if (userList == null || userList.isEmpty()) {
return new ApiResponse<>(ErrorCode.PARAM_ERROR, "导入数据不能为空", null);
}
@ -234,8 +234,29 @@ public class UserService implements UserDetailsService {
user.setEmail(dto.getEmail());
user.setPassword(passwordEncoder.encode(dto.getPassword()));
user.setDepartmentId(department.getId());
user.setRoles(0); // 默认角色
user.setJobs(0); // 默认工作
// 处理角色和岗位
String roleAndJob = dto.getRoleAndJob();
if (!StringUtils.isBlank(roleAndJob)) {
try {
user.setRoles(Integer.parseInt(roleAndJob));
user.setJobs(Integer.parseInt(roleAndJob));
} catch (NumberFormatException e) {
logger.warn("角色/岗位不是有效的数字: {}", roleAndJob);
errorMsg.append(String.format("第%d行角色/岗位不是有效的数字;", i + 2));
continue;
}
} else {
logger.warn("角色/岗位不能为空");
errorMsg.append(String.format("第%d行角色/岗位不能为空;", i + 2));
continue;
}
// 设置创建者ID
String email = jwtService.extractUsername(token);
Optional<User> creator = userRepository.findByEmail(email);
user.setCreatorId(creator.map(User::getId).orElse(0L));
user.setStatus(1); // 设置状态为正常
user.setCreatedAt(System.currentTimeMillis() / 1000);
user.setUpdatedAt(System.currentTimeMillis() / 1000);