ginHelper

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 14 Imported by: 0

README

gin 相关的方法,接口

统一接口输出
// ResponseJson 统一接口输出
type ResponseJson struct {
	Code      int64       `json:"code"`
	Msg       string      `json:"msg"`
	Date      interface{} `json:"data"`
	TimeStamp int64       `json:"timeStamp"`
}

统一接口输出方法

func APIOutPut(c *gin.Context, code int64, msg string, data interface{})


统一接口输出错误方法

func APIOutPutError(c *gin.Context, code int64, msg string)


获取参数

func GetPostArgs(c *gin.Context, obj interface{}) error


Documentation

Index

Constants

View Source
const (
	ReqIP  = "reqIp"
	Lang   = "lang"   // cn: 中文
	Source = "source" // 来源,web:1, h5:2, android:3, ios:4
)
View Source
const (
	SourceWeb     = "1"
	SourceH5      = "2"
	SourceAndroid = "3"
	SourceIos     = "4"
)

Variables

View Source
var (
	CsrfName    = "cToken"
	CsrfAuthKey = "123456789"
)
View Source
var Router *gin.Engine

Functions

func APIOutPut

func APIOutPut(c *gin.Context, msg string, data interface{})

APIOutPut 统一接口输出方法

func APIOutPutError

func APIOutPutError(c *gin.Context, msg string)

APIOutPutError 统一接口输出错误方法

func AuthErrorOut

func AuthErrorOut(c *gin.Context)

AuthErrorOut 鉴权失败

func CSRFMiddleware

func CSRFMiddleware() gin.HandlerFunc

CSRFMiddleware use: ginHelper.CsrfName = "CsrfName" ginHelper.CsrfAuthKey = "CsrfAuthKey" Router.Use(ginHelper.CSRFMiddleware())

func CSRFTokenMiddleware

func CSRFTokenMiddleware() gin.HandlerFunc

func FormSetCSRF

func FormSetCSRF(r *http.Request) template.HTML

FormSetCSRF use:

c.HTML(http.StatusOK, "login.html", gin.H{
	"csrf": ginHelper.FormSetCSRF(c.Request),
})

func GetPostArgs

func GetPostArgs(c *gin.Context, obj interface{}) error

GetPostArgs 获取参数

func Handle

func Handle(h HandlerFunc) gin.HandlerFunc

func NewGinServer

func NewGinServer()

func OutHtml

func OutHtml()

func OutPut added in v0.1.4

func OutPut(c *gin.Context, msg string, data interface{})

OutPut 统一接口输出方法

func OutPutError added in v0.1.4

func OutPutError(c *gin.Context, msg string)

OutPutError 统一接口输出错误方法

func ReqGrpcParameter added in v0.1.4

func ReqGrpcParameter(ctx *gin.Context, obj any) error

func RespWriteGrpcFail added in v0.1.4

func RespWriteGrpcFail(ctx *gin.Context, code GrpcErrorCode, err error)

func RespWriteGrpcOK added in v0.1.4

func RespWriteGrpcOK(ctx *gin.Context, data interface{})

func SetCSRFTokenMiddleware

func SetCSRFTokenMiddleware(key string) gin.HandlerFunc

func TokenInvalidOut added in v0.1.4

func TokenInvalidOut(c *gin.Context)

TokenInvalidOut 鉴权失败

Types

type CommonResp added in v0.1.4

type CommonResp struct {
	Code  int32  `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`  // 错误代码
	Data  []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`   // 成功响应数据
	Msg   string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`     // 消息
	Stack string `protobuf:"bytes,4,opt,name=stack,proto3" json:"stack,omitempty"` // 服务器内部错误时的调用栈(用于开发环境的调试使用)
	// contains filtered or unexported fields
}

type GinCtx

type GinCtx struct {
	*gin.Context
}

GinCtx 给gin context 扩展方法

func NewGinCtx added in v0.1.4

func NewGinCtx() *GinCtx

func (*GinCtx) APIOutPut

func (ctx *GinCtx) APIOutPut(data interface{}, msg string)

func (*GinCtx) APIOutPutError

func (ctx *GinCtx) APIOutPutError(err error, msg string)

func (*GinCtx) AuthErrorOut

func (ctx *GinCtx) AuthErrorOut()

AuthErrorOut 鉴权失败

func (*GinCtx) GetIP added in v0.1.4

func (ctx *GinCtx) GetIP() string

func (*GinCtx) GetLang added in v0.1.4

func (ctx *GinCtx) GetLang() string

func (*GinCtx) GetParam

func (ctx *GinCtx) GetParam(key string) string

func (*GinCtx) GetParamInt

func (ctx *GinCtx) GetParamInt(key string) int

func (*GinCtx) GetParamInt64

func (ctx *GinCtx) GetParamInt64(key string) int64

func (*GinCtx) GetPostArgs

func (ctx *GinCtx) GetPostArgs(obj interface{}) error

GetPostArgs 获取参数

func (*GinCtx) GetQuery

func (ctx *GinCtx) GetQuery(key string) string

func (*GinCtx) GetQueryInt

func (ctx *GinCtx) GetQueryInt(key string) int

func (*GinCtx) GetQueryInt64

func (ctx *GinCtx) GetQueryInt64(key string) int64

func (*GinCtx) GetSource added in v0.1.4

func (ctx *GinCtx) GetSource() string

func (*GinCtx) PageList

func (ctx *GinCtx) PageList(pg, number, count, size int, urlTemp string) []*Page

PageList urlTemp = /a?pg=%d

func (*GinCtx) PageListInt

func (ctx *GinCtx) PageListInt(pg, number, count, size int) []int

type GrpcErrorCode added in v0.1.4

type GrpcErrorCode int32
const (
	ErrorCode_NO               GrpcErrorCode = 0
	ErrorCode_UnKnow           GrpcErrorCode = 1 //未知错误
	ErrorCode_InternalServer   GrpcErrorCode = 2 //内部服务错误
	ErrorCode_RequestData      GrpcErrorCode = 3 //请求数据错误
	ErrorCode_InterfaceInvalid GrpcErrorCode = 4 //接口无效
	ErrorCode_ServerStop       GrpcErrorCode = 5 //服务已暂停
	ErrorCode_ErrDBNoRecord    GrpcErrorCode = 6 //没有记录
	ErrorCode_Database         GrpcErrorCode = 7 //数据错误
)

type HandlerFunc

type HandlerFunc func(c *GinCtx)

type Page

type Page struct {
	Number int
	Action bool
	Url    string
}

type ResponseCode added in v0.1.4

type ResponseCode int64

ResponseCode 统一接口输出码

const (
	SuccessCode      ResponseCode = 0    // 接口成功
	ErrorCode        ResponseCode = 1    // 接口内部错误(业务错误,msg显示具体信息)
	ParamViolation   ResponseCode = 2    // 参数不合法
	InterfaceInvalid ResponseCode = 3    // 接口无效
	ServerStop       ResponseCode = 4    // 服务已暂停
	DataNone         ResponseCode = 5    // 接口没查询到数据
	TokenInvalid     ResponseCode = 1001 // token无效
	TokenExpire      ResponseCode = 1002 // token已过期
	NoPermissions    ResponseCode = 1003 // 没有权限访问
)

type ResponseJson

type ResponseJson struct {
	Code      ResponseCode `json:"code"`
	Msg       string       `json:"msg"`
	Date      any          `json:"data"`
	TimeStamp int64        `json:"timestamp"`
}

ResponseJson 统一接口输出

Jump to

Keyboard shortcuts

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