2026-04-01 22:39:11 +08:00
|
|
|
|
package com.filesystem.controller;
|
|
|
|
|
|
|
|
|
|
|
|
import com.filesystem.entity.User;
|
|
|
|
|
|
import com.filesystem.security.UserPrincipal;
|
|
|
|
|
|
import com.filesystem.service.UserService;
|
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
|
@RequestMapping("/api/users")
|
|
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${file.storage.path:./uploads}")
|
|
|
|
|
|
private String storagePath;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取所有可用用户(用于文件共享等场景)
|
|
|
|
|
|
*/
|
|
|
|
|
|
@GetMapping
|
|
|
|
|
|
public ResponseEntity<?> getAllUsers(@AuthenticationPrincipal UserPrincipal principal) {
|
|
|
|
|
|
List<User> users = userService.getAllUsersExcept(principal.getUserId());
|
|
|
|
|
|
List<Map<String, Object>> result = users.stream()
|
|
|
|
|
|
.filter(u -> u.getStatus() == 1) // 只返回启用状态的用户
|
|
|
|
|
|
.map(u -> {
|
|
|
|
|
|
Map<String, Object> m = new java.util.HashMap<>();
|
|
|
|
|
|
m.put("id", u.getId());
|
|
|
|
|
|
m.put("username", u.getUsername());
|
|
|
|
|
|
m.put("nickname", u.getNickname() != null ? u.getNickname() : u.getUsername());
|
|
|
|
|
|
m.put("avatar", u.getAvatar());
|
|
|
|
|
|
m.put("signature", u.getSignature());
|
|
|
|
|
|
return m;
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
return ResponseEntity.ok(Map.of("data", result));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 上传头像
|
|
|
|
|
|
*/
|
|
|
|
|
|
@PostMapping("/avatar")
|
|
|
|
|
|
public ResponseEntity<?> uploadAvatar(
|
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
|
@RequestParam("avatar") MultipartFile file) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", "请选择图片"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 限制文件大小 2MB
|
|
|
|
|
|
if (file.getSize() > 2 * 1024 * 1024) {
|
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", "图片大小不能超过2MB"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存文件
|
|
|
|
|
|
String originalFilename = file.getOriginalFilename();
|
|
|
|
|
|
String ext = originalFilename != null && originalFilename.contains(".")
|
|
|
|
|
|
? originalFilename.substring(originalFilename.lastIndexOf("."))
|
|
|
|
|
|
: ".jpg";
|
|
|
|
|
|
String fileName = UUID.randomUUID().toString() + ext;
|
|
|
|
|
|
String datePath = java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy/MM"));
|
|
|
|
|
|
|
|
|
|
|
|
// 使用配置文件中的路径 + 日期目录
|
|
|
|
|
|
Path uploadPath = Paths.get(storagePath).toAbsolutePath().resolve("avatars").resolve(datePath);
|
|
|
|
|
|
if (!Files.exists(uploadPath)) {
|
|
|
|
|
|
Files.createDirectories(uploadPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Path filePath = uploadPath.resolve(fileName);
|
|
|
|
|
|
Files.copy(file.getInputStream(), filePath);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新用户头像
|
|
|
|
|
|
String avatarUrl = "/api/files/avatar/" + datePath + "/" + fileName;
|
|
|
|
|
|
userService.updateAvatar(principal.getUserId(), avatarUrl);
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(Map.of("data", Map.of("url", avatarUrl), "message", "上传成功"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前用户信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
@GetMapping("/me")
|
|
|
|
|
|
public ResponseEntity<?> getCurrentUser(@AuthenticationPrincipal UserPrincipal principal) {
|
|
|
|
|
|
User user = userService.findById(principal.getUserId());
|
|
|
|
|
|
Map<String, Object> result = new java.util.HashMap<>();
|
|
|
|
|
|
result.put("id", user.getId());
|
|
|
|
|
|
result.put("username", user.getUsername());
|
|
|
|
|
|
result.put("nickname", user.getNickname());
|
|
|
|
|
|
result.put("avatar", user.getAvatar());
|
|
|
|
|
|
result.put("signature", user.getSignature());
|
|
|
|
|
|
result.put("email", user.getEmail());
|
|
|
|
|
|
result.put("phone", user.getPhone());
|
|
|
|
|
|
return ResponseEntity.ok(Map.of("data", result));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新个人信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
@PutMapping("/profile")
|
|
|
|
|
|
public ResponseEntity<?> updateProfile(
|
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
|
@RequestBody Map<String, String> request) {
|
|
|
|
|
|
String nickname = request.get("nickname");
|
|
|
|
|
|
String signature = request.get("signature");
|
|
|
|
|
|
String phone = request.get("phone");
|
|
|
|
|
|
String email = request.get("email");
|
|
|
|
|
|
userService.updateProfile(principal.getUserId(), nickname, signature, phone, email);
|
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "更新成功"));
|
|
|
|
|
|
}
|
2026-04-02 15:02:54 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 修改密码
|
|
|
|
|
|
*/
|
|
|
|
|
|
@PutMapping("/password")
|
|
|
|
|
|
public ResponseEntity<?> changePassword(
|
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
|
@RequestBody Map<String, String> request) {
|
|
|
|
|
|
String oldPassword = request.get("oldPassword");
|
|
|
|
|
|
String newPassword = request.get("newPassword");
|
|
|
|
|
|
|
|
|
|
|
|
if (oldPassword == null || oldPassword.isEmpty() || newPassword == null || newPassword.isEmpty()) {
|
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", "请填写完整信息"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
userService.changePassword(principal.getUserId(), oldPassword, newPassword);
|
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "密码修改成功"));
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-04-01 22:39:11 +08:00
|
|
|
|
}
|