From 5384dc7cf380394f35578c9aa9308dc9bc0897b5 Mon Sep 17 00:00:00 2001 From: hujie Date: Thu, 5 Mar 2026 16:53:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=9A=84role=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- svr/visitor.go | 2 +- utils/jwt.go | 1 + web/user.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 57dfa24..7791084 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,12 @@ go 1.24.0 require ( github.com/gin-gonic/gin v1.10.0 + github.com/golang-jwt/jwt/v5 v5.3.1 github.com/redis/go-redis/v9 v9.18.0 github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 go.uber.org/zap v1.27.0 + golang.org/x/text v0.34.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gorm.io/driver/mysql v1.6.0 gorm.io/gorm v1.31.1 @@ -30,7 +32,6 @@ require ( github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/go-sql-driver/mysql v1.9.3 // indirect github.com/goccy/go-json v0.10.2 // indirect - github.com/golang-jwt/jwt/v5 v5.3.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect @@ -60,7 +61,6 @@ require ( golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.34.0 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/svr/visitor.go b/svr/visitor.go index 9d9aec6..aec8e50 100644 --- a/svr/visitor.go +++ b/svr/visitor.go @@ -93,7 +93,7 @@ func VisitorMiddleware() gin.HandlerFunc { SecretKey: jwtConfig.Secret, }) if claims, err := jwtUtil.ParseToken(token); err == nil { - userInfo.WithUserID(claims.UserID).WithUserName(claims.UserName) + userInfo.WithUserID(claims.UserID).WithUserName(claims.UserName).WithRole(claims.Role) } } } diff --git a/utils/jwt.go b/utils/jwt.go index baf0865..4d724bd 100644 --- a/utils/jwt.go +++ b/utils/jwt.go @@ -36,6 +36,7 @@ type JWT struct { type CustomClaims struct { UserID string `json:"user_id"` UserName string `json:"user_name"` + Role int `json:"role"` Data map[string]interface{} `json:"data,omitempty"` jwt.RegisteredClaims } diff --git a/web/user.go b/web/user.go index faf8659..9dceda0 100644 --- a/web/user.go +++ b/web/user.go @@ -14,6 +14,7 @@ type UserInfo struct { FromIP string `json:"from_ip"` Province string `json:"province"` City string `json:"city"` + Role int `json:"role"` } // contextKey is the type for context keys to prevent collisions @@ -28,6 +29,7 @@ const ( FromIPKey contextKey = "fromIP" ProvinceKey contextKey = "province" CityKey contextKey = "city" + RoleKey contextKey = "role" ) // GetUserID returns user ID from context @@ -163,6 +165,25 @@ func SetCity(ctx context.Context, city string) context.Context { return context.WithValue(ctx, CityKey, city) } +// GetRole returns role from context +func GetRole(ctx context.Context) int { + if ctx == nil { + return 0 + } + if val, ok := ctx.Value(RoleKey).(int); ok { + return val + } + return 0 +} + +// SetRole sets role in context and returns new context +func SetRole(ctx context.Context, role int) context.Context { + if ctx == nil { + ctx = context.Background() + } + return context.WithValue(ctx, RoleKey, role) +} + // FromContext builds UserInfo from individual context fields // Returns nil if no fields are found func FromContext(ctx context.Context) *UserInfo { @@ -200,6 +221,10 @@ func FromContext(ctx context.Context) *UserInfo { info.City = city hasData = true } + if role := GetRole(ctx); role != 0 { + info.Role = role + hasData = true + } if !hasData { return nil @@ -223,6 +248,7 @@ func ToContext(ctx context.Context, userInfo *UserInfo) context.Context { ctx = SetFromIP(ctx, userInfo.FromIP) ctx = SetProvince(ctx, userInfo.Province) ctx = SetCity(ctx, userInfo.City) + ctx = SetRole(ctx, userInfo.Role) return ctx } @@ -275,3 +301,9 @@ func (u *UserInfo) WithCity(city string) *UserInfo { u.City = city return u } + +// WithRole sets the role +func (u *UserInfo) WithRole(role int) *UserInfo { + u.Role = role + return u +}