Documentation ¶
Index ¶
- Constants
- Variables
- func FindArrayInterface(ds []interface{}, checkor func(interface{}) bool) (int, interface{})
- func FindArrayString(ds []string, checkor func(string) bool) (int, string)
- func FixResponse400Error(c *gin.Context, err error, errfunc func())
- func FixResponse401Error(c *gin.Context, err error, errfunc func())
- func FixResponse403Error(c *gin.Context, err error, errfunc func())
- func FixResponse406Error(c *gin.Context, err error, errfunc func())
- func FixResponse500Error(c *gin.Context, err error, errfunc func())
- func FixResponse500Error2Logger(c *gin.Context, err error)
- func FixResponseError(c *gin.Context, err error) bool
- func ForEache(ds interface{}, next func(interface{}) (bool, error)) (err error)
- func GetAcceptLanguage(c *gin.Context) string
- func GetClientIP(c *gin.Context) string
- func GetCtxValue(ctx context.Context, key string) (interface{}, bool)
- func GetCtxValueToString(ctx context.Context, key string) (string, bool)
- func GetHostIP(c *gin.Context) string
- func GetTraceID(c *gin.Context) string
- func GetUserInfo(c *gin.Context) (auth.UserInfo, bool)
- func IfBool(condition bool, ifture, ifalse bool) bool
- func IfExec(condition bool, ifture, ifalse func())
- func IfFunc(condition bool, ifture, ifalse func() interface{}) interface{}
- func IfInt(condition bool, ifture, ifalse int) int
- func IfObject(condition bool, ifture, ifalse interface{}) interface{}
- func IfString(condition bool, ifture, ifalse string) string
- func JSONMarshalToString(v interface{}) string
- func ParseForm(c *gin.Context, obj interface{}) error
- func ParseJSON(c *gin.Context, obj interface{}) error
- func ParseQuery(c *gin.Context, obj interface{}) error
- func ResError(ctx *gin.Context, em *ErrorModel) error
- func ResErrorResBody(ctx *gin.Context, em *ErrorModel) error
- func ResJSON(ctx *gin.Context, status int, v interface{})
- func ResJSONResBody(ctx *gin.Context, status int, v interface{})
- func ResSuccess(ctx *gin.Context, v interface{}) error
- func ReverseStr(s string) string
- func SetCtxValue(ctx context.Context, key string, value interface{}) bool
- func SetUserInfo(c *gin.Context, user auth.UserInfo)
- func SplitStrCR(s string, x rune, c int) string
- type ErrorInfo
- func New0Error(ctx *gin.Context, showType int, emsg *i18n.Message) *ErrorInfo
- func New0ErrorWithData(ctx *gin.Context, showType int, emsg *i18n.Message, data interface{}) *ErrorInfo
- func NewError(ctx *gin.Context, showType int, emsg *i18n.Message, ...) *ErrorInfo
- func NewErrorWithData(ctx *gin.Context, showType int, emsg *i18n.Message, ...) *ErrorInfo
- func NewWrapError(ctx *gin.Context, em *ErrorModel) *ErrorInfo
- type ErrorModel
- type ErrorNone
- type ErrorRedirect
- type H
- type Page
- type PaginationParam
- type Success
- type UserInfoFunc
Constants ¶
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" )
定义上下文中的键
const ( // ShowNone 静音 ShowNone = 0 // ShowWarn 消息警告 ShowWarn = 1 // ShowError 消息错误 ShowError = 2 // ShowNotify 通知; ShowNotify = 4 // ShowPage 页 ShowPage = 9 )
Variables ¶
var ( Err400BadRequest = &ErrorModel{Status: 400, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-BAD-REQUEST", 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
var ( JSONMarshal = json.Marshal JSONUnmarshal = json.Unmarshal JSONMarshalIndent = json.MarshalIndent JSONNewDecoder = json.NewDecoder JSONNewEncoder = json.NewEncoder )
定义JSON操作
var ( // Response of request ResponseTypeJSON = "application/json; charset=utf-8" ResponseTypeTEXT = "text/plain; charset=utf-8" )
定义Type
Functions ¶
func FindArrayInterface ¶
FindArrayInterface ...
func FindArrayString ¶
FindArrayString ...
func FixResponse400Error ¶
FixResponse400Error 修复返回的异常
func FixResponse401Error ¶
FixResponse401Error 修复返回的异常, 注意, 401异常会导致系统重定向到登陆页面
func FixResponse403Error ¶
FixResponse403Error 修复返回的异常
func FixResponse406Error ¶
FixResponse406Error 修复返回的异常
func FixResponse500Error ¶
FixResponse500Error 修复返回的异常
func FixResponse500Error2Logger ¶
FixResponse500Error2Logger 修复返回的异常
func FixResponseError ¶
FixResponseError 上级应用已经处理了返回值
func GetCtxValue ¶
GetCtxValue 获取令牌加密方式
func GetCtxValueToString ¶
GetCtxValueToString 获取令牌加密方式
func JSONMarshalToString ¶
func JSONMarshalToString(v interface{}) string
JSONMarshalToString JSON编码为字符串
func ParseQuery ¶
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 ResJSONResBody ¶
ResJSONResBody 响应JSON数据 禁止service层调用
func ResSuccess ¶
ResSuccess 包装响应错误 禁止service层调用,请使用NewSuccess替换
func SetCtxValue ¶
SetCtxValue 配置令牌加密方式
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 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 包装响应错误
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 ErrorRedirect ¶
type ErrorRedirect struct { Status int // http.StatusSeeOther State string // 状态, 用户还原缓存现场 Location string }
ErrorRedirect 重定向
func (*ErrorRedirect) Error ¶
func (e *ErrorRedirect) Error() string
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 分页查询条件