Files
backend/internal/handler/admin_verification_handler.go
lafay 67a5660952
All checks were successful
Build Backend / build (push) Successful in 3m54s
Build Backend / build-docker (push) Successful in 1m9s
refactor: unify code formatting and improve push/search implementations
- Remove BOM from all Go source files
- Standardize import ordering and whitespace across handlers and services
- Replace PostgreSQL full-text search with ILIKE pattern matching in post and user repositories
- Enhance JPush client with TLS transport, rate limit monitoring, and simplified API
- Refactor PushService to include userID in device management methods
- Add MaxDevicesPerUser limit and extract helper functions for push payload construction
2026-04-28 14:53:04 +08:00

188 lines
4.9 KiB
Go

package handler
import (
"strconv"
"with_you/internal/dto"
"with_you/internal/model"
"with_you/internal/pkg/response"
"with_you/internal/repository"
"with_you/internal/service"
"github.com/gin-gonic/gin"
)
type AdminVerificationHandler struct {
adminVerificationService service.AdminVerificationService
userRepo repository.UserRepository
}
func NewAdminVerificationHandler(
adminVerificationService service.AdminVerificationService,
userRepo repository.UserRepository,
) *AdminVerificationHandler {
return &AdminVerificationHandler{
adminVerificationService: adminVerificationService,
userRepo: userRepo,
}
}
func (h *AdminVerificationHandler) ListVerifications(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
status := c.Query("status")
keyword := c.Query("keyword")
if page <= 0 {
page = 1
}
if pageSize <= 0 || pageSize > 100 {
pageSize = 20
}
if status != "" && status != string(model.VerificationStatusPending) &&
status != string(model.VerificationStatusApproved) &&
status != string(model.VerificationStatusRejected) {
response.BadRequest(c, "无效的状态值")
return
}
records, total, err := h.adminVerificationService.ListVerifications(c.Request.Context(), page, pageSize, status, keyword)
if err != nil {
response.InternalServerError(c, "获取认证列表失败")
return
}
responses := make([]*dto.AdminVerificationListResponse, len(records))
for i, record := range records {
responses[i] = h.convertToAdminListResponse(record)
}
response.Paginated(c, responses, total, page, pageSize)
}
func (h *AdminVerificationHandler) GetVerificationDetail(c *gin.Context) {
id := c.Param("id")
if id == "" {
response.BadRequest(c, "认证ID不能为空")
return
}
record, err := h.adminVerificationService.GetVerificationDetail(c.Request.Context(), id)
if err != nil {
response.NotFound(c, "认证记录不存在")
return
}
response.Success(c, h.convertToAdminDetailResponse(record))
}
func (h *AdminVerificationHandler) ReviewVerification(c *gin.Context) {
reviewerID, exists := c.Get("user_id")
if !exists {
response.Unauthorized(c, "未授权")
return
}
id := c.Param("id")
if id == "" {
response.BadRequest(c, "认证ID不能为空")
return
}
var req dto.AdminReviewVerificationRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "请求参数错误: "+err.Error())
return
}
if !req.Approve && req.RejectReason == "" {
response.BadRequest(c, "拒绝时必须填写拒绝原因")
return
}
record, err := h.adminVerificationService.ReviewVerification(
c.Request.Context(),
reviewerID.(string),
id,
req.Approve,
req.RejectReason,
)
if err != nil {
response.HandleError(c, err, "审核失败")
return
}
response.Success(c, h.convertToAdminDetailResponse(record))
}
func (h *AdminVerificationHandler) convertToAdminListResponse(record *model.VerificationRecord) *dto.AdminVerificationListResponse {
resp := &dto.AdminVerificationListResponse{
ID: record.ID,
UserID: record.UserID,
Identity: string(record.Identity),
Status: string(record.Status),
RealName: record.RealName,
StudentID: record.StudentID,
EmployeeID: record.EmployeeID,
Department: record.Department,
CreatedAt: dto.FormatTime(record.CreatedAt),
}
user, err := h.userRepo.GetByID(record.UserID)
if err == nil && user != nil {
resp.Username = user.Username
resp.Nickname = user.Nickname
resp.Avatar = user.Avatar
}
if record.ReviewedAt != nil {
resp.ReviewedAt = dto.FormatTime(*record.ReviewedAt)
if record.ReviewedBy != nil {
reviewer, err := h.userRepo.GetByID(*record.ReviewedBy)
if err == nil && reviewer != nil {
resp.ReviewerName = reviewer.Nickname
}
}
}
return resp
}
func (h *AdminVerificationHandler) convertToAdminDetailResponse(record *model.VerificationRecord) *dto.AdminVerificationDetailResponse {
resp := &dto.AdminVerificationDetailResponse{
ID: record.ID,
UserID: record.UserID,
Identity: string(record.Identity),
Status: string(record.Status),
RealName: record.RealName,
StudentID: record.StudentID,
EmployeeID: record.EmployeeID,
Department: record.Department,
ProofMaterials: record.ProofMaterials,
CreatedAt: dto.FormatTime(record.CreatedAt),
}
user, err := h.userRepo.GetByID(record.UserID)
if err == nil && user != nil {
resp.Username = user.Username
resp.Nickname = user.Nickname
resp.Avatar = user.Avatar
}
if record.ReviewedAt != nil {
resp.ReviewedAt = dto.FormatTime(*record.ReviewedAt)
}
if record.ReviewedBy != nil {
resp.ReviewedBy = *record.ReviewedBy
if reviewer, err := h.userRepo.GetByID(*record.ReviewedBy); err == nil && reviewer != nil {
resp.ReviewerName = reviewer.Nickname
}
}
if record.RejectReason != "" {
resp.RejectReason = record.RejectReason
}
return resp
}