ginhelper

package
v0.0.0-...-629b230 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2021 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Failure

func Failure(c *gin.Context, options ...Option)

Failure 异常响应,status 默认为 1,message 默认为 "未知错误"

func FailureAbort

func FailureAbort(c *gin.Context, options ...Option)

FailureAbort 异常响应,status 默认为 1,message 默认为 "未知错误" 关于 gin.Context.AbortWithStatusJSON,请参考 https://pkg.go.dev/github.com/gin-gonic/gin#Context.AbortWithStatusJSON

func Resp

func Resp(status int8, message string, data interface{}) gin.H

Resp 返回约定格式的 map[string]interface{} (gin.H) status 当前接口的响应状态,每种情况的 status 值应该互斥 message 响应描述信息,尽量不要为空字符串 data 如果没有数据则传 nil

func Success

func Success(c *gin.Context, options ...Option)

Success 正常响应,status 默认为 0,message 默认为 "成功"

func SuccessAbort

func SuccessAbort(c *gin.Context, options ...Option)

SuccessAbort 正常响应,status 默认为 0,message 默认为 "成功" 关于 gin.Context.AbortWithStatusJSON,请参考 https://pkg.go.dev/github.com/gin-gonic/gin#Context.AbortWithStatusJSON

Types

type APIError

type APIError interface {
	Code() int
	Status() int
	Message() string
}

type Option

type Option func(c *gin.Context, body gin.H)

Option 在自定义统一响应附加可选参数的函数类型

func Code

func Code(code int) Option

Code 指定响应 HTTP 状态码

func Data

func Data(data interface{}) Option

Data 指定 Response Body 中的 Data

func Error

func Error(e APIError) Option

func ErrorMessage

func ErrorMessage(e error) Option

func Message

func Message(message string) Option

Message 指定 Response Body 中的 Message

func Status

func Status(status int) Option

Status 指定 Response Body 中的 status

type Response

type Response struct {
	Status  int8        `json:"status"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

Response 仅用作生成 API 文档

Jump to

Keyboard shortcuts

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