This commit is contained in:
2025-12-09 18:55:28 +08:00
parent 8d00d67540
commit c23ab8338b
209 changed files with 5445 additions and 3963 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"ycc-server/app/main/model"
jwtx "ycc-server/common/jwt"
"encoding/json"
"errors"
"fmt"
)
@@ -17,40 +16,25 @@ var (
ErrInvalidUserId = errors.New("用户ID格式无效") // 数据异常
)
// GetUidFromCtx 从 context 中获取用户 ID
func GetUidFromCtx(ctx context.Context) (int64, error) {
// GetUidFromCtx 从 context 中获取用户 ID(字符串)
func GetUidFromCtx(ctx context.Context) (string, error) {
// 尝试从上下文中获取 jwtUserId
value := ctx.Value(CtxKeyJwtUserId)
if value == nil {
claims, err := GetClaimsFromCtx(ctx)
if err != nil {
return 0, err
}
return claims.UserId, nil
}
if value == nil {
claims, err := GetClaimsFromCtx(ctx)
if err != nil {
return "", err
}
return claims.UserId, nil
}
// 根据值的类型进行不同处理
switch v := value.(type) {
case json.Number:
// 如果是 json.Number 类型,转换为 int64
uid, err := v.Int64()
if err != nil {
return 0, fmt.Errorf("%w: %v", ErrInvalidUserId, err)
}
return uid, nil
case int64:
// 如果已经是 int64 类型,直接返回
return v, nil
case float64:
// 有些JSON解析器可能会将数字解析为float64
return int64(v), nil
case int:
// 处理int类型
return int64(v), nil
default:
// 其他类型都视为无效
return 0, fmt.Errorf("%w: 期望类型 json.Number 或 int64, 实际类型 %T", ErrInvalidUserId, value)
}
switch v := value.(type) {
case string:
return v, nil
default:
return "", fmt.Errorf("%w: 期望类型 string, 实际类型 %T", ErrInvalidUserId, value)
}
}
func GetClaimsFromCtx(ctx context.Context) (*jwtx.JwtClaims, error) {

View File

@@ -11,13 +11,15 @@ import (
const ExtraKey = "extra"
type JwtClaims struct {
UserId int64 `json:"userId"`
AgentId int64 `json:"agentId"`
UserId string `json:"userId"`
AgentId string `json:"agentId"`
Platform string `json:"platform"`
// 用户身份类型0-临时用户1-正式用户
UserType int64 `json:"userType"`
// 是否代理0-否1-是
IsAgent int64 `json:"isAgent"`
IsAgent int64 `json:"isAgent"`
AuthType string `json:"authType"`
AuthKey string `json:"authKey"`
}
// MapToJwtClaims 将 map[string]interface{} 转换为 JwtClaims 结构体

View File

@@ -11,8 +11,8 @@ import (
func TestGenerateJwtToken(t *testing.T) {
// 测试数据
testClaims := JwtClaims{
UserId: 1,
AgentId: 0,
UserId: "1",
AgentId: "",
Platform: "wxh5",
UserType: 0,
IsAgent: 0,
@@ -37,8 +37,8 @@ func TestGenerateJwtToken(t *testing.T) {
{
name: "不同用户数据",
claims: JwtClaims{
UserId: 99999,
AgentId: 11111,
UserId: "99999",
AgentId: "",
Platform: "mobile",
UserType: 0,
IsAgent: 1,
@@ -103,8 +103,8 @@ func TestGenerateJwtToken(t *testing.T) {
if claims, ok := parsedToken.Claims.(jwt.MapClaims); ok {
// 验证userId
if userId, exists := claims["userId"]; exists {
if int64(userId.(float64)) != tt.claims.UserId {
t.Errorf("token中的userId不匹配期望%d,实际%v", tt.claims.UserId, userId)
if userId.(string) != tt.claims.UserId {
t.Errorf("token中的userId不匹配期望%s,实际%v", tt.claims.UserId, userId)
}
} else {
t.Error("token中缺少userId字段")
@@ -144,8 +144,8 @@ func TestGenerateJwtToken(t *testing.T) {
func TestGenerateJwtTokenAndParse(t *testing.T) {
// 测试生成token后能够正确解析
testClaims := JwtClaims{
UserId: 12345,
AgentId: 67890,
UserId: "12345",
AgentId: "",
Platform: "web",
UserType: 1,
IsAgent: 0,
@@ -188,8 +188,8 @@ func TestGenerateJwtTokenAndParse(t *testing.T) {
func BenchmarkGenerateJwtToken(t *testing.B) {
// 性能测试
testClaims := JwtClaims{
UserId: 12345,
AgentId: 67890,
UserId: "12345",
AgentId: "",
Platform: "web",
UserType: 1,
IsAgent: 0,
@@ -209,8 +209,8 @@ func BenchmarkGenerateJwtToken(t *testing.B) {
func TestParseJwtToken(t *testing.T) {
// 使用你修改的测试数据
testClaims := JwtClaims{
UserId: 6,
AgentId: 0,
UserId: "6",
AgentId: "",
Platform: "wxh5",
UserType: 0,
IsAgent: 0,
@@ -347,8 +347,8 @@ func TestParseCustomJwtToken(t *testing.T) {
func TestGenerateAndParseWithRealData(t *testing.T) {
// 使用真实数据生成token
realClaims := JwtClaims{
UserId: 1,
AgentId: 0,
UserId: "1",
AgentId: "",
Platform: "wxh5",
UserType: 0,
IsAgent: 0,