用户头像

Change-Id: I562c0cb7212c1ac9c7ad1dad96b136fc8c23269c
diff --git a/src/main/java/com/example/myproject/service/UserService.java b/src/main/java/com/example/myproject/service/UserService.java
index 0d37854..283a85c 100644
--- a/src/main/java/com/example/myproject/service/UserService.java
+++ b/src/main/java/com/example/myproject/service/UserService.java
@@ -1,12 +1,21 @@
 package com.example.myproject.service;
 
+import com.example.myproject.entity.User;
 import com.example.myproject.entity.Users;
 import com.example.myproject.entity.UserInviteCode;
 import com.example.myproject.repository.UserRepository;
 import com.example.myproject.repository.UserInviteCodeRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
+import javax.servlet.http.HttpServletRequest;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
 import java.util.*;
 
 @Service
@@ -207,7 +216,7 @@
     }
 
     // 修改用户个人资料
-    public boolean editProfile(Long userId, String avatarUrl, String nickname, String gender, String description, String hobbies) {
+    public boolean editProfile(Long userId, String nickname, String gender, String description, String hobbies) {
         Optional<Users> userOptional = userRepository.findById(userId);
 
         // 如果用户不存在,返回false
@@ -218,9 +227,6 @@
         Users user = userOptional.get();
 
         // 更新用户资料,只有传入值才会更新对应字段
-        if (avatarUrl != null) {
-            user.setAvatarUrl(avatarUrl);
-        }
         if (nickname != null) {
             user.setUsername(nickname);
         }
@@ -266,4 +272,40 @@
     }
 
 
+    private static final String AVATAR_DIR = "uploads/avatarUrl/";
+
+    public Map<String, Object> uploadUserAvatar(Long userId, MultipartFile file) {
+        Users user = userRepository.findById(userId)
+                .orElseThrow(() -> new RuntimeException("用户不存在"));
+
+        try {
+            String avatarUrl = saveAvatar(file, userId);
+            user.setAvatarUrl(avatarUrl);
+            userRepository.save(user);
+
+            Map<String, Object> response = new HashMap<>();
+            response.put("status", "success");
+            response.put("message", "头像上传成功");
+            response.put("userId", user.getUserId());
+            response.put("avatarUrl", avatarUrl);
+            return response;
+
+        } catch (IOException e) {
+            throw new RuntimeException("头像上传失败: " + e.getMessage());
+        }
+    }
+
+    // 保存头像文件并返回可访问 URL
+    public String saveAvatar(MultipartFile file, Long userId) throws IOException {
+        String originalFilename = file.getOriginalFilename();
+        String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
+        String fileName = userId + extension;  // 以用户ID作为文件名
+        Path path = Paths.get(AVATAR_DIR + fileName);
+        Files.createDirectories(path.getParent());
+        Files.write(path, file.getBytes());
+        return "/" + AVATAR_DIR + fileName;  // 返回相对URL路径
+    }
+
+
+
 }