// Package handler 处理所有 HTTP 请求,负责参数校验、调用 Service 层和返回响应 // 按业务模块分为简历管理、后台管理、AI 能力、文档处理、聊天对话等处理器 package handler import ( "encoding/json" "net/http" "github.com/gin-gonic/gin" ) // APIResponse 统一 API 响应结构体,所有接口返回格式遵循此结构 type APIResponse struct { Success bool `json:"success"` // 请求是否成功 Message string `json:"message,omitempty"`// 提示消息 Data interface{} `json:"data,omitempty"` // 响应数据 Error string `json:"error,omitempty"` // 错误信息 } // OK 返回 200 成功响应,附带数据和可选消息 // @param c Gin 上下文 // @param data 响应数据 // @param message 可选提示消息 // @author sunct func OK(c *gin.Context, data interface{}, message ...string) { msg := "" if len(message) > 0 { msg = message[0] } c.JSON(http.StatusOK, APIResponse{ Success: true, Data: data, Message: msg, }) } // Created 返回 201 创建成功响应 // @param c Gin 上下文 // @param data 创建的资源数据 // @param message 可选提示消息 // @author sunct func Created(c *gin.Context, data interface{}, message ...string) { msg := "" if len(message) > 0 { msg = message[0] } c.JSON(http.StatusCreated, APIResponse{ Success: true, Data: data, Message: msg, }) } // Success 返回 200 成功响应,仅包含提示消息 // @param c Gin 上下文 // @param message 成功提示消息 // @author sunct func Success(c *gin.Context, message string) { c.JSON(http.StatusOK, APIResponse{ Success: true, Message: message, }) } // BadRequest 返回 400 参数错误响应 // @param c Gin 上下文 // @param message 错误提示消息 // @author sunct func BadRequest(c *gin.Context, message string) { c.JSON(http.StatusBadRequest, APIResponse{ Success: false, Message: message, }) } // NotFound 返回 404 资源不存在响应 // @param c Gin 上下文 // @param message 错误提示消息 // @author sunct func NotFound(c *gin.Context, message string) { c.JSON(http.StatusNotFound, APIResponse{ Success: false, Message: message, }) } // Conflict 返回 409 资源冲突响应(如重复创建) // @param c Gin 上下文 // @param message 错误提示消息 // @author sunct func Conflict(c *gin.Context, message string) { c.JSON(http.StatusConflict, APIResponse{ Success: false, Message: message, }) } // Unauthorized 返回 401 未授权响应 // @param c Gin 上下文 // @param message 错误提示消息 // @author sunct func Unauthorized(c *gin.Context, message string) { c.JSON(http.StatusUnauthorized, APIResponse{ Success: false, Message: message, }) } // TooManyRequests 返回 429 请求过于频繁响应(限流) // @param c Gin 上下文 // @param message 错误提示消息 // @author sunct func TooManyRequests(c *gin.Context, message string) { c.JSON(http.StatusTooManyRequests, APIResponse{ Success: false, Message: message, }) } // Error 返回 500 服务器内部错误响应 // @param c Gin 上下文 // @param message 错误提示消息 // @author sunct func Error(c *gin.Context, message string) { c.JSON(http.StatusInternalServerError, APIResponse{ Success: false, Message: message, }) } // SuccessResponse 返回成功响应(支持标准库 http.ResponseWriter) // @param w http.ResponseWriter // @param data 响应数据 func SuccessResponse(w http.ResponseWriter, data interface{}) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(APIResponse{ Success: true, Data: data, }) } // ErrorResponse 返回错误响应(支持标准库 http.ResponseWriter) // @param w http.ResponseWriter // @param statusCode HTTP 状态码 // @param message 错误消息 func ErrorResponse(w http.ResponseWriter, statusCode int, message string) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(statusCode) json.NewEncoder(w).Encode(APIResponse{ Success: false, Message: message, }) }