blob: 41a33501ba4deb4c8fd01e95e139b8d7f8d2fe0b [file] [log] [blame]
JinGefe5140c2025-06-06 20:07:42 +08001// package com.example.myproject.controller;
2
3// import com.example.myproject.entity.Users;
4// import com.example.myproject.repository.UserRepository;
5// import com.example.myproject.service.DynamicService;
6// import com.example.myproject.service.TaskService;
7// import com.example.myproject.service.UserService;
8// import org.springframework.beans.factory.annotation.Autowired;
9// import org.springframework.http.ResponseEntity;
10// import org.springframework.web.bind.annotation.*;
11// import org.springframework.web.multipart.MultipartFile;
12
13// import javax.servlet.http.HttpServletRequest;
14// import java.util.Map;
15// import java.util.Optional;
16
17// import java.util.List;
18// import java.util.ArrayList;
19
20
21// @RestController
22// @RequestMapping("/echo/user")
23// public class UserController {
24
25// @Autowired
26// private UserService userService;
27
28// @Autowired
29// private UserRepository userRepository;
30
31// @Autowired
32// private DynamicService dynamicService;
33
34// // 接口:生成邀请码
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);
39// }
40
41// //注册
42// @PostMapping("/register")
43// 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// }
76// } else {
77// return Map.of("msg", resultMessage);
78// }
79// }
80
81// //修改密码
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");
88
89// // 调用服务层的修改密码方法
90// String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
91
92// // 返回修改结果
93// return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
94// }
95
96// // 获取用户个人资料
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// // 获取请求体中的修改数据
109// 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// // 调用服务层方法进行修改
115// boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
116
117// // 返回操作结果消息
118// if (updated) {
119// return Map.of("message", "用户资料更新成功");
120// } else {
121// return Map.of("message", "用户不存在");
122// }
123// }
124
125// // 计算分享率
126// @GetMapping("/{user_id}/calculate-share-rate")
127// public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
128// return userService.calculateShareRate(userId);
129// }
130
131// // 获取用户所有好友的基本信息
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// }
149// }
150
151// return friends;
152// }
153
154// @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
162
163// }
164
22301115cf6dba22025-03-25 19:06:21 +0800165package com.example.myproject.controller;
166
JinGefe5140c2025-06-06 20:07:42 +0800167import com.example.myproject.entity.FriendRelation;
223011385e9c35a2025-06-04 15:52:45 +0800168import com.example.myproject.entity.Users;
JinGefe5140c2025-06-06 20:07:42 +0800169import com.example.myproject.repository.FriendRelationRepository;
223011385e9c35a2025-06-04 15:52:45 +0800170import com.example.myproject.repository.UserRepository;
171import com.example.myproject.service.DynamicService;
22301115cf6dba22025-03-25 19:06:21 +0800172import com.example.myproject.service.UserService;
22301115cf6dba22025-03-25 19:06:21 +0800173import org.springframework.beans.factory.annotation.Autowired;
22301115cf6dba22025-03-25 19:06:21 +0800174import org.springframework.web.bind.annotation.*;
22301138b2533412025-06-05 00:18:58 +0800175import org.springframework.web.multipart.MultipartFile;
22301115cf6dba22025-03-25 19:06:21 +0800176
JinGefe5140c2025-06-06 20:07:42 +0800177import java.util.*;
223011385e9c35a2025-06-04 15:52:45 +0800178
22301115cf6dba22025-03-25 19:06:21 +0800179
180@RestController
223011385e9c35a2025-06-04 15:52:45 +0800181@RequestMapping("/echo/user")
22301115cf6dba22025-03-25 19:06:21 +0800182public class UserController {
183
223011385e9c35a2025-06-04 15:52:45 +0800184 @Autowired
22301115cf6dba22025-03-25 19:06:21 +0800185 private UserService userService;
186
187 @Autowired
223011385e9c35a2025-06-04 15:52:45 +0800188 private UserRepository userRepository;
22301115cf6dba22025-03-25 19:06:21 +0800189
2230111590135d72025-06-03 17:11:40 +0800190 @Autowired
223011385e9c35a2025-06-04 15:52:45 +0800191 private DynamicService dynamicService;
22301115cf6dba22025-03-25 19:06:21 +0800192
JinGefe5140c2025-06-06 20:07:42 +0800193 @Autowired
194 private FriendRelationRepository friendRelationRepository;
195
196
197 @PostMapping ("/addFriend")
198 public Map<String, Object> addFriend(@RequestBody Map<String, Object> request) {
199 //邮箱查用户
200 Optional<Users> userOptional = userRepository.findByEmail(request.get("email").toString());
201 // 如果用户不存在,返回null或者可以抛出异常
202 if (userOptional.isEmpty()) {
203 return Map.of("msg", "没有找到该用户"); // 可以返回 null 或者根据需要返回错误信息
204 }
205 Long userId = Long.parseLong(request.get("userid").toString()); //id查用户
206 Map<String, Object> users = userService.getProfile(userId);
207 // 如果用户不存在,返回null或者可以抛出异常
208 if (users.isEmpty()) {
209 return Map.of("msg", "当前登录账号异常"); // 可以返回 null 或者根据需要返回错误信息
210 }
211
212 FriendRelation newFriendRelation = new FriendRelation();
213 newFriendRelation.setUserId(userId);
214 newFriendRelation.setCreateTime(new Date());
215 newFriendRelation.setFriendId(userOptional.get().getUserId());
216
217 FriendRelation newFriendRelations = new FriendRelation();
218 newFriendRelations.setCreateTime(new Date());
219 newFriendRelations.setUserId(userOptional.get().getUserId());
220 newFriendRelations.setFriendId(userId);
221
222 friendRelationRepository.save(newFriendRelation);
223 friendRelationRepository.save(newFriendRelations);
224
225 return Map.of("msg", "添加成功");
226 }
227
223011385e9c35a2025-06-04 15:52:45 +0800228 // 接口:生成邀请码
229 @PostMapping("/getInviteCode")
230 public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
231 Long userId = Long.parseLong(request.get("user_id").toString());
232 return userService.generateInviteCode(userId);
22301115cf6dba22025-03-25 19:06:21 +0800233 }
234
223011385e9c35a2025-06-04 15:52:45 +0800235 //注册
2230111590135d72025-06-03 17:11:40 +0800236 @PostMapping("/register")
223011385e9c35a2025-06-04 15:52:45 +0800237 public Map<String, Object> register(@RequestBody Map<String, Object> request) {
238 String username = (String) request.get("username");
239 String email = (String) request.get("email");
240 String password = (String) request.get("password");
241 String role = (String) request.get("role");
242 String inviteCode = (String) request.get("inviteCode");
243
244 // 调用服务层的注册方法
245 String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
246
247 // 返回注册结果
248 return Map.of("msg", resultMessage);
249 }
250
251 //登录
252 @PostMapping("/login")
253 public Map<String, Object> login(@RequestBody Map<String, Object> request) {
254 String username = (String) request.get("username");
255 String password = (String) request.get("password");
256
257 // 调用服务层的登录方法
258 String resultMessage = userService.loginUser(username, password);
259
260 // 根据登录结果返回不同的响应
261 if (resultMessage.equals("登录成功")) {
262 // 查询用户信息
263 Optional<Users> user = userRepository.findByUsername(username);
264 if (user.isPresent()) {
265 // 将用户的所有信息作为返回值
266 return Map.of("msg", resultMessage, "user", user.get());
267 } else {
268 return Map.of("msg", "用户信息查询失败");
269 }
22301115cf6dba22025-03-25 19:06:21 +0800270 } else {
223011385e9c35a2025-06-04 15:52:45 +0800271 return Map.of("msg", resultMessage);
22301115cf6dba22025-03-25 19:06:21 +0800272 }
273 }
274
223011385e9c35a2025-06-04 15:52:45 +0800275 //修改密码
276 @PostMapping("/password")
277 public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
278 Long userId = Long.parseLong(request.get("user_id").toString());
279 String oldPassword = (String) request.get("old_password");
280 String newPassword = (String) request.get("new_password");
281 String confirmPassword = (String) request.get("confirm_password");
2230111590135d72025-06-03 17:11:40 +0800282
223011385e9c35a2025-06-04 15:52:45 +0800283 // 调用服务层的修改密码方法
284 String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
285
286 // 返回修改结果
287 return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
22301115cf6dba22025-03-25 19:06:21 +0800288 }
289
223011385e9c35a2025-06-04 15:52:45 +0800290 // 获取用户个人资料
291 @GetMapping("/{userId}/getProfile")
292 public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
293 return userService.getProfile(userId);
294 }
295
296 // 修改用户个人资料
297 @PutMapping("/{userId}/editProfile")
298 public Map<String, String> editProfile(
299 @PathVariable("userId") Long userId,
300 @RequestBody Map<String, Object> profileData) {
301
302 // 获取请求体中的修改数据
223011385e9c35a2025-06-04 15:52:45 +0800303 String nickname = (String) profileData.get("nickname");
304 String gender = (String) profileData.get("gender");
305 String description = (String) profileData.get("description");
306 String hobbies = (String) profileData.get("hobbies");
307
308 // 调用服务层方法进行修改
22301138b2533412025-06-05 00:18:58 +0800309 boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
223011385e9c35a2025-06-04 15:52:45 +0800310
311 // 返回操作结果消息
312 if (updated) {
313 return Map.of("message", "用户资料更新成功");
2230111590135d72025-06-03 17:11:40 +0800314 } else {
223011385e9c35a2025-06-04 15:52:45 +0800315 return Map.of("message", "用户不存在");
2230111590135d72025-06-03 17:11:40 +0800316 }
317 }
318
223011385e9c35a2025-06-04 15:52:45 +0800319 // 计算分享率
320 @GetMapping("/{user_id}/calculate-share-rate")
321 public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
322 return userService.calculateShareRate(userId);
2230111590135d72025-06-03 17:11:40 +0800323 }
324
223011385e9c35a2025-06-04 15:52:45 +0800325 // 获取用户所有好友的基本信息
326 @GetMapping("/{userId}/friends")
327 public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
328 List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
329 List<Map<String, Object>> friends = new ArrayList<>();
330
331 for (Long friendId : friendIds) {
332 Optional<Users> userOpt = userRepository.findById(friendId);
333 if (userOpt.isPresent()) {
334 Users user = userOpt.get();
335 Map<String, Object> friendInfo = Map.of(
336 "id", user.getUserId(),
337 "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
338 "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
339 "email", user.getEmail() != null ? user.getEmail() : "未填写"
340 );
341 friends.add(friendInfo);
342 }
2230111590135d72025-06-03 17:11:40 +0800343 }
344
223011385e9c35a2025-06-04 15:52:45 +0800345 return friends;
2230111590135d72025-06-03 17:11:40 +0800346 }
2230111590135d72025-06-03 17:11:40 +0800347
22301138b2533412025-06-05 00:18:58 +0800348 @PostMapping("/{userId}/uploadAvatar")
349 public Map<String, Object> uploadAvatar(
350 @PathVariable Long userId,
351 @RequestParam("file") MultipartFile file) {
352 return userService.uploadUserAvatar(userId, file);
353 }
354
355
YelinCuifdf4ed72025-05-26 11:49:36 +0800356
22301115cf6dba22025-03-25 19:06:21 +0800357}
223011385e9c35a2025-06-04 15:52:45 +0800358
359
JinGefe5140c2025-06-06 20:07:42 +0800360
361
362
363