blob: 6b3d793244a43769e9634fe50045cb4258f7106b [file] [log] [blame]
22301115cf6dba22025-03-25 19:06:21 +08001package com.example.myproject.controller;
2
223011385e9c35a2025-06-04 15:52:45 +08003import com.example.myproject.entity.Users;
4import com.example.myproject.repository.UserRepository;
5import com.example.myproject.service.DynamicService;
6import com.example.myproject.service.TaskService;
22301115cf6dba22025-03-25 19:06:21 +08007import com.example.myproject.service.UserService;
22301115cf6dba22025-03-25 19:06:21 +08008import org.springframework.beans.factory.annotation.Autowired;
22301138b2533412025-06-05 00:18:58 +08009import org.springframework.http.ResponseEntity;
22301115cf6dba22025-03-25 19:06:21 +080010import org.springframework.web.bind.annotation.*;
22301138b2533412025-06-05 00:18:58 +080011import org.springframework.web.multipart.MultipartFile;
22301115cf6dba22025-03-25 19:06:21 +080012
22301138b2533412025-06-05 00:18:58 +080013import javax.servlet.http.HttpServletRequest;
223011385e9c35a2025-06-04 15:52:45 +080014import java.util.Map;
15import java.util.Optional;
16
2230111590135d72025-06-03 17:11:40 +080017import java.util.List;
223011385e9c35a2025-06-04 15:52:45 +080018import java.util.ArrayList;
19
22301115cf6dba22025-03-25 19:06:21 +080020
21@RestController
223011385e9c35a2025-06-04 15:52:45 +080022@RequestMapping("/echo/user")
22301115cf6dba22025-03-25 19:06:21 +080023public class UserController {
24
223011385e9c35a2025-06-04 15:52:45 +080025 @Autowired
22301115cf6dba22025-03-25 19:06:21 +080026 private UserService userService;
27
28 @Autowired
223011385e9c35a2025-06-04 15:52:45 +080029 private UserRepository userRepository;
22301115cf6dba22025-03-25 19:06:21 +080030
2230111590135d72025-06-03 17:11:40 +080031 @Autowired
223011385e9c35a2025-06-04 15:52:45 +080032 private DynamicService dynamicService;
22301115cf6dba22025-03-25 19:06:21 +080033
223011385e9c35a2025-06-04 15:52:45 +080034 // 接口:生成邀请码
35 @PostMapping("/getInviteCode")
36 public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
37 Long userId = Long.parseLong(request.get("user_id").toString());
38 return userService.generateInviteCode(userId);
22301115cf6dba22025-03-25 19:06:21 +080039 }
40
223011385e9c35a2025-06-04 15:52:45 +080041 //注册
2230111590135d72025-06-03 17:11:40 +080042 @PostMapping("/register")
223011385e9c35a2025-06-04 15:52:45 +080043 public Map<String, Object> register(@RequestBody Map<String, Object> request) {
44 String username = (String) request.get("username");
45 String email = (String) request.get("email");
46 String password = (String) request.get("password");
47 String role = (String) request.get("role");
48 String inviteCode = (String) request.get("inviteCode");
49
50 // 调用服务层的注册方法
51 String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
52
53 // 返回注册结果
54 return Map.of("msg", resultMessage);
55 }
56
57 //登录
58 @PostMapping("/login")
59 public Map<String, Object> login(@RequestBody Map<String, Object> request) {
60 String username = (String) request.get("username");
61 String password = (String) request.get("password");
62
63 // 调用服务层的登录方法
64 String resultMessage = userService.loginUser(username, password);
65
66 // 根据登录结果返回不同的响应
67 if (resultMessage.equals("登录成功")) {
68 // 查询用户信息
69 Optional<Users> user = userRepository.findByUsername(username);
70 if (user.isPresent()) {
71 // 将用户的所有信息作为返回值
72 return Map.of("msg", resultMessage, "user", user.get());
73 } else {
74 return Map.of("msg", "用户信息查询失败");
75 }
22301115cf6dba22025-03-25 19:06:21 +080076 } else {
223011385e9c35a2025-06-04 15:52:45 +080077 return Map.of("msg", resultMessage);
22301115cf6dba22025-03-25 19:06:21 +080078 }
79 }
80
223011385e9c35a2025-06-04 15:52:45 +080081 //修改密码
82 @PostMapping("/password")
83 public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
84 Long userId = Long.parseLong(request.get("user_id").toString());
85 String oldPassword = (String) request.get("old_password");
86 String newPassword = (String) request.get("new_password");
87 String confirmPassword = (String) request.get("confirm_password");
2230111590135d72025-06-03 17:11:40 +080088
223011385e9c35a2025-06-04 15:52:45 +080089 // 调用服务层的修改密码方法
90 String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
91
92 // 返回修改结果
93 return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
22301115cf6dba22025-03-25 19:06:21 +080094 }
95
223011385e9c35a2025-06-04 15:52:45 +080096 // 获取用户个人资料
97 @GetMapping("/{userId}/getProfile")
98 public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
99 return userService.getProfile(userId);
100 }
101
102 // 修改用户个人资料
103 @PutMapping("/{userId}/editProfile")
104 public Map<String, String> editProfile(
105 @PathVariable("userId") Long userId,
106 @RequestBody Map<String, Object> profileData) {
107
108 // 获取请求体中的修改数据
223011385e9c35a2025-06-04 15:52:45 +0800109 String nickname = (String) profileData.get("nickname");
110 String gender = (String) profileData.get("gender");
111 String description = (String) profileData.get("description");
112 String hobbies = (String) profileData.get("hobbies");
113
114 // 调用服务层方法进行修改
22301138b2533412025-06-05 00:18:58 +0800115 boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
223011385e9c35a2025-06-04 15:52:45 +0800116
117 // 返回操作结果消息
118 if (updated) {
119 return Map.of("message", "用户资料更新成功");
2230111590135d72025-06-03 17:11:40 +0800120 } else {
223011385e9c35a2025-06-04 15:52:45 +0800121 return Map.of("message", "用户不存在");
2230111590135d72025-06-03 17:11:40 +0800122 }
123 }
124
223011385e9c35a2025-06-04 15:52:45 +0800125 // 计算分享率
126 @GetMapping("/{user_id}/calculate-share-rate")
127 public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
128 return userService.calculateShareRate(userId);
2230111590135d72025-06-03 17:11:40 +0800129 }
130
223011385e9c35a2025-06-04 15:52:45 +0800131 // 获取用户所有好友的基本信息
132 @GetMapping("/{userId}/friends")
133 public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
134 List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
135 List<Map<String, Object>> friends = new ArrayList<>();
136
137 for (Long friendId : friendIds) {
138 Optional<Users> userOpt = userRepository.findById(friendId);
139 if (userOpt.isPresent()) {
140 Users user = userOpt.get();
141 Map<String, Object> friendInfo = Map.of(
142 "id", user.getUserId(),
143 "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
144 "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
145 "email", user.getEmail() != null ? user.getEmail() : "未填写"
146 );
147 friends.add(friendInfo);
148 }
2230111590135d72025-06-03 17:11:40 +0800149 }
150
223011385e9c35a2025-06-04 15:52:45 +0800151 return friends;
2230111590135d72025-06-03 17:11:40 +0800152 }
2230111590135d72025-06-03 17:11:40 +0800153
22301138b2533412025-06-05 00:18:58 +0800154 @PostMapping("/{userId}/uploadAvatar")
155 public Map<String, Object> uploadAvatar(
156 @PathVariable Long userId,
157 @RequestParam("file") MultipartFile file) {
158 return userService.uploadUserAvatar(userId, file);
159 }
160
161
YelinCuifdf4ed72025-05-26 11:49:36 +0800162
22301115cf6dba22025-03-25 19:06:21 +0800163}
223011385e9c35a2025-06-04 15:52:45 +0800164
165