blob: dc4073bb75e562cf2004c9c947ff56ad818d848a [file] [log] [blame]
Krishyab0cc1882025-06-09 10:54:09 +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//
165//package com.example.myproject.controller;
166//
167//import com.example.myproject.entity.FriendRelation;
168//import com.example.myproject.entity.Users;
169//import com.example.myproject.repository.FriendRelationRepository;
170//import com.example.myproject.repository.UserRepository;
171//import com.example.myproject.service.DynamicService;
172//import com.example.myproject.service.UserService;
173//import org.springframework.beans.factory.annotation.Autowired;
174//import org.springframework.web.bind.annotation.*;
175//import org.springframework.web.multipart.MultipartFile;
176//
177//import java.util.*;
178//
179//
180//@RestController
181//@RequestMapping("/echo/user")
182//public class UserController {
183//
184// @Autowired
185// private UserService userService;
186//
187// @Autowired
188// private UserRepository userRepository;
189//
190// @Autowired
191// private DynamicService dynamicService;
192//
193// @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//
228// // 接口:生成邀请码
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);
233// }
234//
235// //注册
236// @PostMapping("/register")
237// 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// }
270// } else {
271// return Map.of("msg", resultMessage);
272// }
273// }
274//
275// //修改密码
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");
282//
283// // 调用服务层的修改密码方法
284// String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
285//
286// // 返回修改结果
287// return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
288// }
289//
290// // 获取用户个人资料
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// // 获取请求体中的修改数据
303// 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// // 调用服务层方法进行修改
309// boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
310//
311// // 返回操作结果消息
312// if (updated) {
313// return Map.of("message", "用户资料更新成功");
314// } else {
315// return Map.of("message", "用户不存在");
316// }
317// }
318//
319// // 计算分享率
320// @GetMapping("/{user_id}/calculate-share-rate")
321// public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
322// return userService.calculateShareRate(userId);
323// }
324//
325// // 获取用户所有好友的基本信息
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// }
343// }
344//
345// return friends;
346// }
347//
348// @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//
356//
357//}
358//
359
JinGefe5140c2025-06-06 20:07:42 +0800360// package com.example.myproject.controller;
361
362// import com.example.myproject.entity.Users;
363// import com.example.myproject.repository.UserRepository;
364// import com.example.myproject.service.DynamicService;
365// import com.example.myproject.service.TaskService;
366// import com.example.myproject.service.UserService;
367// import org.springframework.beans.factory.annotation.Autowired;
368// import org.springframework.http.ResponseEntity;
369// import org.springframework.web.bind.annotation.*;
370// import org.springframework.web.multipart.MultipartFile;
371
372// import javax.servlet.http.HttpServletRequest;
373// import java.util.Map;
374// import java.util.Optional;
375
376// import java.util.List;
377// import java.util.ArrayList;
378
379
380// @RestController
381// @RequestMapping("/echo/user")
382// public class UserController {
383
384// @Autowired
385// private UserService userService;
386
387// @Autowired
388// private UserRepository userRepository;
389
390// @Autowired
391// private DynamicService dynamicService;
392
393// // 接口:生成邀请码
394// @PostMapping("/getInviteCode")
395// public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
396// Long userId = Long.parseLong(request.get("user_id").toString());
397// return userService.generateInviteCode(userId);
398// }
399
400// //注册
401// @PostMapping("/register")
402// public Map<String, Object> register(@RequestBody Map<String, Object> request) {
403// String username = (String) request.get("username");
404// String email = (String) request.get("email");
405// String password = (String) request.get("password");
406// String role = (String) request.get("role");
407// String inviteCode = (String) request.get("inviteCode");
408
409// // 调用服务层的注册方法
410// String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
411
412// // 返回注册结果
413// return Map.of("msg", resultMessage);
414// }
415
416// //登录
417// @PostMapping("/login")
418// public Map<String, Object> login(@RequestBody Map<String, Object> request) {
419// String username = (String) request.get("username");
420// String password = (String) request.get("password");
421
422// // 调用服务层的登录方法
423// String resultMessage = userService.loginUser(username, password);
424
425// // 根据登录结果返回不同的响应
426// if (resultMessage.equals("登录成功")) {
427// // 查询用户信息
428// Optional<Users> user = userRepository.findByUsername(username);
429// if (user.isPresent()) {
430// // 将用户的所有信息作为返回值
431// return Map.of("msg", resultMessage, "user", user.get());
432// } else {
433// return Map.of("msg", "用户信息查询失败");
434// }
435// } else {
436// return Map.of("msg", resultMessage);
437// }
438// }
439
440// //修改密码
441// @PostMapping("/password")
442// public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
443// Long userId = Long.parseLong(request.get("user_id").toString());
444// String oldPassword = (String) request.get("old_password");
445// String newPassword = (String) request.get("new_password");
446// String confirmPassword = (String) request.get("confirm_password");
447
448// // 调用服务层的修改密码方法
449// String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
450
451// // 返回修改结果
452// return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
453// }
454
455// // 获取用户个人资料
456// @GetMapping("/{userId}/getProfile")
457// public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
458// return userService.getProfile(userId);
459// }
460
461// // 修改用户个人资料
462// @PutMapping("/{userId}/editProfile")
463// public Map<String, String> editProfile(
464// @PathVariable("userId") Long userId,
465// @RequestBody Map<String, Object> profileData) {
466
467// // 获取请求体中的修改数据
468// String nickname = (String) profileData.get("nickname");
469// String gender = (String) profileData.get("gender");
470// String description = (String) profileData.get("description");
471// String hobbies = (String) profileData.get("hobbies");
472
473// // 调用服务层方法进行修改
474// boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
475
476// // 返回操作结果消息
477// if (updated) {
478// return Map.of("message", "用户资料更新成功");
479// } else {
480// return Map.of("message", "用户不存在");
481// }
482// }
483
484// // 计算分享率
485// @GetMapping("/{user_id}/calculate-share-rate")
486// public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
487// return userService.calculateShareRate(userId);
488// }
489
490// // 获取用户所有好友的基本信息
491// @GetMapping("/{userId}/friends")
492// public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
493// List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
494// List<Map<String, Object>> friends = new ArrayList<>();
495
496// for (Long friendId : friendIds) {
497// Optional<Users> userOpt = userRepository.findById(friendId);
498// if (userOpt.isPresent()) {
499// Users user = userOpt.get();
500// Map<String, Object> friendInfo = Map.of(
501// "id", user.getUserId(),
502// "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
503// "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
504// "email", user.getEmail() != null ? user.getEmail() : "未填写"
505// );
506// friends.add(friendInfo);
507// }
508// }
509
510// return friends;
511// }
512
513// @PostMapping("/{userId}/uploadAvatar")
514// public Map<String, Object> uploadAvatar(
515// @PathVariable Long userId,
516// @RequestParam("file") MultipartFile file) {
517// return userService.uploadUserAvatar(userId, file);
518// }
519
520
521
522// }
523
22301115cf6dba22025-03-25 19:06:21 +0800524package com.example.myproject.controller;
525
JinGefe5140c2025-06-06 20:07:42 +0800526import com.example.myproject.entity.FriendRelation;
223011385e9c35a2025-06-04 15:52:45 +0800527import com.example.myproject.entity.Users;
JinGefe5140c2025-06-06 20:07:42 +0800528import com.example.myproject.repository.FriendRelationRepository;
223011385e9c35a2025-06-04 15:52:45 +0800529import com.example.myproject.repository.UserRepository;
530import com.example.myproject.service.DynamicService;
22301115cf6dba22025-03-25 19:06:21 +0800531import com.example.myproject.service.UserService;
22301115cf6dba22025-03-25 19:06:21 +0800532import org.springframework.beans.factory.annotation.Autowired;
22301115cf6dba22025-03-25 19:06:21 +0800533import org.springframework.web.bind.annotation.*;
22301138b2533412025-06-05 00:18:58 +0800534import org.springframework.web.multipart.MultipartFile;
22301115cf6dba22025-03-25 19:06:21 +0800535
JinGefe5140c2025-06-06 20:07:42 +0800536import java.util.*;
223011385e9c35a2025-06-04 15:52:45 +0800537
22301115cf6dba22025-03-25 19:06:21 +0800538
539@RestController
223011385e9c35a2025-06-04 15:52:45 +0800540@RequestMapping("/echo/user")
22301115cf6dba22025-03-25 19:06:21 +0800541public class UserController {
542
223011385e9c35a2025-06-04 15:52:45 +0800543 @Autowired
22301115cf6dba22025-03-25 19:06:21 +0800544 private UserService userService;
545
546 @Autowired
223011385e9c35a2025-06-04 15:52:45 +0800547 private UserRepository userRepository;
22301115cf6dba22025-03-25 19:06:21 +0800548
2230111590135d72025-06-03 17:11:40 +0800549 @Autowired
223011385e9c35a2025-06-04 15:52:45 +0800550 private DynamicService dynamicService;
22301115cf6dba22025-03-25 19:06:21 +0800551
JinGefe5140c2025-06-06 20:07:42 +0800552 @Autowired
553 private FriendRelationRepository friendRelationRepository;
554
555
556 @PostMapping ("/addFriend")
557 public Map<String, Object> addFriend(@RequestBody Map<String, Object> request) {
558 //邮箱查用户
559 Optional<Users> userOptional = userRepository.findByEmail(request.get("email").toString());
560 // 如果用户不存在,返回null或者可以抛出异常
561 if (userOptional.isEmpty()) {
562 return Map.of("msg", "没有找到该用户"); // 可以返回 null 或者根据需要返回错误信息
563 }
564 Long userId = Long.parseLong(request.get("userid").toString()); //id查用户
565 Map<String, Object> users = userService.getProfile(userId);
566 // 如果用户不存在,返回null或者可以抛出异常
567 if (users.isEmpty()) {
568 return Map.of("msg", "当前登录账号异常"); // 可以返回 null 或者根据需要返回错误信息
569 }
570
571 FriendRelation newFriendRelation = new FriendRelation();
572 newFriendRelation.setUserId(userId);
573 newFriendRelation.setCreateTime(new Date());
574 newFriendRelation.setFriendId(userOptional.get().getUserId());
575
576 FriendRelation newFriendRelations = new FriendRelation();
577 newFriendRelations.setCreateTime(new Date());
578 newFriendRelations.setUserId(userOptional.get().getUserId());
579 newFriendRelations.setFriendId(userId);
580
581 friendRelationRepository.save(newFriendRelation);
582 friendRelationRepository.save(newFriendRelations);
583
584 return Map.of("msg", "添加成功");
585 }
586
223011385e9c35a2025-06-04 15:52:45 +0800587 // 接口:生成邀请码
588 @PostMapping("/getInviteCode")
589 public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
590 Long userId = Long.parseLong(request.get("user_id").toString());
591 return userService.generateInviteCode(userId);
22301115cf6dba22025-03-25 19:06:21 +0800592 }
593
223011385e9c35a2025-06-04 15:52:45 +0800594 //注册
2230111590135d72025-06-03 17:11:40 +0800595 @PostMapping("/register")
223011385e9c35a2025-06-04 15:52:45 +0800596 public Map<String, Object> register(@RequestBody Map<String, Object> request) {
597 String username = (String) request.get("username");
598 String email = (String) request.get("email");
599 String password = (String) request.get("password");
600 String role = (String) request.get("role");
601 String inviteCode = (String) request.get("inviteCode");
602
603 // 调用服务层的注册方法
604 String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
605
606 // 返回注册结果
607 return Map.of("msg", resultMessage);
608 }
609
610 //登录
611 @PostMapping("/login")
612 public Map<String, Object> login(@RequestBody Map<String, Object> request) {
613 String username = (String) request.get("username");
614 String password = (String) request.get("password");
615
616 // 调用服务层的登录方法
617 String resultMessage = userService.loginUser(username, password);
618
619 // 根据登录结果返回不同的响应
620 if (resultMessage.equals("登录成功")) {
621 // 查询用户信息
622 Optional<Users> user = userRepository.findByUsername(username);
623 if (user.isPresent()) {
624 // 将用户的所有信息作为返回值
625 return Map.of("msg", resultMessage, "user", user.get());
626 } else {
627 return Map.of("msg", "用户信息查询失败");
628 }
22301115cf6dba22025-03-25 19:06:21 +0800629 } else {
223011385e9c35a2025-06-04 15:52:45 +0800630 return Map.of("msg", resultMessage);
22301115cf6dba22025-03-25 19:06:21 +0800631 }
632 }
633
223011385e9c35a2025-06-04 15:52:45 +0800634 //修改密码
635 @PostMapping("/password")
636 public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
637 Long userId = Long.parseLong(request.get("user_id").toString());
638 String oldPassword = (String) request.get("old_password");
639 String newPassword = (String) request.get("new_password");
640 String confirmPassword = (String) request.get("confirm_password");
2230111590135d72025-06-03 17:11:40 +0800641
223011385e9c35a2025-06-04 15:52:45 +0800642 // 调用服务层的修改密码方法
643 String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
644
645 // 返回修改结果
646 return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
22301115cf6dba22025-03-25 19:06:21 +0800647 }
648
223011385e9c35a2025-06-04 15:52:45 +0800649 // 获取用户个人资料
650 @GetMapping("/{userId}/getProfile")
651 public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
652 return userService.getProfile(userId);
653 }
654
655 // 修改用户个人资料
656 @PutMapping("/{userId}/editProfile")
657 public Map<String, String> editProfile(
658 @PathVariable("userId") Long userId,
659 @RequestBody Map<String, Object> profileData) {
660
661 // 获取请求体中的修改数据
223011385e9c35a2025-06-04 15:52:45 +0800662 String nickname = (String) profileData.get("nickname");
663 String gender = (String) profileData.get("gender");
664 String description = (String) profileData.get("description");
665 String hobbies = (String) profileData.get("hobbies");
666
667 // 调用服务层方法进行修改
22301138b2533412025-06-05 00:18:58 +0800668 boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
223011385e9c35a2025-06-04 15:52:45 +0800669
670 // 返回操作结果消息
671 if (updated) {
672 return Map.of("message", "用户资料更新成功");
2230111590135d72025-06-03 17:11:40 +0800673 } else {
223011385e9c35a2025-06-04 15:52:45 +0800674 return Map.of("message", "用户不存在");
2230111590135d72025-06-03 17:11:40 +0800675 }
676 }
677
223011385e9c35a2025-06-04 15:52:45 +0800678 // 计算分享率
679 @GetMapping("/{user_id}/calculate-share-rate")
680 public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
681 return userService.calculateShareRate(userId);
2230111590135d72025-06-03 17:11:40 +0800682 }
683
223011385e9c35a2025-06-04 15:52:45 +0800684 // 获取用户所有好友的基本信息
685 @GetMapping("/{userId}/friends")
686 public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
687 List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
688 List<Map<String, Object>> friends = new ArrayList<>();
689
690 for (Long friendId : friendIds) {
691 Optional<Users> userOpt = userRepository.findById(friendId);
692 if (userOpt.isPresent()) {
693 Users user = userOpt.get();
694 Map<String, Object> friendInfo = Map.of(
695 "id", user.getUserId(),
696 "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
697 "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
698 "email", user.getEmail() != null ? user.getEmail() : "未填写"
699 );
700 friends.add(friendInfo);
701 }
2230111590135d72025-06-03 17:11:40 +0800702 }
703
223011385e9c35a2025-06-04 15:52:45 +0800704 return friends;
2230111590135d72025-06-03 17:11:40 +0800705 }
2230111590135d72025-06-03 17:11:40 +0800706
22301138b2533412025-06-05 00:18:58 +0800707 @PostMapping("/{userId}/uploadAvatar")
708 public Map<String, Object> uploadAvatar(
709 @PathVariable Long userId,
710 @RequestParam("file") MultipartFile file) {
711 return userService.uploadUserAvatar(userId, file);
712 }
713
Krishyab0cc1882025-06-09 10:54:09 +0800714 @GetMapping("/users/{userId}/share-rate")
715 public String checkUserShareRate(@PathVariable Long userId) {
716 return userService.checkUserShareRate(userId);
717 }
22301138b2533412025-06-05 00:18:58 +0800718
YelinCuifdf4ed72025-05-26 11:49:36 +0800719
22301115cf6dba22025-03-25 19:06:21 +0800720}
223011385e9c35a2025-06-04 15:52:45 +0800721
722
JinGefe5140c2025-06-06 20:07:42 +0800723
724
725
726