2026-03-29 20:18:36 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"carrot_bbs/internal/dto"
|
2026-03-30 02:11:12 +08:00
|
|
|
"carrot_bbs/internal/pkg/response"
|
2026-03-29 20:18:36 +08:00
|
|
|
"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
|
|
|
|
|
}
|