wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 3 | import com.example.g8backend.dto.ApiResponse; |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 4 | import com.example.g8backend.entity.Message; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 5 | import com.example.g8backend.entity.User; |
| 6 | import com.example.g8backend.service.IUserService; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 8 | import org.springframework.security.core.Authentication; |
| 9 | import org.springframework.security.core.context.SecurityContextHolder; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 10 | import org.springframework.web.bind.annotation.*; |
夜雨声烦 | 35c9da9 | 2025-05-20 00:12:48 +0800 | [diff] [blame] | 11 | import com.example.g8backend.service.ISigningService; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 12 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 13 | import java.util.List; |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 14 | import java.util.Map; |
| 15 | |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 16 | @RestController |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 17 | @RequestMapping("/user") |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 18 | public class UserController { |
| 19 | |
| 20 | @Autowired |
| 21 | private IUserService userService; |
| 22 | |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 23 | // 获取已登录的用户信息 |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 24 | @GetMapping |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 25 | public ApiResponse<User> getUserInfo(){ |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 26 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 27 | long userId = (long) authentication.getPrincipal(); |
| 28 | User user = userService.getById(userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 29 | user.setPassword(null); // 不返回密码 |
| 30 | return ApiResponse.success(user); |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 31 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 32 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 33 | // ==================== 关注功能 ==================== |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 34 | |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 35 | @PostMapping("/follow/{userId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 36 | public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 37 | Long currentUserId = getCurrentUserId(); |
| 38 | boolean success = userService.followUser(currentUserId, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 39 | return ApiResponse.success(Map.of("success", success)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 40 | } |
| 41 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 42 | @DeleteMapping("/follow/{userId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 43 | public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 44 | Long currentUserId = getCurrentUserId(); |
| 45 | boolean success = userService.unfollowUser(currentUserId, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 46 | return ApiResponse.success(Map.of("success", success)); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @GetMapping("/followings") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 50 | public ApiResponse<List<User>> getFollowings() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 51 | Long currentUserId = getCurrentUserId(); |
| 52 | List<User> followings = userService.getFollowings(currentUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 53 | return ApiResponse.success(followings); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @GetMapping("/followers") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 57 | public ApiResponse<List<User>> getFollowers() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 58 | Long currentUserId = getCurrentUserId(); |
| 59 | List<User> followers = userService.getFollowers(currentUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 60 | return ApiResponse.success(followers); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // ==================== 私信功能 ==================== |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 64 | |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 65 | @PostMapping("/message/{receiverId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 66 | public ApiResponse<Map<String, Long>> sendMessage( |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 67 | @PathVariable Long receiverId, |
| 68 | @RequestBody String content |
| 69 | ) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 70 | Long senderId = getCurrentUserId(); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 71 | Long messageId = userService.sendMessage(senderId, receiverId, content); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 72 | return ApiResponse.success(Map.of("messageId", messageId)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 73 | } |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 74 | |
| 75 | @GetMapping("/messages/{otherUserId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 76 | public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 77 | Long currentUserId = getCurrentUserId(); |
| 78 | List<Message> messages = userService.getMessages(currentUserId, otherUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 79 | return ApiResponse.success(messages); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @GetMapping("/messages/history") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 83 | public ApiResponse<List<Message>> getMessageHistory() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 84 | Long currentUserId = getCurrentUserId(); |
| 85 | List<Message> messages = userService.getMessageHistory(currentUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 86 | return ApiResponse.success(messages); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // ==================== 工具方法 ==================== |
| 90 | private Long getCurrentUserId() { |
| 91 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 92 | return (Long) authentication.getPrincipal(); |
| 93 | } |
夜雨声烦 | 35c9da9 | 2025-05-20 00:12:48 +0800 | [diff] [blame] | 94 | |
| 95 | @Autowired |
| 96 | private ISigningService signingService; // 修改接口名称 |
| 97 | |
| 98 | @PostMapping("/signin") |
| 99 | public String signIn() { |
| 100 | Long userId = getCurrentUserId(); // 假设从安全上下文中获取用户ID |
| 101 | boolean success = signingService.signIn(userId); |
| 102 | return success ? "签到成功" : "今日已签到"; |
| 103 | } |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 104 | } |