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