blob: b6aaab74f3e0c5f8485a778fcebe3fd6d84a008a [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;
Jin80d879a2025-06-09 20:53:18 +0800533import org.springframework.http.ResponseEntity;
22301115cf6dba22025-03-25 19:06:21 +0800534import org.springframework.web.bind.annotation.*;
22301138b2533412025-06-05 00:18:58 +0800535import org.springframework.web.multipart.MultipartFile;
22301115cf6dba22025-03-25 19:06:21 +0800536
JinGefe5140c2025-06-06 20:07:42 +0800537import java.util.*;
223011385e9c35a2025-06-04 15:52:45 +0800538
22301115cf6dba22025-03-25 19:06:21 +0800539
540@RestController
223011385e9c35a2025-06-04 15:52:45 +0800541@RequestMapping("/echo/user")
22301115cf6dba22025-03-25 19:06:21 +0800542public class UserController {
543
223011385e9c35a2025-06-04 15:52:45 +0800544 @Autowired
22301115cf6dba22025-03-25 19:06:21 +0800545 private UserService userService;
546
547 @Autowired
223011385e9c35a2025-06-04 15:52:45 +0800548 private UserRepository userRepository;
22301115cf6dba22025-03-25 19:06:21 +0800549
2230111590135d72025-06-03 17:11:40 +0800550 @Autowired
223011385e9c35a2025-06-04 15:52:45 +0800551 private DynamicService dynamicService;
22301115cf6dba22025-03-25 19:06:21 +0800552
JinGefe5140c2025-06-06 20:07:42 +0800553 @Autowired
554 private FriendRelationRepository friendRelationRepository;
555
556
557 @PostMapping ("/addFriend")
558 public Map<String, Object> addFriend(@RequestBody Map<String, Object> request) {
559 //邮箱查用户
560 Optional<Users> userOptional = userRepository.findByEmail(request.get("email").toString());
561 // 如果用户不存在,返回null或者可以抛出异常
562 if (userOptional.isEmpty()) {
563 return Map.of("msg", "没有找到该用户"); // 可以返回 null 或者根据需要返回错误信息
564 }
565 Long userId = Long.parseLong(request.get("userid").toString()); //id查用户
566 Map<String, Object> users = userService.getProfile(userId);
567 // 如果用户不存在,返回null或者可以抛出异常
568 if (users.isEmpty()) {
569 return Map.of("msg", "当前登录账号异常"); // 可以返回 null 或者根据需要返回错误信息
570 }
571
572 FriendRelation newFriendRelation = new FriendRelation();
573 newFriendRelation.setUserId(userId);
574 newFriendRelation.setCreateTime(new Date());
575 newFriendRelation.setFriendId(userOptional.get().getUserId());
576
577 FriendRelation newFriendRelations = new FriendRelation();
578 newFriendRelations.setCreateTime(new Date());
579 newFriendRelations.setUserId(userOptional.get().getUserId());
580 newFriendRelations.setFriendId(userId);
581
582 friendRelationRepository.save(newFriendRelation);
583 friendRelationRepository.save(newFriendRelations);
584
585 return Map.of("msg", "添加成功");
586 }
587
223011385e9c35a2025-06-04 15:52:45 +0800588 // 接口:生成邀请码
589 @PostMapping("/getInviteCode")
590 public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
591 Long userId = Long.parseLong(request.get("user_id").toString());
592 return userService.generateInviteCode(userId);
22301115cf6dba22025-03-25 19:06:21 +0800593 }
594
223011385e9c35a2025-06-04 15:52:45 +0800595 //注册
2230111590135d72025-06-03 17:11:40 +0800596 @PostMapping("/register")
223011385e9c35a2025-06-04 15:52:45 +0800597 public Map<String, Object> register(@RequestBody Map<String, Object> request) {
598 String username = (String) request.get("username");
599 String email = (String) request.get("email");
600 String password = (String) request.get("password");
601 String role = (String) request.get("role");
602 String inviteCode = (String) request.get("inviteCode");
603
604 // 调用服务层的注册方法
605 String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
606
607 // 返回注册结果
608 return Map.of("msg", resultMessage);
609 }
610
611 //登录
612 @PostMapping("/login")
613 public Map<String, Object> login(@RequestBody Map<String, Object> request) {
614 String username = (String) request.get("username");
615 String password = (String) request.get("password");
616
617 // 调用服务层的登录方法
618 String resultMessage = userService.loginUser(username, password);
619
620 // 根据登录结果返回不同的响应
621 if (resultMessage.equals("登录成功")) {
622 // 查询用户信息
623 Optional<Users> user = userRepository.findByUsername(username);
624 if (user.isPresent()) {
625 // 将用户的所有信息作为返回值
626 return Map.of("msg", resultMessage, "user", user.get());
627 } else {
628 return Map.of("msg", "用户信息查询失败");
629 }
22301115cf6dba22025-03-25 19:06:21 +0800630 } else {
223011385e9c35a2025-06-04 15:52:45 +0800631 return Map.of("msg", resultMessage);
22301115cf6dba22025-03-25 19:06:21 +0800632 }
633 }
634
223011385e9c35a2025-06-04 15:52:45 +0800635 //修改密码
636 @PostMapping("/password")
637 public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
638 Long userId = Long.parseLong(request.get("user_id").toString());
639 String oldPassword = (String) request.get("old_password");
640 String newPassword = (String) request.get("new_password");
641 String confirmPassword = (String) request.get("confirm_password");
2230111590135d72025-06-03 17:11:40 +0800642
223011385e9c35a2025-06-04 15:52:45 +0800643 // 调用服务层的修改密码方法
644 String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
645
646 // 返回修改结果
647 return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
22301115cf6dba22025-03-25 19:06:21 +0800648 }
649
223011385e9c35a2025-06-04 15:52:45 +0800650 // 获取用户个人资料
651 @GetMapping("/{userId}/getProfile")
652 public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
653 return userService.getProfile(userId);
654 }
655
656 // 修改用户个人资料
657 @PutMapping("/{userId}/editProfile")
658 public Map<String, String> editProfile(
659 @PathVariable("userId") Long userId,
660 @RequestBody Map<String, Object> profileData) {
661
662 // 获取请求体中的修改数据
223011385e9c35a2025-06-04 15:52:45 +0800663 String nickname = (String) profileData.get("nickname");
664 String gender = (String) profileData.get("gender");
665 String description = (String) profileData.get("description");
666 String hobbies = (String) profileData.get("hobbies");
667
668 // 调用服务层方法进行修改
22301138b2533412025-06-05 00:18:58 +0800669 boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
223011385e9c35a2025-06-04 15:52:45 +0800670
671 // 返回操作结果消息
672 if (updated) {
673 return Map.of("message", "用户资料更新成功");
2230111590135d72025-06-03 17:11:40 +0800674 } else {
223011385e9c35a2025-06-04 15:52:45 +0800675 return Map.of("message", "用户不存在");
2230111590135d72025-06-03 17:11:40 +0800676 }
677 }
678
223011385e9c35a2025-06-04 15:52:45 +0800679 // 计算分享率
680 @GetMapping("/{user_id}/calculate-share-rate")
681 public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
682 return userService.calculateShareRate(userId);
2230111590135d72025-06-03 17:11:40 +0800683 }
684
223011385e9c35a2025-06-04 15:52:45 +0800685 // 获取用户所有好友的基本信息
686 @GetMapping("/{userId}/friends")
687 public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
688 List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
689 List<Map<String, Object>> friends = new ArrayList<>();
690
691 for (Long friendId : friendIds) {
692 Optional<Users> userOpt = userRepository.findById(friendId);
693 if (userOpt.isPresent()) {
694 Users user = userOpt.get();
695 Map<String, Object> friendInfo = Map.of(
696 "id", user.getUserId(),
697 "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
698 "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
699 "email", user.getEmail() != null ? user.getEmail() : "未填写"
700 );
701 friends.add(friendInfo);
702 }
2230111590135d72025-06-03 17:11:40 +0800703 }
704
223011385e9c35a2025-06-04 15:52:45 +0800705 return friends;
2230111590135d72025-06-03 17:11:40 +0800706 }
2230111590135d72025-06-03 17:11:40 +0800707
22301138b2533412025-06-05 00:18:58 +0800708 @PostMapping("/{userId}/uploadAvatar")
709 public Map<String, Object> uploadAvatar(
710 @PathVariable Long userId,
711 @RequestParam("file") MultipartFile file) {
712 return userService.uploadUserAvatar(userId, file);
713 }
714
Krishyab0cc1882025-06-09 10:54:09 +0800715 @GetMapping("/users/{userId}/share-rate")
716 public String checkUserShareRate(@PathVariable Long userId) {
717 return userService.checkUserShareRate(userId);
718 }
22301138b2533412025-06-05 00:18:58 +0800719
Jin80d879a2025-06-09 20:53:18 +0800720 // 用户充值接口
721 @PostMapping("/recharge")
722 public ResponseEntity<?> recharge(@RequestParam Long userId, @RequestParam Integer amount) {
723 String result = userService.recharge(userId, amount);
724 if (result.startsWith("充值成功")) {
725 return ResponseEntity.ok(Map.of("status", "success", "message", result));
726 } else {
727 return ResponseEntity.badRequest().body(Map.of("status", "failure", "message", result));
728 }
729 }
730
YelinCuifdf4ed72025-05-26 11:49:36 +0800731
22301115cf6dba22025-03-25 19:06:21 +0800732}
223011385e9c35a2025-06-04 15:52:45 +0800733
734
JinGefe5140c2025-06-06 20:07:42 +0800735
736
737
738