init: taotie-api 项目初始化
This commit is contained in:
42
utils/sctx/sctx.go
Normal file
42
utils/sctx/sctx.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package sctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MyKey string
|
||||
|
||||
const CurrentKey MyKey = "taotie"
|
||||
|
||||
type CurrentUser struct {
|
||||
UserId string
|
||||
UserName string
|
||||
TenantId string
|
||||
}
|
||||
|
||||
func SetCurrentUser(ctx context.Context, cuser *CurrentUser) context.Context {
|
||||
if c, ok := ctx.(*gin.Context); ok {
|
||||
c.Set(CurrentKey, cuser)
|
||||
return c
|
||||
}
|
||||
|
||||
return context.WithValue(ctx, CurrentKey, cuser)
|
||||
}
|
||||
|
||||
func GetCurrentUser(ctx context.Context) *CurrentUser {
|
||||
if c, ok := ctx.(*gin.Context); ok {
|
||||
if user, ok := c.Get(CurrentKey); ok {
|
||||
if cuser, ok := user.(*CurrentUser); ok {
|
||||
return cuser
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cuser, ok := ctx.Value(CurrentKey).(*CurrentUser); ok {
|
||||
return cuser
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user