response

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package response 提供HTTP响应数据的数据结构定义 本文件定义了统一的响应码常量和响应消息,用于标准化API响应格式

Package response 提供HTTP响应数据的数据结构定义 本文件定义了分页查询结果的响应结构,用于返回分页数据和分页信息

Package response 提供HTTP响应数据的数据结构定义 本文件定义了统一的HTTP响应结构和便捷的响应方法,用于标准化API响应格式

Index

Constants

This section is empty.

Variables

View Source
var (
	// 特殊响应码
	ResponseNull = responseCode{/* contains filtered or unexported fields */} // 空回复,该回复不写入到responseBody中,一般用于文件下载等特殊场景

	// 成功响应码
	ResponseSuccess = responseCode{/* contains filtered or unexported fields */} // 标准成功响应

	// 认证相关响应码(41xxx系列)
	ResponseLoginNotLogin  = responseCode{/* contains filtered or unexported fields */} // 用户未登录状态
	ResponseLoginButUnAuth = responseCode{/* contains filtered or unexported fields */} // 未通过双因子认证
	ResponseLoginInvalid   = responseCode{/* contains filtered or unexported fields */} // 登录会话已过期
	ResponseAuthFailed     = responseCode{/* contains filtered or unexported fields */} // 权限不足,拒绝访问

	// 业务逻辑响应码(50xxx系列)
	ResponseFail           = responseCode{/* contains filtered or unexported fields */} // 通用操作失败
	ResponseParamInvalid   = responseCode{/* contains filtered or unexported fields */} // 请求参数验证失败
	ResponseParamTypeError = responseCode{/* contains filtered or unexported fields */} // 请求参数类型不匹配

	// 系统异常响应码(90xxx系列)
	ResponseExceptionCommon  = responseCode{/* contains filtered or unexported fields */} // 通用服务端异常
	ResponseExceptionRpc     = responseCode{/* contains filtered or unexported fields */} // RPC服务调用异常
	ResponseExceptionUnknown = responseCode{/* contains filtered or unexported fields */} // 未分类的系统异常
)

预定义的响应码常量,按照功能模块和错误类型进行分类

Functions

func Fail

func Fail(c *gin.Context)

Fail 返回失败响应(无数据) 该方法返回标准的失败响应,使用预定义的失败状态码和消息 参数:

  • c: Gin上下文,用于HTTP响应

func FailWithDetail

func FailWithDetail(c *gin.Context, message string, data any)

FailWithDetail 返回失败响应(自定义消息和数据) 该方法返回失败响应,支持自定义消息和错误详情数据 参数:

  • c: Gin.Context,用于HTTP响应
  • message: 自定义失败消息
  • data: 错误详情数据

func FailWithMessage

func FailWithMessage(c *gin.Context, message string)

FailWithMessage 返回失败响应(自定义消息) 该方法返回失败响应,支持自定义失败消息 参数:

  • c: Gin上下文,用于HTTP响应
  • message: 自定义失败消息

func NoAuth

func NoAuth(c *gin.Context, message string)

NoAuth 返回未授权响应 该方法返回HTTP 401状态码的未授权响应,用于认证失败场景 参数:

  • c: Gin上下文,用于HTTP响应
  • message: 未授权原因说明

func Ok

func Ok(c *gin.Context)

Ok 返回成功响应(无数据) 该方法返回标准的成功响应,使用预定义的成功状态码和消息 参数:

  • c: Gin上下文,用于HTTP响应

func OkWithData

func OkWithData(c *gin.Context, data any)

OkWithData 返回成功响应(带数据) 该方法返回成功响应,包含业务数据 参数:

  • c: Gin上下文,用于HTTP响应
  • data: 业务数据

func OkWithDetail

func OkWithDetail(c *gin.Context, message string, data any)

OkWithDetail 返回成功响应(自定义消息和数据) 该方法返回成功响应,支持自定义消息和业务数据 参数:

  • c: Gin上下文,用于HTTP响应
  • message: 自定义成功消息
  • data: 业务数据

func OkWithMessage

func OkWithMessage(c *gin.Context, message string)

OkWithMessage 返回成功响应(自定义消息) 该方法返回成功响应,支持自定义成功消息 参数:

  • c: Gin上下文,用于HTTP响应
  • message: 自定义成功消息

func Result

func Result(c *gin.Context, code int, data any, msg string)

Result 通用响应方法 该方法用于构建和返回标准的HTTP响应,支持自定义状态码、数据和消息 参数:

  • c: Gin上下文,用于HTTP响应
  • code: 响应状态码
  • data: 响应数据
  • msg: 响应消息

Types

type PageResult

type PageResult struct {
	List      any   `json:"list"`      // 数据列表,包含当前页的数据内容,支持任意类型
	Total     int64 `json:"total"`     // 数据总数,表示符合查询条件的所有数据条数
	PageIndex int   `json:"pageIndex"` // 当前页码,表示当前返回的是第几页的数据
	PageSize  int   `json:"pageSize"`  // 每页大小,表示每页包含的数据条数
}

PageResult 分页查询结果响应结构 该结构体定义了分页查询的标准响应格式,包含数据列表、总数和分页信息

type Response

type Response struct {
	Code int    `json:"code"` // 响应状态码,用于标识请求处理结果
	Data any    `json:"data"` // 响应数据,包含业务数据或错误详情,支持任意类型
	Msg  string `json:"msg"`  // 响应消息,用于描述响应状态或提供用户提示
}

Response 统一HTTP响应结构 该结构体定义了所有API响应的标准格式,包含状态码、数据和消息三个字段

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL