helper

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Prefix      = "zgo"
	UserInfoKey = Prefix + "/user-info"   // user info
	TraceIDKey  = Prefix + "/tract-id"    // trace id
	ReqBodyKey  = Prefix + "/req-body"    // request body
	ResBodyKey  = Prefix + "/res-body"    // response body
	ResJwtKey   = Prefix + "/res-jwt-kid" // jwt kid
	ResO2cKey   = Prefix + "/res-o2c-kid" // o2c kid
	ResS2cKey   = Prefix + "/res-s2c-kid" // s2c kid, 子母令牌, 标记母令牌ID

	XReqOriginHostKey   = "X-Request-Origin-Host"
	XReqOriginPathKey   = "X-Request-Origin-Path"
	XReqOriginMethodKey = "X-Request-Origin-Method"
)

定义上下文中的键

View Source
const (
	// ShowNone 静音
	ShowNone = 0
	// ShowWarn 消息警告
	ShowWarn = 1
	// ShowError 消息错误
	ShowError = 2
	// ShowNotify 通知;
	ShowNotify = 4
	// ShowPage 页
	ShowPage = 9
)

Variables

View Source
var (
	Err400BadRequest       = &ErrorModel{Status: 400, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-BAD-REQUEST", Other: "请求发生错误"}}
	Err401Unauthorized     = &ErrorModel{Status: 401, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-UNAUTHORIZED", Other: "用户没有权限(令牌、用户名、密码错误)"}}
	Err403Forbidden        = &ErrorModel{Status: 403, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-FORBIDDEN", Other: "用户未得到授权,访问是被禁止的"}}
	Err404NotFound         = &ErrorModel{Status: 404, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-NOT-FOUND", Other: "发出的请求针对的是不存在的记录,服务器没有进行操作"}}
	Err405MethodNotAllowed = &ErrorModel{Status: 405, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-METHOD-NOT-ALLOWED", Other: "请求的方法不允许"}}
	Err406NotAcceptable    = &ErrorModel{Status: 406, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-NOT-ACCEPTABLE", Other: "请求的格式不可得"}}
	Err429TooManyRequests  = &ErrorModel{Status: 429, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-TOO-MANY-REQUESTS", Other: "请求次数过多"}}
	Err456TokenExpired     = &ErrorModel{Status: 456, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-TOKEN-EXPIRED", Other: "请求令牌已过期"}}
	Err500InternalServer   = &ErrorModel{Status: 500, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-INTERNAL-SERVER", Other: "服务器发生错误"}}
)

定义错误 https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

View Source
var (
	JSONMarshal       = json.Marshal
	JSONUnmarshal     = json.Unmarshal
	JSONMarshalIndent = json.MarshalIndent
	JSONNewDecoder    = json.NewDecoder
	JSONNewEncoder    = json.NewEncoder
)

定义JSON操作

View Source
var (
	// Response of request
	ResponseTypeJSON = "application/json; charset=utf-8"
	ResponseTypeTEXT = "text/plain; charset=utf-8"
)

定义Type

Functions

func FindArrayInterface

func FindArrayInterface(ds []interface{}, checkor func(interface{}) bool) (int, interface{})

FindArrayInterface ...

func FindArrayString

func FindArrayString(ds []string, checkor func(string) bool) (int, string)

FindArrayString ...

func FixResponse400Error

func FixResponse400Error(c *gin.Context, err error, errfunc func())

FixResponse400Error 修复返回的异常

func FixResponse401Error

func FixResponse401Error(c *gin.Context, err error, errfunc func())

FixResponse401Error 修复返回的异常, 注意, 401异常会导致系统重定向到登陆页面

func FixResponse403Error

func FixResponse403Error(c *gin.Context, err error, errfunc func())

FixResponse403Error 修复返回的异常

func FixResponse406Error

func FixResponse406Error(c *gin.Context, err error, errfunc func())

FixResponse406Error 修复返回的异常

func FixResponse500Error

func FixResponse500Error(c *gin.Context, err error, errfunc func())

FixResponse500Error 修复返回的异常

func FixResponse500Error2Logger

func FixResponse500Error2Logger(c *gin.Context, err error)

FixResponse500Error2Logger 修复返回的异常

func FixResponseError

func FixResponseError(c *gin.Context, err error) bool

FixResponseError 上级应用已经处理了返回值

func ForEache

func ForEache(ds interface{}, next func(interface{}) (bool, error)) (err error)

ForEache ...

func GetAcceptLanguage

func GetAcceptLanguage(c *gin.Context) string

GetAcceptLanguage 获取浏览器语言

func GetClientIP

func GetClientIP(c *gin.Context) string

GetClientIP 获取客户端IP

func GetCtxValue

func GetCtxValue(ctx context.Context, key string) (interface{}, bool)

GetCtxValue 获取令牌加密方式

func GetCtxValueToString

func GetCtxValueToString(ctx context.Context, key string) (string, bool)

GetCtxValueToString 获取令牌加密方式

func GetHostIP

func GetHostIP(c *gin.Context) string

GetHostIP 获取主机端IP

func GetTraceID

func GetTraceID(c *gin.Context) string

GetTraceID 根据山下问,获取追踪ID

func GetUserInfo

func GetUserInfo(c *gin.Context) (auth.UserInfo, bool)

GetUserInfo 用户

func IfBool

func IfBool(condition bool, ifture, ifalse bool) bool

IfBool 判断

func IfExec

func IfExec(condition bool, ifture, ifalse func())

IfExec 判断

func IfFunc

func IfFunc(condition bool, ifture, ifalse func() interface{}) interface{}

IfFunc 判断

func IfInt

func IfInt(condition bool, ifture, ifalse int) int

IfInt 判断

func IfObject

func IfObject(condition bool, ifture, ifalse interface{}) interface{}

IfObject 判断

func IfString

func IfString(condition bool, ifture, ifalse string) string

IfString 判断

func JSONMarshalToString

func JSONMarshalToString(v interface{}) string

JSONMarshalToString JSON编码为字符串

func ParseForm

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

ParseForm 解析Form请求, 注意,解析失败后需要直接返回

func ParseJSON

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

ParseJSON 解析请求JSON, 注意,解析失败后需要直接返回

func ParseQuery

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

ParseQuery 解析Query参数, 注意,解析失败后需要直接返回

func ResError

func ResError(ctx *gin.Context, em *ErrorModel) error

ResError 包装响应错误 禁止service层调用,请使用NewWarpError替换

func ResErrorResBody

func ResErrorResBody(ctx *gin.Context, em *ErrorModel) error

ResErrorResBody 包装响应错误 禁止service层调用

func ResJSON

func ResJSON(ctx *gin.Context, status int, v interface{})

ResJSON 响应JSON数据 禁止service层调用

func ResJSONResBody

func ResJSONResBody(ctx *gin.Context, status int, v interface{})

ResJSONResBody 响应JSON数据 禁止service层调用

func ResSuccess

func ResSuccess(ctx *gin.Context, v interface{}) error

ResSuccess 包装响应错误 禁止service层调用,请使用NewSuccess替换

func ReverseStr

func ReverseStr(s string) string

ReverseStr 反正字符串

func SetCtxValue

func SetCtxValue(ctx context.Context, key string, value interface{}) bool

SetCtxValue 配置令牌加密方式

func SetUserInfo

func SetUserInfo(c *gin.Context, user auth.UserInfo)

SetUserInfo 用户

func SplitStrCR

func SplitStrCR(s string, x rune, c int) string

SplitStrCR 截取字符串

Types

type ErrorInfo

type ErrorInfo struct {
	Success      bool        `json:"success"`        // 请求成功, false
	Data         interface{} `json:"data,omitempty"` // 响应数据
	ErrorCode    string      `json:"errorCode"`      // 错误代码
	ErrorMessage string      `json:"errorMessage"`   // 向用户显示消息
	ShowType     int         `json:"showType"`       //错误显示类型:0静音; 1条消息警告; 2消息错误; 4通知; 9页
	TraceID      string      `json:"traceId"`        // 方便进行后端故障排除:唯一的请求ID

}

ErrorInfo 异常的请求结果体

func New0Error

func New0Error(ctx *gin.Context, showType int, emsg *i18n.Message) *ErrorInfo

New0Error 包装响应错误, 没有参数

func New0ErrorWithData

func New0ErrorWithData(ctx *gin.Context, showType int, emsg *i18n.Message, data interface{}) *ErrorInfo

New0ErrorWithData 包装响应错误, 没有参数

func NewError

func NewError(ctx *gin.Context, showType int, emsg *i18n.Message, args map[string]interface{}) *ErrorInfo

NewError 包装响应错误

func NewErrorWithData

func NewErrorWithData(ctx *gin.Context, showType int, emsg *i18n.Message, args map[string]interface{}, data interface{}) *ErrorInfo

NewErrorWithData 包装响应错误

func NewWrapError

func NewWrapError(ctx *gin.Context, em *ErrorModel) *ErrorInfo

NewWrapError 包装响应错误

func (*ErrorInfo) Error

func (e *ErrorInfo) Error() string

type ErrorModel

type ErrorModel struct {
	Status       int
	ShowType     int
	ErrorMessage *i18n.Message
	ErrorArgs    map[string]interface{}
}

ErrorModel 异常模型

func Wrap400Response

func Wrap400Response(ctx *gin.Context, err error) *ErrorModel

Wrap400Response 无法解析异常

func (*ErrorModel) Error

func (a *ErrorModel) Error() string

type ErrorNone

type ErrorNone struct {
}

ErrorNone 返回值已经被处理,无返回值

func (*ErrorNone) Error

func (e *ErrorNone) Error() string

type ErrorRedirect

type ErrorRedirect struct {
	Status   int    // http.StatusSeeOther
	State    string // 状态, 用户还原缓存现场
	Location string
}

ErrorRedirect 重定向

func (*ErrorRedirect) Error

func (e *ErrorRedirect) Error() string

type H

type H map[string]interface{}

H h -> map

type Page

type Page struct {
	PageNo   int         `json:"pageNo,omitempty"`   // 页索引
	PageSize int         `json:"pageSize,omitempty"` // 页条数
	Total    int         `json:"total,omitempty"`    // 总条数
	List     interface{} `json:"list,omitempty"`     // 数据
}

Page 分页数据

type PaginationParam

type PaginationParam struct {
	PageSign  string `query:"pageSign"`                              // 请求参数, total | list | both
	PageNo    uint   `query:"pageNo,default=1"`                      // 当前页
	PageSize  uint   `query:"pageSize,default=20" binding:"max=100"` // 页大小
	PageTotal uint   `query:"pageTotal"`                             // 上次统计的数据条数
}

PaginationParam 分页查询条件

type Success

type Success struct {
	Success bool        `json:"success"`        // 请求成功, false
	Data    interface{} `json:"data,omitempty"` // 响应数据
	TraceID string      `json:"traceId"`        // 方便进行后端故障排除:唯一的请求ID
}

Success 正常请求结构体

func NewSuccess

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

NewSuccess 包装响应结果

func (*Success) Error

func (e *Success) Error() string

type UserInfoFunc

type UserInfoFunc interface {
	GetUserInfo() (auth.UserInfo, bool)
	SetUserInfo(auth.UserInfo)
}

UserInfoFunc user

Jump to

Keyboard shortcuts

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