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; |
夜雨声烦 | 45c25dd | 2025-05-20 11:59:03 +0800 | [diff] [blame] | 6 | import com.example.g8backend.entity.UserSignin; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 7 | import com.example.g8backend.service.IUserService; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; |
夜雨声烦 | 45c25dd | 2025-05-20 11:59:03 +0800 | [diff] [blame] | 9 | import org.springframework.format.annotation.DateTimeFormat; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 10 | import org.springframework.security.core.Authentication; |
| 11 | import org.springframework.security.core.context.SecurityContextHolder; |
wuchimedes | a4c6c2b | 2025-06-07 22:42:11 +0800 | [diff] [blame] | 12 | import org.springframework.security.core.parameters.P; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 13 | import org.springframework.web.bind.annotation.*; |
夜雨声烦 | 35c9da9 | 2025-05-20 00:12:48 +0800 | [diff] [blame] | 14 | import com.example.g8backend.service.ISigningService; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 15 | |
夜雨声烦 | 45c25dd | 2025-05-20 11:59:03 +0800 | [diff] [blame] | 16 | import java.time.LocalDate; |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 17 | import java.util.List; |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 18 | import java.util.Map; |
| 19 | |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 20 | @RestController |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 21 | @RequestMapping("/user") |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 22 | public class UserController { |
| 23 | |
| 24 | @Autowired |
| 25 | private IUserService userService; |
| 26 | |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 27 | @GetMapping |
wuchimedes | a4c6c2b | 2025-06-07 22:42:11 +0800 | [diff] [blame] | 28 | public ApiResponse<Map<String, Object>> getCurrentUserInfo() { |
| 29 | Long userId = getCurrentUserId(); |
| 30 | return getUserInfo(userId); |
| 31 | } |
| 32 | |
| 33 | @GetMapping("/{userId}") |
| 34 | public ApiResponse<Map<String, Object>> getOtherUserInfo(@PathVariable(required = false) Long userId){ |
| 35 | return getUserInfo(userId); |
| 36 | } |
| 37 | |
| 38 | @GetMapping("/{userId}/follow-status") |
| 39 | public ApiResponse<Map<String, Boolean>> getFollowStatus(@PathVariable Long userId) { |
| 40 | Long currentUserId = getCurrentUserId(); |
| 41 | boolean isFollowing = userService.isFollowing(currentUserId, userId); |
| 42 | return ApiResponse.success(Map.of("isFollowing", isFollowing)); |
| 43 | } |
| 44 | |
| 45 | private ApiResponse<Map<String, Object>> getUserInfo(Long userId) { |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 46 | User user = userService.getById(userId); |
wuchimedes | a4c6c2b | 2025-06-07 22:42:11 +0800 | [diff] [blame] | 47 | if (user == null) { |
| 48 | return ApiResponse.error(404, "用户不存在"); |
| 49 | } |
| 50 | user.setPassword(null); |
| 51 | user.setPasskey(null); |
| 52 | |
| 53 | int followingCount = userService.getFollowingsCount(userId); |
| 54 | int followersCount = userService.getFollowersCount(userId); |
| 55 | |
| 56 | return ApiResponse.success(Map.of( |
| 57 | "userInfo", user, |
| 58 | "statistics", Map.of( |
| 59 | "followingCount", followingCount, |
| 60 | "followersCount", followersCount |
| 61 | ) |
| 62 | )); |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 63 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 64 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 65 | // ==================== 关注功能 ==================== |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 66 | |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 67 | @PostMapping("/follow/{userId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 68 | public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 69 | Long currentUserId = getCurrentUserId(); |
| 70 | boolean success = userService.followUser(currentUserId, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 71 | return ApiResponse.success(Map.of("success", success)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 72 | } |
| 73 | |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 74 | @DeleteMapping("/follow/{userId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 75 | public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 76 | Long currentUserId = getCurrentUserId(); |
| 77 | boolean success = userService.unfollowUser(currentUserId, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 78 | return ApiResponse.success(Map.of("success", success)); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @GetMapping("/followings") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 82 | public ApiResponse<List<User>> getFollowings() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 83 | Long currentUserId = getCurrentUserId(); |
| 84 | List<User> followings = userService.getFollowings(currentUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 85 | return ApiResponse.success(followings); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @GetMapping("/followers") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 89 | public ApiResponse<List<User>> getFollowers() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 90 | Long currentUserId = getCurrentUserId(); |
| 91 | List<User> followers = userService.getFollowers(currentUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 92 | return ApiResponse.success(followers); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // ==================== 私信功能 ==================== |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 96 | |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 97 | @PostMapping("/message/{receiverId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 98 | public ApiResponse<Map<String, Long>> sendMessage( |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 99 | @PathVariable Long receiverId, |
| 100 | @RequestBody String content |
| 101 | ) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 102 | Long senderId = getCurrentUserId(); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 103 | Long messageId = userService.sendMessage(senderId, receiverId, content); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 104 | return ApiResponse.success(Map.of("messageId", messageId)); |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 105 | } |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 106 | |
| 107 | @GetMapping("/messages/{otherUserId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 108 | public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 109 | Long currentUserId = getCurrentUserId(); |
| 110 | List<Message> messages = userService.getMessages(currentUserId, otherUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 111 | return ApiResponse.success(messages); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @GetMapping("/messages/history") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 115 | public ApiResponse<List<Message>> getMessageHistory() { |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 116 | Long currentUserId = getCurrentUserId(); |
| 117 | List<Message> messages = userService.getMessageHistory(currentUserId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 118 | return ApiResponse.success(messages); |
夜雨声烦 | 1997616 | 2025-04-22 01:29:11 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // ==================== 工具方法 ==================== |
| 122 | private Long getCurrentUserId() { |
| 123 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 124 | return (Long) authentication.getPrincipal(); |
| 125 | } |
夜雨声烦 | 35c9da9 | 2025-05-20 00:12:48 +0800 | [diff] [blame] | 126 | |
| 127 | @Autowired |
| 128 | private ISigningService signingService; // 修改接口名称 |
| 129 | |
| 130 | @PostMapping("/signin") |
夜雨声烦 | 45c25dd | 2025-05-20 11:59:03 +0800 | [diff] [blame] | 131 | public ApiResponse<String> signIn() { // 修改返回类型为 ApiResponse |
| 132 | Long userId = getCurrentUserId(); |
夜雨声烦 | 35c9da9 | 2025-05-20 00:12:48 +0800 | [diff] [blame] | 133 | boolean success = signingService.signIn(userId); |
夜雨声烦 | 45c25dd | 2025-05-20 11:59:03 +0800 | [diff] [blame] | 134 | String message = success ? "签到成功" : "今日已签到"; |
| 135 | return ApiResponse.success(message); |
| 136 | } |
| 137 | |
| 138 | // 新增获取时间段内的签到记录接口 |
| 139 | @GetMapping("/signins") |
| 140 | public ApiResponse<List<UserSignin>> getSignins( |
| 141 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, |
| 142 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) { |
| 143 | Long userId = getCurrentUserId(); |
| 144 | List<UserSignin> signins = signingService.getSigninsByDateRange(userId, startDate, endDate); |
| 145 | return ApiResponse.success(signins); |
夜雨声烦 | 35c9da9 | 2025-05-20 00:12:48 +0800 | [diff] [blame] | 146 | } |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 147 | } |