// Copyright 2020 github.com. All rights reserved. // Use of this source code is governed by github.com. package utils import ( "fmt" "google.golang.org/grpc/status" "property-household-gateway/errors" "strconv" "github.com/tidwall/gjson" "git.getensh.com/common/gopkgs/jwtwrapper" "git.getensh.com/common/gopkgs/logger" "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "go.uber.org/zap" ) type TokenInfo struct { Uid int64 OpenId string NickName string RealName string Phone string OpenimId string UserIcon string IdNumber string } func GetJwtTokenInfo(ctx *gin.Context) (TokenInfo, error) { ret := TokenInfo{} // 从上下文获取信息 value, ok := ctx.Get("claims") if !ok { logger.Error("func", zap.String("call", "ctx.Get"), zap.String("error", "no claims data")) return ret, errors.ParamsError } // 解析数据 claims, ok := value.(*jwt.StandardClaims) if !ok { logger.Error("func", zap.String("call", "Claims assert"), zap.String("error", "NOT Claims type")) return ret, errors.ParamsError } // 类型转换 ret.Uid, _ = strconv.ParseInt(claims.Id, 10, 64) ret.NickName = gjson.GetBytes(StrToBytes(claims.Subject), "nick_name").String() ret.RealName = gjson.GetBytes(StrToBytes(claims.Subject), "real_name").String() ret.OpenId = gjson.GetBytes(StrToBytes(claims.Subject), "open_id").String() ret.Phone = gjson.GetBytes(StrToBytes(claims.Subject), "phone").String() ret.OpenimId = gjson.GetBytes(StrToBytes(claims.Subject), "openim_id").String() ret.UserIcon = gjson.GetBytes(StrToBytes(claims.Subject), "user_icon").String() ret.IdNumber = gjson.GetBytes(StrToBytes(claims.Subject), "id_number").String() return ret, nil } // GetJwtIdFromCtx 从 gin 上下文中获取 id func GetJwtIdFromCtx(ctx *gin.Context) (int64, string, error) { // 从上下文获取信息 value, ok := ctx.Get("claims") if !ok { logger.Error("func", zap.String("call", "ctx.Get"), zap.String("error", "no claims data")) return 0, "", errors.ParamsError } // 解析数据 claims, ok := value.(*jwt.StandardClaims) if !ok { logger.Error("func", zap.String("call", "Claims assert"), zap.String("error", "NOT Claims type")) return 0, "", errors.ParamsError } // 类型转换 id, _ := strconv.ParseInt(claims.Id, 10, 64) userName := gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String() return id, userName, nil } func GetSubjectValue(ctx *gin.Context) (bool, int64, error) { value, ok := ctx.Get("claims") if !ok { logger.Error("func", zap.String("call", "ctx.Get"), zap.String("error", "no claims data")) return false, 0, errors.ParamsError } // 解析数据 claims, ok := value.(*jwt.StandardClaims) if !ok { logger.Error("func", zap.String("call", "Claims assert"), zap.String("error", "NOT Claims type")) return false, 0, errors.ParamsError } projectUser := gjson.GetBytes(StrToBytes(claims.Subject), "project_user").Bool() projectId := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int() fmt.Printf("projectid:%v\n", projectId) return projectUser, projectId, nil } // GetJwtIdFromCtx 从 gin 上下文中获取 id func GetJwtProjectIdFromCtx(ctx *gin.Context) (int64, error) { // 从上下文获取信息 value, ok := ctx.Get("claims") if !ok { logger.Error("func", zap.String("call", "ctx.Get"), zap.String("error", "no claims data")) return 0, errors.ParamsError } // 解析数据 claims, ok := value.(*jwt.StandardClaims) if !ok { logger.Error("func", zap.String("call", "Claims assert"), zap.String("error", "NOT Claims type")) return 0, errors.ParamsError } // 类型转换 id := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int() return id, nil } func TmpTokenVeriy(token string) (int64, error) { // 解析token claims, err := jwtwrapper.ParseToken(token) if err != nil { switch err.(*jwt.ValidationError).Errors { case jwt.ValidationErrorExpired: return 0, status.Error(10010, "登录token错误") default: return 0, status.Error(10010, "登录token错误") } return 0, status.Error(10010, "登录token错误") } isTmp := gjson.GetBytes([]byte(claims.Subject), "tmp_token").Bool() if !isTmp { return 0, status.Error(10010, "登录token错误") } projectId, _ := strconv.ParseInt(claims.Id, 10, 64) return projectId, nil } func EmailTokenVeriy(token string) (uid int64, projectId int64, name string, email string, err error) { // 解析token claims, err := jwtwrapper.ParseToken(token) if err != nil { switch err.(*jwt.ValidationError).Errors { case jwt.ValidationErrorExpired: return 0, 0, "", "", status.Error(10010, "凭据过期") default: return 0, 0, "", "", status.Error(10010, "凭据错误") } return 0, 0, "", "", status.Error(10010, "凭据错误") } name = gjson.GetBytes([]byte(claims.Subject), "user_name").String() email = gjson.GetBytes([]byte(claims.Subject), "email").String() if email == "" { return 0, 0, "", "", status.Error(10010, "邮箱为空") } uid, _ = strconv.ParseInt(claims.Id, 10, 64) projectId = gjson.GetBytes([]byte(claims.Subject), "project_id").Int() return uid, projectId, name, email, nil }