mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-20 17:54:30 +08:00
add core/error/util files
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/mayswind/lab/pkg/errs"
|
||||
)
|
||||
|
||||
const FIELD_REQUEST_ID_KEY = "REQUEST_ID"
|
||||
const FIELD_TOKEN_CLAIMS_KEY = "TOKEN_CLAIMS"
|
||||
const FIELD_RESPONSE_ERROR = "RESPONSE_ERROR"
|
||||
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
// DO NOT ADD ANY FIELD IN THIS CONTEXT, THIS CONTEXT IS JUST A WRAPPER
|
||||
}
|
||||
|
||||
func (c *Context) SetRequestId(requestId string) {
|
||||
c.Set(FIELD_REQUEST_ID_KEY, requestId)
|
||||
}
|
||||
|
||||
func (c *Context) GetRequestId() string {
|
||||
requestId, exists := c.Get(FIELD_REQUEST_ID_KEY)
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return requestId.(string)
|
||||
}
|
||||
|
||||
func (c *Context) SetTokenClaims(claims *UserTokenClaims) {
|
||||
c.Set(FIELD_TOKEN_CLAIMS_KEY, claims)
|
||||
}
|
||||
|
||||
func (c *Context) GetTokenClaims() *UserTokenClaims {
|
||||
claims, exists := c.Get(FIELD_TOKEN_CLAIMS_KEY)
|
||||
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return claims.(*UserTokenClaims)
|
||||
}
|
||||
|
||||
func (c *Context) GetCurrentUid() int64 {
|
||||
claims := c.GetTokenClaims()
|
||||
|
||||
if claims == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
uid, err := strconv.ParseInt(claims.Id, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return uid
|
||||
}
|
||||
|
||||
func (c *Context) SetResponseError(error *errs.Error) {
|
||||
c.Set(FIELD_RESPONSE_ERROR, error)
|
||||
}
|
||||
|
||||
func (c *Context) GetResponseError() *errs.Error {
|
||||
err, exists := c.Get(FIELD_RESPONSE_ERROR)
|
||||
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err.(*errs.Error)
|
||||
}
|
||||
|
||||
func WrapContext(ginCtx *gin.Context) *Context {
|
||||
return &Context{
|
||||
Context: ginCtx,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package core
|
||||
|
||||
import "github.com/mayswind/lab/pkg/errs"
|
||||
|
||||
type MiddlewareHandlerFunc func(*Context)
|
||||
|
||||
type ApiHandlerFunc func(*Context) (interface{}, *errs.Error)
|
||||
@@ -0,0 +1,19 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
type TokenType byte
|
||||
|
||||
const (
|
||||
USER_TOKEN_TYPE_NORMAL TokenType = 1
|
||||
USER_TOKEN_TYPE_REQUIRE_2FA TokenType = 2
|
||||
)
|
||||
|
||||
type UserTokenClaims struct {
|
||||
UserTokenId string `json:"userTokenId"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Type TokenType `json:"type"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
Reference in New Issue
Block a user