| package com.example.g8backend.controller; |
| |
| import com.example.g8backend.dto.ApiResponse; |
| import com.example.g8backend.entity.Message; |
| import com.example.g8backend.entity.User; |
| import com.example.g8backend.entity.UserSignin; |
| import com.example.g8backend.service.IUserService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.format.annotation.DateTimeFormat; |
| import org.springframework.security.core.Authentication; |
| import org.springframework.security.core.context.SecurityContextHolder; |
| import org.springframework.security.core.parameters.P; |
| import org.springframework.web.bind.annotation.*; |
| import com.example.g8backend.service.ISigningService; |
| |
| import java.time.LocalDate; |
| import java.util.List; |
| import java.util.Map; |
| |
| @RestController |
| @RequestMapping("/user") |
| public class UserController { |
| |
| @Autowired |
| private IUserService userService; |
| |
| @GetMapping |
| public ApiResponse<Map<String, Object>> getCurrentUserInfo() { |
| Long userId = getCurrentUserId(); |
| return getUserInfo(userId); |
| } |
| |
| @GetMapping("/{userId}") |
| public ApiResponse<Map<String, Object>> getOtherUserInfo(@PathVariable(required = false) Long userId){ |
| return getUserInfo(userId); |
| } |
| |
| @GetMapping("/{userId}/follow-status") |
| public ApiResponse<Map<String, Boolean>> getFollowStatus(@PathVariable Long userId) { |
| Long currentUserId = getCurrentUserId(); |
| boolean isFollowing = userService.isFollowing(currentUserId, userId); |
| return ApiResponse.success(Map.of("isFollowing", isFollowing)); |
| } |
| |
| private ApiResponse<Map<String, Object>> getUserInfo(Long userId) { |
| User user = userService.getById(userId); |
| if (user == null) { |
| return ApiResponse.error(404, "用户不存在"); |
| } |
| user.setPassword(null); |
| user.setPasskey(null); |
| |
| int followingCount = userService.getFollowingsCount(userId); |
| int followersCount = userService.getFollowersCount(userId); |
| |
| return ApiResponse.success(Map.of( |
| "userInfo", user, |
| "statistics", Map.of( |
| "followingCount", followingCount, |
| "followersCount", followersCount |
| ) |
| )); |
| } |
| |
| // ==================== 关注功能 ==================== |
| |
| @PostMapping("/follow/{userId}") |
| public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) { |
| Long currentUserId = getCurrentUserId(); |
| boolean success = userService.followUser(currentUserId, userId); |
| return ApiResponse.success(Map.of("success", success)); |
| } |
| |
| @DeleteMapping("/follow/{userId}") |
| public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) { |
| Long currentUserId = getCurrentUserId(); |
| boolean success = userService.unfollowUser(currentUserId, userId); |
| return ApiResponse.success(Map.of("success", success)); |
| } |
| |
| @GetMapping("/followings") |
| public ApiResponse<List<User>> getFollowings() { |
| Long currentUserId = getCurrentUserId(); |
| List<User> followings = userService.getFollowings(currentUserId); |
| return ApiResponse.success(followings); |
| } |
| |
| @GetMapping("/followers") |
| public ApiResponse<List<User>> getFollowers() { |
| Long currentUserId = getCurrentUserId(); |
| List<User> followers = userService.getFollowers(currentUserId); |
| return ApiResponse.success(followers); |
| } |
| |
| // ==================== 私信功能 ==================== |
| |
| @PostMapping("/message/{receiverId}") |
| public ApiResponse<Map<String, Long>> sendMessage( |
| @PathVariable Long receiverId, |
| @RequestBody String content |
| ) { |
| Long senderId = getCurrentUserId(); |
| Long messageId = userService.sendMessage(senderId, receiverId, content); |
| return ApiResponse.success(Map.of("messageId", messageId)); |
| } |
| |
| @GetMapping("/messages/{otherUserId}") |
| public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) { |
| Long currentUserId = getCurrentUserId(); |
| List<Message> messages = userService.getMessages(currentUserId, otherUserId); |
| return ApiResponse.success(messages); |
| } |
| |
| @GetMapping("/messages/history") |
| public ApiResponse<List<Message>> getMessageHistory() { |
| Long currentUserId = getCurrentUserId(); |
| List<Message> messages = userService.getMessageHistory(currentUserId); |
| return ApiResponse.success(messages); |
| } |
| |
| // ==================== 工具方法 ==================== |
| private Long getCurrentUserId() { |
| Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| return (Long) authentication.getPrincipal(); |
| } |
| |
| @Autowired |
| private ISigningService signingService; // 修改接口名称 |
| |
| @PostMapping("/signin") |
| public ApiResponse<String> signIn() { // 修改返回类型为 ApiResponse |
| Long userId = getCurrentUserId(); |
| boolean success = signingService.signIn(userId); |
| String message = success ? "签到成功" : "今日已签到"; |
| return ApiResponse.success(message); |
| } |
| |
| // 新增获取时间段内的签到记录接口 |
| @GetMapping("/signins") |
| public ApiResponse<List<UserSignin>> getSignins( |
| @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, |
| @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) { |
| Long userId = getCurrentUserId(); |
| List<UserSignin> signins = signingService.getSigninsByDateRange(userId, startDate, endDate); |
| return ApiResponse.success(signins); |
| } |
| } |