// 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-applete-gateway/errors" "strconv" "github.com/tidwall/gjson" "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "git.getensh.com/common/gopkgs/jwtwrapper" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" ) const SYSTEMPHONELOGINKEY = "system_phone_login_" func GetKey(prefix, tail string) string { return fmt.Sprintf("%s%s", prefix, tail) } // 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 } type TokenInfo struct { Uid int64 Cid int64 GardenId int64 UserName string GardenName string ShouldChooseGarden bool Phone string // 公司登录小区成功后,为true ByCompany bool // 公司登录小区验证接口,该字段为true IsCompanyEnter bool Super bool Codes []string SingleSignTime string GPermissionTime string UPermissionTime string } func GetSubjectValue(ctx *gin.Context) (info TokenInfo, err 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.Cid = gjson.GetBytes(StrToBytes(claims.Subject), "cid").Int() ret.GardenId = gjson.GetBytes(StrToBytes(claims.Subject), "garden_id").Int() ret.UserName = gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String() ret.GardenName = gjson.GetBytes(StrToBytes(claims.Subject), "garden_name").String() ret.Phone = gjson.GetBytes(StrToBytes(claims.Subject), "phone").String() ret.IsCompanyEnter = gjson.GetBytes(StrToBytes(claims.Subject), "is_company_enter").Bool() ret.ByCompany = gjson.GetBytes(StrToBytes(claims.Subject), "by_company").Bool() ret.ShouldChooseGarden = gjson.GetBytes(StrToBytes(claims.Subject), "shood_choose_garden").Bool() ret.Super = gjson.GetBytes(StrToBytes(claims.Subject), "is_super_group").Bool() codes := gjson.GetBytes(StrToBytes(claims.Subject), "codes").Map() if len(codes) > 0 { for k, _ := range codes { ret.Codes = append(ret.Codes, k) } } return ret, 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 }