blob: f9d0de43d8897bcf835b71398c44a8d2a22a5f1d [file] [log] [blame]
wuchimedesa1bf2782025-03-27 15:08:54 +08001package com.example.g8backend.controller;
2
夜雨声烦f995a442025-05-13 18:43:29 +08003import com.example.g8backend.dto.ApiResponse;
夜雨声烦19976162025-04-22 01:29:11 +08004import com.example.g8backend.entity.Message;
wuchimedesa1bf2782025-03-27 15:08:54 +08005import com.example.g8backend.entity.User;
夜雨声烦45c25dd2025-05-20 11:59:03 +08006import com.example.g8backend.entity.UserSignin;
wuchimedesa1bf2782025-03-27 15:08:54 +08007import com.example.g8backend.service.IUserService;
8import org.springframework.beans.factory.annotation.Autowired;
夜雨声烦45c25dd2025-05-20 11:59:03 +08009import org.springframework.format.annotation.DateTimeFormat;
wuchimedes223bfab2025-04-04 17:16:05 +080010import org.springframework.security.core.Authentication;
11import org.springframework.security.core.context.SecurityContextHolder;
wuchimedesa1bf2782025-03-27 15:08:54 +080012import org.springframework.web.bind.annotation.*;
夜雨声烦35c9da92025-05-20 00:12:48 +080013import com.example.g8backend.service.ISigningService;
wuchimedesa1bf2782025-03-27 15:08:54 +080014
夜雨声烦45c25dd2025-05-20 11:59:03 +080015import java.time.LocalDate;
夜雨声烦19976162025-04-22 01:29:11 +080016import java.util.List;
夜雨声烦7e6eb382025-04-22 01:18:00 +080017import java.util.Map;
18
wuchimedesa1bf2782025-03-27 15:08:54 +080019@RestController
wuchimedes223bfab2025-04-04 17:16:05 +080020@RequestMapping("/user")
wuchimedesa1bf2782025-03-27 15:08:54 +080021public class UserController {
22
23 @Autowired
24 private IUserService userService;
25
wuchimedes223bfab2025-04-04 17:16:05 +080026 // 获取已登录的用户信息
wuchimedesa1bf2782025-03-27 15:08:54 +080027 @GetMapping
夜雨声烦f995a442025-05-13 18:43:29 +080028 public ApiResponse<User> getUserInfo(){
wuchimedes223bfab2025-04-04 17:16:05 +080029 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
30 long userId = (long) authentication.getPrincipal();
31 User user = userService.getById(userId);
夜雨声烦f995a442025-05-13 18:43:29 +080032 user.setPassword(null); // 不返回密码
33 return ApiResponse.success(user);
wuchimedesa1bf2782025-03-27 15:08:54 +080034 }
夜雨声烦f995a442025-05-13 18:43:29 +080035
夜雨声烦19976162025-04-22 01:29:11 +080036 // ==================== 关注功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080037
夜雨声烦7e6eb382025-04-22 01:18:00 +080038 @PostMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080039 public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080040 Long currentUserId = getCurrentUserId();
41 boolean success = userService.followUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080042 return ApiResponse.success(Map.of("success", success));
夜雨声烦7e6eb382025-04-22 01:18:00 +080043 }
44
夜雨声烦19976162025-04-22 01:29:11 +080045 @DeleteMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080046 public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080047 Long currentUserId = getCurrentUserId();
48 boolean success = userService.unfollowUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080049 return ApiResponse.success(Map.of("success", success));
夜雨声烦19976162025-04-22 01:29:11 +080050 }
51
52 @GetMapping("/followings")
夜雨声烦f995a442025-05-13 18:43:29 +080053 public ApiResponse<List<User>> getFollowings() {
夜雨声烦19976162025-04-22 01:29:11 +080054 Long currentUserId = getCurrentUserId();
55 List<User> followings = userService.getFollowings(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080056 return ApiResponse.success(followings);
夜雨声烦19976162025-04-22 01:29:11 +080057 }
58
59 @GetMapping("/followers")
夜雨声烦f995a442025-05-13 18:43:29 +080060 public ApiResponse<List<User>> getFollowers() {
夜雨声烦19976162025-04-22 01:29:11 +080061 Long currentUserId = getCurrentUserId();
62 List<User> followers = userService.getFollowers(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080063 return ApiResponse.success(followers);
夜雨声烦19976162025-04-22 01:29:11 +080064 }
65
66 // ==================== 私信功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080067
夜雨声烦7e6eb382025-04-22 01:18:00 +080068 @PostMapping("/message/{receiverId}")
夜雨声烦f995a442025-05-13 18:43:29 +080069 public ApiResponse<Map<String, Long>> sendMessage(
夜雨声烦7e6eb382025-04-22 01:18:00 +080070 @PathVariable Long receiverId,
71 @RequestBody String content
72 ) {
夜雨声烦19976162025-04-22 01:29:11 +080073 Long senderId = getCurrentUserId();
夜雨声烦7e6eb382025-04-22 01:18:00 +080074 Long messageId = userService.sendMessage(senderId, receiverId, content);
夜雨声烦f995a442025-05-13 18:43:29 +080075 return ApiResponse.success(Map.of("messageId", messageId));
夜雨声烦7e6eb382025-04-22 01:18:00 +080076 }
夜雨声烦19976162025-04-22 01:29:11 +080077
78 @GetMapping("/messages/{otherUserId}")
夜雨声烦f995a442025-05-13 18:43:29 +080079 public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) {
夜雨声烦19976162025-04-22 01:29:11 +080080 Long currentUserId = getCurrentUserId();
81 List<Message> messages = userService.getMessages(currentUserId, otherUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080082 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +080083 }
84
85 @GetMapping("/messages/history")
夜雨声烦f995a442025-05-13 18:43:29 +080086 public ApiResponse<List<Message>> getMessageHistory() {
夜雨声烦19976162025-04-22 01:29:11 +080087 Long currentUserId = getCurrentUserId();
88 List<Message> messages = userService.getMessageHistory(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080089 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +080090 }
91
92 // ==================== 工具方法 ====================
93 private Long getCurrentUserId() {
94 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
95 return (Long) authentication.getPrincipal();
96 }
夜雨声烦35c9da92025-05-20 00:12:48 +080097
98 @Autowired
99 private ISigningService signingService; // 修改接口名称
100
101 @PostMapping("/signin")
夜雨声烦45c25dd2025-05-20 11:59:03 +0800102 public ApiResponse<String> signIn() { // 修改返回类型为 ApiResponse
103 Long userId = getCurrentUserId();
夜雨声烦35c9da92025-05-20 00:12:48 +0800104 boolean success = signingService.signIn(userId);
夜雨声烦45c25dd2025-05-20 11:59:03 +0800105 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);
夜雨声烦35c9da92025-05-20 00:12:48 +0800117 }
wuchimedesa1bf2782025-03-27 15:08:54 +0800118}