blob: 8019967e26c5276667493570143ad593dff5e058 [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;
6import com.example.g8backend.service.IUserService;
7import org.springframework.beans.factory.annotation.Autowired;
wuchimedes223bfab2025-04-04 17:16:05 +08008import org.springframework.security.core.Authentication;
9import org.springframework.security.core.context.SecurityContextHolder;
wuchimedesa1bf2782025-03-27 15:08:54 +080010import org.springframework.web.bind.annotation.*;
夜雨声烦35c9da92025-05-20 00:12:48 +080011import com.example.g8backend.service.ISigningService;
wuchimedesa1bf2782025-03-27 15:08:54 +080012
夜雨声烦19976162025-04-22 01:29:11 +080013import java.util.List;
夜雨声烦7e6eb382025-04-22 01:18:00 +080014import java.util.Map;
15
wuchimedesa1bf2782025-03-27 15:08:54 +080016@RestController
wuchimedes223bfab2025-04-04 17:16:05 +080017@RequestMapping("/user")
wuchimedesa1bf2782025-03-27 15:08:54 +080018public class UserController {
19
20 @Autowired
21 private IUserService userService;
22
wuchimedes223bfab2025-04-04 17:16:05 +080023 // 获取已登录的用户信息
wuchimedesa1bf2782025-03-27 15:08:54 +080024 @GetMapping
夜雨声烦f995a442025-05-13 18:43:29 +080025 public ApiResponse<User> getUserInfo(){
wuchimedes223bfab2025-04-04 17:16:05 +080026 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
27 long userId = (long) authentication.getPrincipal();
28 User user = userService.getById(userId);
夜雨声烦f995a442025-05-13 18:43:29 +080029 user.setPassword(null); // 不返回密码
30 return ApiResponse.success(user);
wuchimedesa1bf2782025-03-27 15:08:54 +080031 }
夜雨声烦f995a442025-05-13 18:43:29 +080032
夜雨声烦19976162025-04-22 01:29:11 +080033 // ==================== 关注功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080034
夜雨声烦7e6eb382025-04-22 01:18:00 +080035 @PostMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080036 public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080037 Long currentUserId = getCurrentUserId();
38 boolean success = userService.followUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080039 return ApiResponse.success(Map.of("success", success));
夜雨声烦7e6eb382025-04-22 01:18:00 +080040 }
41
夜雨声烦19976162025-04-22 01:29:11 +080042 @DeleteMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080043 public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080044 Long currentUserId = getCurrentUserId();
45 boolean success = userService.unfollowUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080046 return ApiResponse.success(Map.of("success", success));
夜雨声烦19976162025-04-22 01:29:11 +080047 }
48
49 @GetMapping("/followings")
夜雨声烦f995a442025-05-13 18:43:29 +080050 public ApiResponse<List<User>> getFollowings() {
夜雨声烦19976162025-04-22 01:29:11 +080051 Long currentUserId = getCurrentUserId();
52 List<User> followings = userService.getFollowings(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080053 return ApiResponse.success(followings);
夜雨声烦19976162025-04-22 01:29:11 +080054 }
55
56 @GetMapping("/followers")
夜雨声烦f995a442025-05-13 18:43:29 +080057 public ApiResponse<List<User>> getFollowers() {
夜雨声烦19976162025-04-22 01:29:11 +080058 Long currentUserId = getCurrentUserId();
59 List<User> followers = userService.getFollowers(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080060 return ApiResponse.success(followers);
夜雨声烦19976162025-04-22 01:29:11 +080061 }
62
63 // ==================== 私信功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080064
夜雨声烦7e6eb382025-04-22 01:18:00 +080065 @PostMapping("/message/{receiverId}")
夜雨声烦f995a442025-05-13 18:43:29 +080066 public ApiResponse<Map<String, Long>> sendMessage(
夜雨声烦7e6eb382025-04-22 01:18:00 +080067 @PathVariable Long receiverId,
68 @RequestBody String content
69 ) {
夜雨声烦19976162025-04-22 01:29:11 +080070 Long senderId = getCurrentUserId();
夜雨声烦7e6eb382025-04-22 01:18:00 +080071 Long messageId = userService.sendMessage(senderId, receiverId, content);
夜雨声烦f995a442025-05-13 18:43:29 +080072 return ApiResponse.success(Map.of("messageId", messageId));
夜雨声烦7e6eb382025-04-22 01:18:00 +080073 }
夜雨声烦19976162025-04-22 01:29:11 +080074
75 @GetMapping("/messages/{otherUserId}")
夜雨声烦f995a442025-05-13 18:43:29 +080076 public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) {
夜雨声烦19976162025-04-22 01:29:11 +080077 Long currentUserId = getCurrentUserId();
78 List<Message> messages = userService.getMessages(currentUserId, otherUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080079 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +080080 }
81
82 @GetMapping("/messages/history")
夜雨声烦f995a442025-05-13 18:43:29 +080083 public ApiResponse<List<Message>> getMessageHistory() {
夜雨声烦19976162025-04-22 01:29:11 +080084 Long currentUserId = getCurrentUserId();
85 List<Message> messages = userService.getMessageHistory(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080086 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +080087 }
88
89 // ==================== 工具方法 ====================
90 private Long getCurrentUserId() {
91 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
92 return (Long) authentication.getPrincipal();
93 }
夜雨声烦35c9da92025-05-20 00:12:48 +080094
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 }
wuchimedesa1bf2782025-03-27 15:08:54 +0800104}