blob: 224c138aeac514d3256db5224bacd998aa5ba168 [file] [log] [blame]
22301115cf6dba22025-03-25 19:06:21 +08001package com.example.myproject.controller;
2
22301138f6824512025-06-04 02:03:13 +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;
22301115cf6dba22025-03-25 19:06:21 +08009import org.springframework.web.bind.annotation.*;
10
22301138f6824512025-06-04 02:03:13 +080011import java.util.Map;
12import java.util.Optional;
13
YelinCuifdf4ed72025-05-26 11:49:36 +080014import java.util.List;
22301138f6824512025-06-04 02:03:13 +080015import java.util.ArrayList;
16
22301115cf6dba22025-03-25 19:06:21 +080017
18@RestController
22301138f6824512025-06-04 02:03:13 +080019@RequestMapping("/echo/user")
22301115cf6dba22025-03-25 19:06:21 +080020public class UserController {
21
22301138f6824512025-06-04 02:03:13 +080022 @Autowired
22301115cf6dba22025-03-25 19:06:21 +080023 private UserService userService;
24
25 @Autowired
22301138f6824512025-06-04 02:03:13 +080026 private UserRepository userRepository;
22301115cf6dba22025-03-25 19:06:21 +080027
28 @Autowired
22301138f6824512025-06-04 02:03:13 +080029 private DynamicService dynamicService;
22301115cf6dba22025-03-25 19:06:21 +080030
22301138f6824512025-06-04 02:03:13 +080031 // 接口:生成邀请码
32 @PostMapping("/getInviteCode")
33 public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
34 Long userId = Long.parseLong(request.get("user_id").toString());
35 return userService.generateInviteCode(userId);
22301115cf6dba22025-03-25 19:06:21 +080036 }
37
22301138f6824512025-06-04 02:03:13 +080038 //注册
22301115cf6dba22025-03-25 19:06:21 +080039 @PostMapping("/register")
22301138f6824512025-06-04 02:03:13 +080040 public Map<String, Object> register(@RequestBody Map<String, Object> request) {
41 String username = (String) request.get("username");
42 String email = (String) request.get("email");
43 String password = (String) request.get("password");
44 String role = (String) request.get("role");
45 String inviteCode = (String) request.get("inviteCode");
46
47 // 调用服务层的注册方法
48 String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
49
50 // 返回注册结果
51 return Map.of("msg", resultMessage);
52 }
53
54 //登录
55 @PostMapping("/login")
56 public Map<String, Object> login(@RequestBody Map<String, Object> request) {
57 String username = (String) request.get("username");
58 String password = (String) request.get("password");
59
60 // 调用服务层的登录方法
61 String resultMessage = userService.loginUser(username, password);
62
63 // 根据登录结果返回不同的响应
64 if (resultMessage.equals("登录成功")) {
65 // 查询用户信息
66 Optional<Users> user = userRepository.findByUsername(username);
67 if (user.isPresent()) {
68 // 将用户的所有信息作为返回值
69 return Map.of("msg", resultMessage, "user", user.get());
70 } else {
71 return Map.of("msg", "用户信息查询失败");
72 }
22301115cf6dba22025-03-25 19:06:21 +080073 } else {
22301138f6824512025-06-04 02:03:13 +080074 return Map.of("msg", resultMessage);
22301115cf6dba22025-03-25 19:06:21 +080075 }
76 }
77
22301138f6824512025-06-04 02:03:13 +080078 //修改密码
79 @PostMapping("/password")
80 public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
81 Long userId = Long.parseLong(request.get("user_id").toString());
82 String oldPassword = (String) request.get("old_password");
83 String newPassword = (String) request.get("new_password");
84 String confirmPassword = (String) request.get("confirm_password");
22301115cf6dba22025-03-25 19:06:21 +080085
22301138f6824512025-06-04 02:03:13 +080086 // 调用服务层的修改密码方法
87 String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
88
89 // 返回修改结果
90 return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
22301115cf6dba22025-03-25 19:06:21 +080091 }
92
22301138f6824512025-06-04 02:03:13 +080093 // 获取用户个人资料
94 @GetMapping("/{userId}/getProfile")
95 public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
96 return userService.getProfile(userId);
97 }
98
99 // 修改用户个人资料
100 @PutMapping("/{userId}/editProfile")
101 public Map<String, String> editProfile(
102 @PathVariable("userId") Long userId,
103 @RequestBody Map<String, Object> profileData) {
104
105 // 获取请求体中的修改数据
106 String avatarUrl = (String) profileData.get("avatarUrl");
107 String nickname = (String) profileData.get("nickname");
108 String gender = (String) profileData.get("gender");
109 String description = (String) profileData.get("description");
110 String hobbies = (String) profileData.get("hobbies");
111
112 // 调用服务层方法进行修改
113 boolean updated = userService.editProfile(userId, avatarUrl, nickname, gender, description, hobbies);
114
115 // 返回操作结果消息
116 if (updated) {
117 return Map.of("message", "用户资料更新成功");
22301115cf6dba22025-03-25 19:06:21 +0800118 } else {
22301138f6824512025-06-04 02:03:13 +0800119 return Map.of("message", "用户不存在");
22301115cf6dba22025-03-25 19:06:21 +0800120 }
121 }
122
22301138f6824512025-06-04 02:03:13 +0800123 // 计算分享率
124 @GetMapping("/{user_id}/calculate-share-rate")
125 public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
126 return userService.calculateShareRate(userId);
22301115cf6dba22025-03-25 19:06:21 +0800127 }
128
22301138f6824512025-06-04 02:03:13 +0800129 // 获取用户所有好友的基本信息
130 @GetMapping("/{userId}/friends")
131 public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
132 List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
133 List<Map<String, Object>> friends = new ArrayList<>();
134
135 for (Long friendId : friendIds) {
136 Optional<Users> userOpt = userRepository.findById(friendId);
137 if (userOpt.isPresent()) {
138 Users user = userOpt.get();
139 Map<String, Object> friendInfo = Map.of(
140 "id", user.getUserId(),
141 "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
142 "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
143 "email", user.getEmail() != null ? user.getEmail() : "未填写"
144 );
145 friends.add(friendInfo);
146 }
22301115cf6dba22025-03-25 19:06:21 +0800147 }
148
22301138f6824512025-06-04 02:03:13 +0800149 return friends;
22301115cf6dba22025-03-25 19:06:21 +0800150 }
YelinCuifdf4ed72025-05-26 11:49:36 +0800151
152
22301115cf6dba22025-03-25 19:06:21 +0800153}
22301138f6824512025-06-04 02:03:13 +0800154
155