43 lines
715 B
Go
43 lines
715 B
Go
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
|
|
}
|