mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-07 02:26:00 +08:00
Error code standardization
This commit is contained in:
Vendored
+9
-25
@@ -5,14 +5,9 @@ import (
|
||||
"Open_IM/pkg/common/tokenverify"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
uidPidToken = "UID_PID_TOKEN_STATUS:"
|
||||
)
|
||||
|
||||
type Token interface {
|
||||
//结果为空 不返回错误
|
||||
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||
@@ -21,9 +16,9 @@ type Token interface {
|
||||
}
|
||||
|
||||
type TokenRedis struct {
|
||||
RedisClient *RedisClient
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
redisClient *RedisClient
|
||||
accessSecret string
|
||||
accessExpire int64
|
||||
}
|
||||
|
||||
func NewTokenRedis(redisClient *RedisClient, accessSecret string, accessExpire int64) *TokenRedis {
|
||||
@@ -32,21 +27,12 @@ func NewTokenRedis(redisClient *RedisClient, accessSecret string, accessExpire i
|
||||
|
||||
// 结果为空 不返回错误
|
||||
func (t *TokenRedis) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
m, err := t.RedisClient.GetClient().HGetAll(context.Background(), key).Result()
|
||||
if err != nil && err == redis.Nil {
|
||||
return nil, nil
|
||||
}
|
||||
mm := make(map[string]int)
|
||||
for k, v := range m {
|
||||
mm[k] = utils.StringToInt(v)
|
||||
}
|
||||
return mm, utils.Wrap(err, "")
|
||||
return t.redisClient.GetTokensWithoutError(ctx, userID, platform)
|
||||
}
|
||||
|
||||
// 创建token
|
||||
func (t *TokenRedis) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||
tokens, err := t.GetTokensWithoutError(ctx, userID, platform)
|
||||
tokens, err := t.redisClient.GetTokensWithoutError(ctx, userID, platform)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -58,18 +44,16 @@ func (t *TokenRedis) CreateToken(ctx context.Context, userID string, platform st
|
||||
}
|
||||
}
|
||||
if len(deleteTokenKey) != 0 {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
err := t.RedisClient.GetClient().HDel(context.Background(), key, deleteTokenKey...).Err()
|
||||
err := t.redisClient.DeleteTokenByUidPid(ctx, userID, platform, deleteTokenKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
claims := tokenverify.BuildClaims(userID, platform, t.AccessExpire)
|
||||
claims := tokenverify.BuildClaims(userID, platform, t.accessExpire)
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString([]byte(t.AccessSecret))
|
||||
tokenString, err := token.SignedString([]byte(t.accessSecret))
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
return "", utils.Wrap(t.RedisClient.GetClient().HSet(context.Background(), key, tokenString, constant.NormalToken).Err(), "")
|
||||
return tokenString, t.redisClient.AddTokenFlag(ctx, userID, platform, tokenString, constant.NormalToken)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user