夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 3 | import com.example.g8backend.dto.ApiResponse; |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 4 | import com.example.g8backend.entity.Post; |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 5 | import com.example.g8backend.entity.Report; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 6 | import com.example.g8backend.service.AdminService; |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 7 | import com.example.g8backend.service.IPostService; |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 8 | import com.example.g8backend.service.IReportService; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 9 | import org.springframework.beans.factory.annotation.Autowired; |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 10 | import org.springframework.http.ResponseEntity; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 11 | import org.springframework.security.access.prepost.PreAuthorize; |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 12 | import org.springframework.security.core.context.SecurityContextHolder; |
| 13 | import org.springframework.web.bind.annotation.*; |
| 14 | |
| 15 | import java.util.List; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 16 | |
| 17 | @RestController |
| 18 | @RequestMapping("/admin") |
| 19 | public class AdminController { |
| 20 | @Autowired |
| 21 | private AdminService adminService; |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 22 | private IReportService reportService; |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 23 | @Autowired |
| 24 | private IPostService postService; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 25 | @PostMapping("/grant-vip/{userId}") |
| 26 | @PreAuthorize("hasRole('ADMIN')") // 仅允许管理员访问 |
| 27 | public String grantVip(@PathVariable Long userId) { |
| 28 | boolean success = adminService.grantVip(userId); |
| 29 | return success ? "VIP授予成功" : "操作失败(用户不存在)"; |
| 30 | } |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 31 | // 获取举报记录(支持按状态过滤) |
| 32 | @GetMapping("/reports") |
| 33 | @PreAuthorize("hasRole('ADMIN')") |
| 34 | public ApiResponse<List<Report>> getReports( |
| 35 | @RequestParam(required = false) String status) { |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 36 | Long adminId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 37 | return ApiResponse.success(reportService.getReports(status, adminId)); |
| 38 | } |
| 39 | // 处理举报 |
| 40 | @PutMapping("/reports/{reportId}") |
| 41 | @PreAuthorize("hasRole('ADMIN')") |
| 42 | public ApiResponse<String> resolveReport( |
| 43 | @PathVariable Long reportId, |
| 44 | @RequestParam String status, |
| 45 | @RequestParam(required = false) String notes) { |
夜雨声烦 | 45c25dd | 2025-05-20 11:59:03 +0800 | [diff] [blame] | 46 | Long adminId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();//这里之前不太对,service改了 |
夜雨声烦 | 363e536 | 2025-05-20 02:01:13 +0800 | [diff] [blame] | 47 | reportService.resolveReport(reportId, adminId, status, notes); |
夜雨声烦 | 70f4c65 | 2025-05-20 01:52:41 +0800 | [diff] [blame] | 48 | return ApiResponse.success("举报处理完成"); |
| 49 | } |
| 50 | |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 51 | |
| 52 | // 封禁用户 |
| 53 | @PostMapping("/users/{userId}/ban") |
| 54 | @PreAuthorize("hasRole('ADMIN')") |
| 55 | public ApiResponse<String> banUser( |
| 56 | @PathVariable Long userId, |
| 57 | @RequestParam String reason) { |
| 58 | Long adminId = getCurrentAdminId(); |
| 59 | boolean success = adminService.banUser(userId, reason, adminId); |
| 60 | return success ? |
| 61 | ApiResponse.success("用户封禁成功") : |
| 62 | ApiResponse.error(400, "操作失败"); |
| 63 | } |
| 64 | |
| 65 | // 解封用户 |
| 66 | @PostMapping("/users/{userId}/unban") |
| 67 | @PreAuthorize("hasRole('ADMIN')") |
| 68 | public ApiResponse<String> unbanUser(@PathVariable Long userId) { |
| 69 | Long adminId = getCurrentAdminId(); |
| 70 | boolean success = adminService.unbanUser(userId, adminId); |
| 71 | return success ? |
| 72 | ApiResponse.success("用户解封成功") : |
| 73 | ApiResponse.error(400, "操作失败"); |
| 74 | } |
| 75 | |
| 76 | // 锁定帖子 |
| 77 | @PostMapping("/posts/{postId}/lock") |
| 78 | @PreAuthorize("hasRole('ADMIN')") |
| 79 | public ApiResponse<String> lockPost( |
| 80 | @PathVariable Long postId, |
| 81 | @RequestParam String reason) { |
| 82 | Long adminId = getCurrentAdminId(); |
| 83 | boolean success = adminService.lockPost(postId, reason, adminId); |
| 84 | return success ? |
| 85 | ApiResponse.success("帖子已锁定") : |
| 86 | ApiResponse.error(400, "操作失败"); |
| 87 | } |
| 88 | |
| 89 | // 解锁帖子 |
| 90 | @PostMapping("/posts/{postId}/unlock") |
| 91 | @PreAuthorize("hasRole('ADMIN')") |
| 92 | public ApiResponse<String> unlockPost(@PathVariable Long postId) { |
| 93 | Long adminId = getCurrentAdminId(); |
| 94 | boolean success = adminService.unlockPost(postId, adminId); |
| 95 | return success ? |
| 96 | ApiResponse.success("帖子已解锁") : |
| 97 | ApiResponse.error(400, "操作失败"); |
| 98 | } |
| 99 | @DeleteMapping("/{postId}") |
| 100 | public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) { |
| 101 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 102 | Post post = postService.getById(postId); |
| 103 | if (post == null) { |
| 104 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
| 105 | } |
| 106 | postService.removeById(postId); |
| 107 | return ResponseEntity.ok(ApiResponse.message("Post deleted successfully.")); |
| 108 | } |
| 109 | |
| 110 | private Long getCurrentAdminId() { |
| 111 | return (Long) SecurityContextHolder.getContext() |
| 112 | .getAuthentication().getPrincipal(); |
| 113 | } |
| 114 | |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 115 | } |