Files
backend/internal/handler/admin_report_handler.go
lan d9aa4b46c3
All checks were successful
Build Backend / build (push) Successful in 4m55s
Build Backend / build-docker (push) Successful in 10m34s
refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion.
- Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects.
- Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management.
- Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering.
- Improve database initialization by moving it from `internal/model` to `internal/database`.
- Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool.
- Enhance error handling and security by implementing fail-fast checks for encryption key length during startup.
- Clean up unused code, including the `avatar` package and several unused DTOs.
2026-06-15 03:41:59 +08:00

140 lines
3.3 KiB
Go

package handler
import (
"with_you/internal/dto"
"with_you/internal/pkg/response"
"with_you/internal/query"
"with_you/internal/service"
"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 query.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, 500, 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, 500, err.Error())
return
}
response.Success(c, result)
}