手机号适配双端
This commit is contained in:
@@ -5,14 +5,16 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"tydata-server/app/main/model"
|
||||
jwtx "tydata-server/common/jwt"
|
||||
)
|
||||
|
||||
const CtxKeyJwtUserId = "userId"
|
||||
|
||||
// 定义错误类型
|
||||
var (
|
||||
ErrNoUserIdInCtx = errors.New("上下文中没有用户ID") // 未登录
|
||||
ErrInvalidUserId = errors.New("用户ID格式无效") // 数据异常
|
||||
ErrNoInCtx = errors.New("上下文中没有相关数据")
|
||||
ErrInvalidUserId = errors.New("用户ID格式无效") // 数据异常
|
||||
)
|
||||
|
||||
// GetUidFromCtx 从 context 中获取用户 ID
|
||||
@@ -20,7 +22,11 @@ func GetUidFromCtx(ctx context.Context) (int64, error) {
|
||||
// 尝试从上下文中获取 jwtUserId
|
||||
value := ctx.Value(CtxKeyJwtUserId)
|
||||
if value == nil {
|
||||
return 0, ErrNoUserIdInCtx
|
||||
claims, err := GetClaimsFromCtx(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return claims.UserId, nil
|
||||
}
|
||||
|
||||
// 根据值的类型进行不同处理
|
||||
@@ -47,12 +53,52 @@ func GetUidFromCtx(ctx context.Context) (int64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetClaimsFromCtx(ctx context.Context) (*jwtx.JwtClaims, error) {
|
||||
value := ctx.Value(jwtx.ExtraKey)
|
||||
if value == nil {
|
||||
return nil, ErrNoInCtx
|
||||
}
|
||||
|
||||
// 首先尝试直接断言为 *jwtx.JwtClaims
|
||||
if claims, ok := value.(*jwtx.JwtClaims); ok {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
// 如果直接断言失败,尝试从 map[string]interface{} 中解析
|
||||
if claimsMap, ok := value.(map[string]interface{}); ok {
|
||||
return jwtx.MapToJwtClaims(claimsMap)
|
||||
}
|
||||
|
||||
return nil, ErrNoInCtx
|
||||
}
|
||||
|
||||
// IsNoUserIdError 判断是否是未登录错误
|
||||
func IsNoUserIdError(err error) bool {
|
||||
return errors.Is(err, ErrNoUserIdInCtx)
|
||||
return errors.Is(err, ErrNoInCtx)
|
||||
}
|
||||
|
||||
// IsInvalidUserIdError 判断是否是用户ID格式错误
|
||||
func IsInvalidUserIdError(err error) bool {
|
||||
return errors.Is(err, ErrInvalidUserId)
|
||||
}
|
||||
|
||||
// GetPlatformFromCtx 从 context 中获取平台
|
||||
func GetPlatformFromCtx(ctx context.Context) (string, error) {
|
||||
platform, platformOk := ctx.Value("platform").(string)
|
||||
if !platformOk {
|
||||
return "", fmt.Errorf("平台不存在: %s", platform)
|
||||
}
|
||||
|
||||
switch platform {
|
||||
case model.PlatformWxMini:
|
||||
return model.PlatformWxMini, nil
|
||||
case model.PlatformWxH5:
|
||||
return model.PlatformWxH5, nil
|
||||
case model.PlatformApp:
|
||||
return model.PlatformApp, nil
|
||||
case model.PlatformH5:
|
||||
return model.PlatformH5, nil
|
||||
default:
|
||||
return "", fmt.Errorf("不支持的支付平台: %s", platform)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
package ctxdata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetUidFromCtx(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctxSetup func() context.Context
|
||||
wantUid int64
|
||||
wantError error
|
||||
}{
|
||||
{
|
||||
name: "正常情况_有效用户ID_json.Number",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.WithValue(context.Background(), CtxKeyJwtUserId, json.Number("12345"))
|
||||
},
|
||||
wantUid: 12345,
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "正常情况_有效用户ID_int64",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.WithValue(context.Background(), CtxKeyJwtUserId, int64(12345))
|
||||
},
|
||||
wantUid: 12345,
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "正常情况_有效用户ID_int",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.WithValue(context.Background(), CtxKeyJwtUserId, 12345)
|
||||
},
|
||||
wantUid: 12345,
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "正常情况_有效用户ID_float64",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.WithValue(context.Background(), CtxKeyJwtUserId, float64(12345))
|
||||
},
|
||||
wantUid: 12345,
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "异常情况_上下文中无用户ID",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.Background()
|
||||
},
|
||||
wantUid: 0,
|
||||
wantError: ErrNoUserIdInCtx,
|
||||
},
|
||||
{
|
||||
name: "异常情况_用户ID类型错误",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.WithValue(context.Background(), CtxKeyJwtUserId, "非数字类型")
|
||||
},
|
||||
wantUid: 0,
|
||||
wantError: ErrInvalidUserId,
|
||||
},
|
||||
{
|
||||
name: "异常情况_用户ID无法转换为int64",
|
||||
ctxSetup: func() context.Context {
|
||||
return context.WithValue(context.Background(), CtxKeyJwtUserId, json.Number("非数字内容"))
|
||||
},
|
||||
wantUid: 0,
|
||||
wantError: ErrInvalidUserId,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := tt.ctxSetup()
|
||||
gotUid, gotErr := GetUidFromCtx(ctx)
|
||||
|
||||
// 检查返回的用户ID
|
||||
if gotUid != tt.wantUid {
|
||||
t.Errorf("GetUidFromCtx() 返回用户ID = %v, 期望值 %v", gotUid, tt.wantUid)
|
||||
}
|
||||
|
||||
// 检查错误类型
|
||||
if tt.wantError == nil && gotErr != nil {
|
||||
t.Errorf("GetUidFromCtx() 返回意外错误 = %v", gotErr)
|
||||
}
|
||||
|
||||
if tt.wantError != nil && !errors.Is(gotErr, tt.wantError) {
|
||||
t.Errorf("GetUidFromCtx() 错误类型 = %v, 期望错误类型 %v", gotErr, tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNoUserIdError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "是未登录错误",
|
||||
err: ErrNoUserIdInCtx,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "包装的未登录错误",
|
||||
err: errors.New("外层错误: " + ErrNoUserIdInCtx.Error()),
|
||||
expected: false, // 直接字符串拼接不会保留错误链
|
||||
},
|
||||
{
|
||||
name: "使用fmt.Errorf包装的未登录错误",
|
||||
err: errors.New("外层错误"),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "非未登录错误",
|
||||
err: ErrInvalidUserId,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "nil错误",
|
||||
err: nil,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsNoUserIdError(tt.err); got != tt.expected {
|
||||
t.Errorf("IsNoUserIdError() = %v, 期望值 %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsInvalidUserIdError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "是无效用户ID错误",
|
||||
err: ErrInvalidUserId,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "包装的无效用户ID错误",
|
||||
err: errors.New("外层错误: " + ErrInvalidUserId.Error()),
|
||||
expected: false, // 直接字符串拼接不会保留错误链
|
||||
},
|
||||
{
|
||||
name: "使用fmt.Errorf包装的无效用户ID错误",
|
||||
err: errors.New("外层错误"),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "非无效用户ID错误",
|
||||
err: ErrNoUserIdInCtx,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "nil错误",
|
||||
err: nil,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsInvalidUserIdError(tt.err); got != tt.expected {
|
||||
t.Errorf("IsInvalidUserIdError() = %v, 期望值 %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,68 +1,94 @@
|
||||
package jwtx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
// Token 生成逻辑的函数,接收 userId、过期时间和密钥,返回生成的 token
|
||||
func GenerateJwtToken(userId int64, secret string, expireTime int64) (string, error) {
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
// 定义 JWT Claims
|
||||
claims := jwt.MapClaims{
|
||||
"exp": now + expireTime, // token 过期时间
|
||||
"iat": now, // 签发时间
|
||||
"userId": userId, // 用户ID
|
||||
const ExtraKey = "extra"
|
||||
|
||||
type JwtClaims struct {
|
||||
UserId int64 `json:"userId"`
|
||||
AgentId int64 `json:"agentId"`
|
||||
Platform string `json:"platform"`
|
||||
// 用户身份类型:0-临时用户,1-正式用户
|
||||
UserType int64 `json:"userType"`
|
||||
// 是否代理:0-否,1-是
|
||||
IsAgent int64 `json:"isAgent"`
|
||||
}
|
||||
|
||||
// MapToJwtClaims 将 map[string]interface{} 转换为 JwtClaims 结构体
|
||||
func MapToJwtClaims(claimsMap map[string]interface{}) (*JwtClaims, error) {
|
||||
// 使用JSON序列化/反序列化的方式自动转换
|
||||
jsonData, err := json.Marshal(claimsMap)
|
||||
if err != nil {
|
||||
return nil, errors.New("序列化claims失败")
|
||||
}
|
||||
|
||||
// 创建新的 JWT token
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
var claims JwtClaims
|
||||
if err := json.Unmarshal(jsonData, &claims); err != nil {
|
||||
return nil, errors.New("反序列化claims失败")
|
||||
}
|
||||
|
||||
// 使用密钥对 token 签名
|
||||
signedToken, err := token.SignedString([]byte(secret))
|
||||
return &claims, nil
|
||||
}
|
||||
|
||||
// GenerateJwtToken 生成JWT token
|
||||
func GenerateJwtToken(claims JwtClaims, secret string, expire int64) (string, error) {
|
||||
now := time.Now().Unix()
|
||||
|
||||
// 将 claims 结构体转换为 map[string]interface{}
|
||||
claimsBytes, err := json.Marshal(claims)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return signedToken, nil
|
||||
var claimsMap map[string]interface{}
|
||||
if err := json.Unmarshal(claimsBytes, &claimsMap); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
jwtClaims := jwt.MapClaims{
|
||||
"exp": now + expire,
|
||||
"iat": now,
|
||||
"userId": claims.UserId,
|
||||
ExtraKey: claimsMap,
|
||||
}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtClaims)
|
||||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
func ParseJwtToken(tokenStr string, secret string) (int64, error) {
|
||||
|
||||
func ParseJwtToken(tokenStr string, secret string) (*JwtClaims, error) {
|
||||
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(secret), nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
return 0, errors.New("invalid JWT")
|
||||
return nil, errors.New("invalid JWT")
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok || !token.Valid {
|
||||
return 0, errors.New("invalid JWT claims")
|
||||
return nil, errors.New("invalid JWT claims")
|
||||
}
|
||||
|
||||
// 从 claims 中提取 userId
|
||||
userIdRaw, ok := claims["userId"]
|
||||
if !ok {
|
||||
return 0, errors.New("userId not found in JWT")
|
||||
extraInfo, exists := claims[ExtraKey]
|
||||
if !exists {
|
||||
return nil, errors.New("extra not found in JWT")
|
||||
}
|
||||
|
||||
// 处理不同类型的 userId,确保它被转换为 int64
|
||||
switch userId := userIdRaw.(type) {
|
||||
case float64:
|
||||
return int64(userId), nil
|
||||
case int64:
|
||||
return userId, nil
|
||||
case string:
|
||||
// 如果 userId 是字符串,可以尝试将其转换为 int64
|
||||
parsedId, err := strconv.ParseInt(userId, 10, 64)
|
||||
if err != nil {
|
||||
return 0, errors.New("invalid userId in JWT")
|
||||
}
|
||||
return parsedId, nil
|
||||
default:
|
||||
return 0, errors.New("unsupported userId type in JWT")
|
||||
// 尝试直接断言为 JwtClaims 结构体
|
||||
if jwtClaims, ok := extraInfo.(JwtClaims); ok {
|
||||
return &jwtClaims, nil
|
||||
}
|
||||
|
||||
// 尝试从 map[string]interface{} 中解析
|
||||
if claimsMap, ok := extraInfo.(map[string]interface{}); ok {
|
||||
return MapToJwtClaims(claimsMap)
|
||||
}
|
||||
|
||||
return nil, errors.New("unsupported extra type in JWT")
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ const DB_UPDATE_AFFECTED_ZERO_ERROR uint32 = 100006
|
||||
const PARAM_VERIFICATION_ERROR uint32 = 100007
|
||||
const CUSTOM_ERROR uint32 = 100008
|
||||
const USER_NOT_FOUND uint32 = 100009
|
||||
const USER_NEED_BIND_MOBILE uint32 = 100010
|
||||
|
||||
const LOGIN_FAILED uint32 = 200001
|
||||
const LOGIC_QUERY_WAIT uint32 = 200002
|
||||
|
||||
Reference in New Issue
Block a user