blob: 6b3d793244a43769e9634fe50045cb4258f7106b [file] [log] [blame]
package com.example.myproject.controller;
import com.example.myproject.entity.Users;
import com.example.myproject.repository.UserRepository;
import com.example.myproject.service.DynamicService;
import com.example.myproject.service.TaskService;
import com.example.myproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;
import java.util.List;
import java.util.ArrayList;
@RestController
@RequestMapping("/echo/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
private DynamicService dynamicService;
// 接口:生成邀请码
@PostMapping("/getInviteCode")
public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
Long userId = Long.parseLong(request.get("user_id").toString());
return userService.generateInviteCode(userId);
}
//注册
@PostMapping("/register")
public Map<String, Object> register(@RequestBody Map<String, Object> request) {
String username = (String) request.get("username");
String email = (String) request.get("email");
String password = (String) request.get("password");
String role = (String) request.get("role");
String inviteCode = (String) request.get("inviteCode");
// 调用服务层的注册方法
String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
// 返回注册结果
return Map.of("msg", resultMessage);
}
//登录
@PostMapping("/login")
public Map<String, Object> login(@RequestBody Map<String, Object> request) {
String username = (String) request.get("username");
String password = (String) request.get("password");
// 调用服务层的登录方法
String resultMessage = userService.loginUser(username, password);
// 根据登录结果返回不同的响应
if (resultMessage.equals("登录成功")) {
// 查询用户信息
Optional<Users> user = userRepository.findByUsername(username);
if (user.isPresent()) {
// 将用户的所有信息作为返回值
return Map.of("msg", resultMessage, "user", user.get());
} else {
return Map.of("msg", "用户信息查询失败");
}
} else {
return Map.of("msg", resultMessage);
}
}
//修改密码
@PostMapping("/password")
public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
Long userId = Long.parseLong(request.get("user_id").toString());
String oldPassword = (String) request.get("old_password");
String newPassword = (String) request.get("new_password");
String confirmPassword = (String) request.get("confirm_password");
// 调用服务层的修改密码方法
String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
// 返回修改结果
return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
}
// 获取用户个人资料
@GetMapping("/{userId}/getProfile")
public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
return userService.getProfile(userId);
}
// 修改用户个人资料
@PutMapping("/{userId}/editProfile")
public Map<String, String> editProfile(
@PathVariable("userId") Long userId,
@RequestBody Map<String, Object> profileData) {
// 获取请求体中的修改数据
String nickname = (String) profileData.get("nickname");
String gender = (String) profileData.get("gender");
String description = (String) profileData.get("description");
String hobbies = (String) profileData.get("hobbies");
// 调用服务层方法进行修改
boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
// 返回操作结果消息
if (updated) {
return Map.of("message", "用户资料更新成功");
} else {
return Map.of("message", "用户不存在");
}
}
// 计算分享率
@GetMapping("/{user_id}/calculate-share-rate")
public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
return userService.calculateShareRate(userId);
}
// 获取用户所有好友的基本信息
@GetMapping("/{userId}/friends")
public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
List<Map<String, Object>> friends = new ArrayList<>();
for (Long friendId : friendIds) {
Optional<Users> userOpt = userRepository.findById(friendId);
if (userOpt.isPresent()) {
Users user = userOpt.get();
Map<String, Object> friendInfo = Map.of(
"id", user.getUserId(),
"avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
"nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
"email", user.getEmail() != null ? user.getEmail() : "未填写"
);
friends.add(friendInfo);
}
}
return friends;
}
@PostMapping("/{userId}/uploadAvatar")
public Map<String, Object> uploadAvatar(
@PathVariable Long userId,
@RequestParam("file") MultipartFile file) {
return userService.uploadUserAvatar(userId, file);
}
}