feat(admin-backend): add user avatar upload endpoint for admins
This commit is contained in:
@@ -207,6 +207,27 @@ public class UserController {
|
|||||||
return Result.success("更新成功", userDTO);
|
return Result.success("更新成功", userDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员上传指定用户头像
|
||||||
|
*
|
||||||
|
* @param id 用户ID
|
||||||
|
* @param file 头像文件
|
||||||
|
* @return 响应结果
|
||||||
|
*/
|
||||||
|
@Operation(summary = "管理员上传用户头像", description = "管理员上传指定用户头像并更新用户信息,需要管理员权限")
|
||||||
|
@SaCheckRole("admin")
|
||||||
|
@PostMapping("/{id}/upload/avatar")
|
||||||
|
public Result<String> adminUploadUserAvatar(
|
||||||
|
@Parameter(description = "用户ID", example = "1") @PathVariable Long id,
|
||||||
|
@Parameter(description = "头像文件") @RequestParam("file") MultipartFile file) {
|
||||||
|
String avatarPhysicalPath = fileService.uploadImage(file);
|
||||||
|
String avatarUrl = avatarPhysicalPath.replace(System.getProperty("user.dir") + File.separator + "music-houduan", "").replace(File.separator, "/");
|
||||||
|
|
||||||
|
userService.adminUpdateUserAvatar(id, avatarUrl);
|
||||||
|
|
||||||
|
return Result.success("上传成功", avatarUrl);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传用户头像
|
* 上传用户头像
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -107,6 +107,15 @@ public interface UserService {
|
|||||||
*/
|
*/
|
||||||
UserDTO adminUpdateUser(Long id, UserUpdateRequest request);
|
UserDTO adminUpdateUser(Long id, UserUpdateRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员上传并更新指定用户头像
|
||||||
|
*
|
||||||
|
* @param id 用户ID
|
||||||
|
* @param avatar 头像地址
|
||||||
|
* @return 用户DTO
|
||||||
|
*/
|
||||||
|
UserDTO adminUpdateUserAvatar(Long id, String avatar);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID获取用户实体
|
* 根据ID获取用户实体
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -48,9 +48,9 @@ public class FileServiceImpl implements FileService {
|
|||||||
// 检查文件类型
|
// 检查文件类型
|
||||||
String originalFilename = file.getOriginalFilename();
|
String originalFilename = file.getOriginalFilename();
|
||||||
String extension = getFileExtension(originalFilename);
|
String extension = getFileExtension(originalFilename);
|
||||||
List<String> imageTypes = Arrays.asList("jpg", "jpeg", "png", "gif");
|
List<String> imageTypes = Arrays.asList("jpg", "jpeg", "png", "gif", "webp");
|
||||||
if (!imageTypes.contains(extension.toLowerCase())) {
|
if (!imageTypes.contains(extension.toLowerCase())) {
|
||||||
throw new BusinessException("只能上传jpg、jpeg、png、gif格式的图片");
|
throw new BusinessException("只能上传jpg、jpeg、png、gif、webp格式的图片");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上传文件
|
// 上传文件
|
||||||
|
|||||||
@@ -329,6 +329,15 @@ public class UserServiceImpl implements UserService {
|
|||||||
return convertToDTO(savedUser);
|
return convertToDTO(savedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public UserDTO adminUpdateUserAvatar(Long id, String avatar) {
|
||||||
|
User user = getById(id);
|
||||||
|
user.setAvatar(avatar);
|
||||||
|
User savedUser = userRepository.save(user);
|
||||||
|
return convertToDTO(savedUser);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User getById(Long id) {
|
public User getById(Long id) {
|
||||||
return userRepository.findById(id)
|
return userRepository.findById(id)
|
||||||
|
|||||||
Reference in New Issue
Block a user