blob: 64a0f014c39caf21cf50e902731228695ccc3d12 [file] [log] [blame]
夜雨声烦451d71c2025-05-20 00:58:36 +08001package com.example.g8backend.controller;
2
夜雨声烦70f4c652025-05-20 01:52:41 +08003import com.example.g8backend.dto.ApiResponse;
4import com.example.g8backend.entity.Report;
夜雨声烦451d71c2025-05-20 00:58:36 +08005import com.example.g8backend.service.AdminService;
夜雨声烦70f4c652025-05-20 01:52:41 +08006import com.example.g8backend.service.IReportService;
夜雨声烦451d71c2025-05-20 00:58:36 +08007import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.security.access.prepost.PreAuthorize;
夜雨声烦70f4c652025-05-20 01:52:41 +08009import org.springframework.security.core.context.SecurityContextHolder;
10import org.springframework.web.bind.annotation.*;
11
12import java.util.List;
夜雨声烦451d71c2025-05-20 00:58:36 +080013
14@RestController
15@RequestMapping("/admin")
16public class AdminController {
17 @Autowired
18 private AdminService adminService;
夜雨声烦70f4c652025-05-20 01:52:41 +080019 private IReportService reportService;
夜雨声烦451d71c2025-05-20 00:58:36 +080020 @PostMapping("/grant-vip/{userId}")
21 @PreAuthorize("hasRole('ADMIN')") // 仅允许管理员访问
22 public String grantVip(@PathVariable Long userId) {
23 boolean success = adminService.grantVip(userId);
24 return success ? "VIP授予成功" : "操作失败(用户不存在)";
25 }
夜雨声烦70f4c652025-05-20 01:52:41 +080026 // 获取举报记录(支持按状态过滤)
27 @GetMapping("/reports")
28 @PreAuthorize("hasRole('ADMIN')")
29 public ApiResponse<List<Report>> getReports(
30 @RequestParam(required = false) String status) {
31 // 从安全上下文自动获取管理员ID
32 Long adminId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
33 return ApiResponse.success(reportService.getReports(status, adminId));
34 }
35 // 处理举报
36 @PutMapping("/reports/{reportId}")
37 @PreAuthorize("hasRole('ADMIN')")
38 public ApiResponse<String> resolveReport(
39 @PathVariable Long reportId,
40 @RequestParam String status,
41 @RequestParam(required = false) String notes) {
42 reportService.resolveReport(reportId, null, status, notes); // adminId在服务层自动获取
43 return ApiResponse.success("举报处理完成");
44 }
45
夜雨声烦451d71c2025-05-20 00:58:36 +080046}