feat: 添加举报功能支持
- 新增 Report 数据模型、DTO、Repository、Service 层 - 实现用户端举报 API (POST /api/v1/reports) - 实现管理端举报管理 API (列表、详情、处理、批量处理) - 添加举报自动隐藏机制(达到阈值自动隐藏内容) - 集成系统通知(举报处理结果通知) - 更新路由和 Wire 依赖注入配置 Made-with: Cursor
This commit is contained in:
152
internal/handler/admin_report_handler.go
Normal file
152
internal/handler/admin_report_handler.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrot_bbs/internal/dto"
|
||||
"carrot_bbs/internal/response"
|
||||
"carrot_bbs/internal/service"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// AdminReportHandler 管理端举报处理器
|
||||
type AdminReportHandler struct {
|
||||
adminReportService service.AdminReportService
|
||||
}
|
||||
|
||||
// NewAdminReportHandler 创建管理端举报处理器
|
||||
func NewAdminReportHandler(adminReportService service.AdminReportService) *AdminReportHandler {
|
||||
return &AdminReportHandler{
|
||||
adminReportService: adminReportService,
|
||||
}
|
||||
}
|
||||
|
||||
// List 获取举报列表
|
||||
func (h *AdminReportHandler) List(c *gin.Context) {
|
||||
// 解析查询参数
|
||||
var query dto.AdminReportListQuery
|
||||
if err := c.ShouldBindQuery(&query); err != nil {
|
||||
response.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 设置默认分页
|
||||
if query.Page == 0 {
|
||||
query.Page = 1
|
||||
}
|
||||
if query.PageSize == 0 {
|
||||
query.PageSize = 20
|
||||
}
|
||||
|
||||
// 查询列表
|
||||
reports, total, err := h.adminReportService.GetReportList(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to get report list", zap.Error(err))
|
||||
response.InternalServerError(c, "获取举报列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.Paginated(c, reports, total, query.Page, query.PageSize)
|
||||
}
|
||||
|
||||
// Detail 获取举报详情
|
||||
func (h *AdminReportHandler) Detail(c *gin.Context) {
|
||||
// 获取举报ID
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
response.BadRequest(c, "举报ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 查询详情
|
||||
report, err := h.adminReportService.GetReportDetail(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to get report detail", zap.Error(err), zap.String("id", id))
|
||||
response.NotFound(c, "举报不存在")
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, report)
|
||||
}
|
||||
|
||||
// Handle 处理举报
|
||||
func (h *AdminReportHandler) Handle(c *gin.Context) {
|
||||
// 获取处理人ID
|
||||
handledBy := c.GetString("user_id")
|
||||
if handledBy == "" {
|
||||
response.Unauthorized(c, "未授权")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取举报ID
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
response.BadRequest(c, "举报ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析请求
|
||||
var req dto.HandleReportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 处理举报
|
||||
err := h.adminReportService.HandleReport(c.Request.Context(), id, req.Action, req.Result, handledBy)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to handle report",
|
||||
zap.Error(err),
|
||||
zap.String("id", id),
|
||||
zap.String("action", req.Action),
|
||||
)
|
||||
response.Error(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{"message": "处理成功"})
|
||||
}
|
||||
|
||||
// BatchHandle 批量处理举报
|
||||
func (h *AdminReportHandler) BatchHandle(c *gin.Context) {
|
||||
// 获取处理人ID
|
||||
handledBy := c.GetString("user_id")
|
||||
if handledBy == "" {
|
||||
response.Unauthorized(c, "未授权")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析请求
|
||||
var req dto.BatchHandleReportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 批量处理
|
||||
result, err := h.adminReportService.BatchHandle(c.Request.Context(), req.IDs, req.Action, req.Result, handledBy)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to batch handle reports",
|
||||
zap.Error(err),
|
||||
zap.Int("count", len(req.IDs)),
|
||||
)
|
||||
response.Error(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, result)
|
||||
}
|
||||
|
||||
// parsePageParams 解析分页参数
|
||||
func parsePageParams(c *gin.Context) (int, int) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
return page, pageSize
|
||||
}
|
||||
61
internal/handler/report_handler.go
Normal file
61
internal/handler/report_handler.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrot_bbs/internal/dto"
|
||||
"carrot_bbs/internal/response"
|
||||
"carrot_bbs/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ReportHandler 举报处理器
|
||||
type ReportHandler struct {
|
||||
reportService service.ReportService
|
||||
}
|
||||
|
||||
// NewReportHandler 创建举报处理器
|
||||
func NewReportHandler(reportService service.ReportService) *ReportHandler {
|
||||
return &ReportHandler{
|
||||
reportService: reportService,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建举报
|
||||
func (h *ReportHandler) Create(c *gin.Context) {
|
||||
// 获取用户ID
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
response.Unauthorized(c, "未授权")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析请求
|
||||
var req dto.CreateReportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 创建举报
|
||||
report, err := h.reportService.CreateReport(
|
||||
c.Request.Context(),
|
||||
userID,
|
||||
req.TargetType,
|
||||
req.TargetID,
|
||||
req.Reason,
|
||||
req.Description,
|
||||
)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to create report",
|
||||
zap.Error(err),
|
||||
zap.String("user_id", userID),
|
||||
zap.String("target_type", req.TargetType),
|
||||
zap.String("target_id", req.TargetID),
|
||||
)
|
||||
response.Error(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, dto.ConvertReportToResponse(report))
|
||||
}
|
||||
Reference in New Issue
Block a user