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

@@ -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,