JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 1 | // 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 | |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 165 | package com.example.myproject.controller; |
| 166 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 167 | import com.example.myproject.entity.FriendRelation; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 168 | import com.example.myproject.entity.Users; |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 169 | import com.example.myproject.repository.FriendRelationRepository; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 170 | import com.example.myproject.repository.UserRepository; |
| 171 | import com.example.myproject.service.DynamicService; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 172 | import com.example.myproject.service.UserService; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 173 | import org.springframework.beans.factory.annotation.Autowired; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 174 | import org.springframework.web.bind.annotation.*; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 175 | import org.springframework.web.multipart.MultipartFile; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 176 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 177 | import java.util.*; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 178 | |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 179 | |
| 180 | @RestController |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 181 | @RequestMapping("/echo/user") |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 182 | public class UserController { |
| 183 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 184 | @Autowired |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 185 | private UserService userService; |
| 186 | |
| 187 | @Autowired |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 188 | private UserRepository userRepository; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 189 | |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 190 | @Autowired |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 191 | private DynamicService dynamicService; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 192 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 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 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 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); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 233 | } |
| 234 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 235 | //注册 |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 236 | @PostMapping("/register") |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 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 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 270 | } else { |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 271 | return Map.of("msg", resultMessage); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 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"); |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 282 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 283 | // 调用服务层的修改密码方法 |
| 284 | String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword); |
| 285 | |
| 286 | // 返回修改结果 |
| 287 | return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error"); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 288 | } |
| 289 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 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 | // 获取请求体中的修改数据 |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 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 | // 调用服务层方法进行修改 |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 309 | boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies); |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 310 | |
| 311 | // 返回操作结果消息 |
| 312 | if (updated) { |
| 313 | return Map.of("message", "用户资料更新成功"); |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 314 | } else { |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 315 | return Map.of("message", "用户不存在"); |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 319 | // 计算分享率 |
| 320 | @GetMapping("/{user_id}/calculate-share-rate") |
| 321 | public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) { |
| 322 | return userService.calculateShareRate(userId); |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 323 | } |
| 324 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 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 | } |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 343 | } |
| 344 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 345 | return friends; |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 346 | } |
22301115 | 90135d7 | 2025-06-03 17:11:40 +0800 | [diff] [blame] | 347 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 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 | |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 356 | |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 357 | } |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 358 | |
| 359 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 360 | |
| 361 | |
| 362 | |
| 363 | |