2025-11-28 23:30:49 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
2025-12-02 17:40:39 +08:00
|
|
|
|
import "os"
|
|
|
|
|
|
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// Response 通用API响应结构
|
|
|
|
|
|
type Response struct {
|
2025-12-02 17:40:39 +08:00
|
|
|
|
Code int `json:"code"` // 业务状态码
|
|
|
|
|
|
Message string `json:"message"` // 响应消息
|
|
|
|
|
|
Data interface{} `json:"data,omitempty"` // 响应数据
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// PaginationResponse 分页响应结构
|
|
|
|
|
|
type PaginationResponse struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
Data interface{} `json:"data"`
|
2025-12-02 17:40:39 +08:00
|
|
|
|
Total int64 `json:"total"` // 总记录数
|
|
|
|
|
|
Page int `json:"page"` // 当前页码
|
|
|
|
|
|
PerPage int `json:"per_page"` // 每页数量
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ErrorResponse 错误响应
|
|
|
|
|
|
type ErrorResponse struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
Error string `json:"error,omitempty"` // 详细错误信息(仅开发环境)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 常用状态码
|
|
|
|
|
|
const (
|
2025-12-02 17:40:39 +08:00
|
|
|
|
CodeSuccess = 200 // 成功
|
|
|
|
|
|
CodeCreated = 201 // 创建成功
|
|
|
|
|
|
CodeBadRequest = 400 // 请求参数错误
|
|
|
|
|
|
CodeUnauthorized = 401 // 未授权
|
|
|
|
|
|
CodeForbidden = 403 // 禁止访问
|
|
|
|
|
|
CodeNotFound = 404 // 资源不存在
|
|
|
|
|
|
CodeConflict = 409 // 资源冲突
|
|
|
|
|
|
CodeServerError = 500 // 服务器错误
|
2025-11-28 23:30:49 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 常用响应消息
|
|
|
|
|
|
const (
|
|
|
|
|
|
MsgSuccess = "操作成功"
|
|
|
|
|
|
MsgCreated = "创建成功"
|
|
|
|
|
|
MsgBadRequest = "请求参数错误"
|
|
|
|
|
|
MsgUnauthorized = "未授权,请先登录"
|
|
|
|
|
|
MsgForbidden = "权限不足"
|
|
|
|
|
|
MsgNotFound = "资源不存在"
|
|
|
|
|
|
MsgConflict = "资源已存在"
|
|
|
|
|
|
MsgServerError = "服务器内部错误"
|
|
|
|
|
|
MsgInvalidToken = "无效的令牌"
|
|
|
|
|
|
MsgTokenExpired = "令牌已过期"
|
|
|
|
|
|
MsgInvalidCredentials = "用户名或密码错误"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// NewSuccessResponse 创建成功响应
|
|
|
|
|
|
func NewSuccessResponse(data interface{}) *Response {
|
|
|
|
|
|
return &Response{
|
|
|
|
|
|
Code: CodeSuccess,
|
|
|
|
|
|
Message: MsgSuccess,
|
|
|
|
|
|
Data: data,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewErrorResponse 创建错误响应
|
2025-12-02 17:40:39 +08:00
|
|
|
|
// 注意:err参数仅在开发环境下显示,生产环境不应暴露详细错误信息
|
2025-11-28 23:30:49 +08:00
|
|
|
|
func NewErrorResponse(code int, message string, err error) *ErrorResponse {
|
|
|
|
|
|
resp := &ErrorResponse{
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
Message: message,
|
|
|
|
|
|
}
|
2025-12-02 17:40:39 +08:00
|
|
|
|
// 仅在非生产环境下返回详细错误信息
|
|
|
|
|
|
// 可以通过环境变量 ENVIRONMENT 控制
|
|
|
|
|
|
if err != nil && !isProductionEnvironment() {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
resp.Error = err.Error()
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 17:40:39 +08:00
|
|
|
|
// isProductionEnvironment 检查是否为生产环境
|
|
|
|
|
|
func isProductionEnvironment() bool {
|
|
|
|
|
|
env := os.Getenv("ENVIRONMENT")
|
|
|
|
|
|
return env == "production" || env == "prod"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// NewPaginationResponse 创建分页响应
|
|
|
|
|
|
func NewPaginationResponse(data interface{}, total int64, page, perPage int) *PaginationResponse {
|
|
|
|
|
|
return &PaginationResponse{
|
|
|
|
|
|
Code: CodeSuccess,
|
|
|
|
|
|
Message: MsgSuccess,
|
|
|
|
|
|
Data: data,
|
|
|
|
|
|
Total: total,
|
|
|
|
|
|
Page: page,
|
|
|
|
|
|
PerPage: perPage,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|