The Go module name has been changed from `carrot_bbs` to `with_you`, which requires all import paths to be updated throughout the codebase and by external consumers. BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports and dependencies must be updated from carrot_bbs/* to with_you/*.
138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"with_you/internal/dto"
|
|
"with_you/internal/model"
|
|
"with_you/internal/pkg/response"
|
|
"with_you/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type VerificationHandler struct {
|
|
verificationService service.VerificationService
|
|
}
|
|
|
|
func NewVerificationHandler(verificationService service.VerificationService) *VerificationHandler {
|
|
return &VerificationHandler{
|
|
verificationService: verificationService,
|
|
}
|
|
}
|
|
|
|
func (h *VerificationHandler) SubmitVerification(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c, "未授权")
|
|
return
|
|
}
|
|
|
|
var req dto.SubmitVerificationRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "请求参数错误: "+err.Error())
|
|
return
|
|
}
|
|
|
|
record, err := h.verificationService.SubmitVerification(
|
|
c.Request.Context(),
|
|
userID.(string),
|
|
model.UserIdentity(req.Identity),
|
|
req.RealName,
|
|
req.StudentID,
|
|
req.EmployeeID,
|
|
req.Department,
|
|
req.ProofMaterials,
|
|
)
|
|
if err != nil {
|
|
response.HandleError(c, err, "提交认证申请失败")
|
|
return
|
|
}
|
|
|
|
response.Success(c, convertVerificationRecordToResponse(record))
|
|
}
|
|
|
|
func (h *VerificationHandler) GetVerificationStatus(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c, "未授权")
|
|
return
|
|
}
|
|
|
|
status, err := h.verificationService.GetVerificationStatus(c.Request.Context(), userID.(string))
|
|
if err != nil {
|
|
response.HandleError(c, err, "获取认证状态失败")
|
|
return
|
|
}
|
|
|
|
response.Success(c, &dto.VerificationStatusResponse{
|
|
Identity: status.Identity,
|
|
VerificationStatus: status.VerificationStatus,
|
|
HasPendingRequest: status.HasPendingRequest,
|
|
LatestRecord: convertVerificationRecordToResponse(status.LatestRecord),
|
|
})
|
|
}
|
|
|
|
func (h *VerificationHandler) GetVerificationRecords(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c, "未授权")
|
|
return
|
|
}
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
|
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if pageSize <= 0 || pageSize > 50 {
|
|
pageSize = 10
|
|
}
|
|
|
|
records, total, err := h.verificationService.GetVerificationRecords(c.Request.Context(), userID.(string), page, pageSize)
|
|
if err != nil {
|
|
response.HandleError(c, err, "获取认证记录失败")
|
|
return
|
|
}
|
|
|
|
responses := make([]*dto.VerificationRecordResponse, len(records))
|
|
for i, record := range records {
|
|
responses[i] = convertVerificationRecordToResponse(record)
|
|
}
|
|
|
|
response.Paginated(c, responses, total, page, pageSize)
|
|
}
|
|
|
|
func convertVerificationRecordToResponse(record *model.VerificationRecord) *dto.VerificationRecordResponse {
|
|
if record == nil {
|
|
return nil
|
|
}
|
|
|
|
resp := &dto.VerificationRecordResponse{
|
|
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),
|
|
UpdatedAt: dto.FormatTime(record.UpdatedAt),
|
|
}
|
|
|
|
if record.ReviewedAt != nil {
|
|
resp.ReviewedAt = dto.FormatTime(*record.ReviewedAt)
|
|
}
|
|
if record.ReviewedBy != nil {
|
|
resp.ReviewedBy = *record.ReviewedBy
|
|
}
|
|
if record.RejectReason != "" {
|
|
resp.RejectReason = record.RejectReason
|
|
}
|
|
|
|
return resp
|
|
}
|