Codex Assistant
17 小时以前 58d9f460b2f8c34430285115e2557d18333c5cab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const http = require('http');
 
// 使用之前获取的有效token
const validToken = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiItODMzNDg4IiwicGhvbmUiOiJvZ3h4QTEtS3JTVlRkcUk5VDF1YUIxQlF3UEdVIiwiaWF0IjoxNzU5ODM5NDQ1LCJleHAiOjE3NTk5MjU4NDV9.Xoq2S-zQzI3GMWxaSS2A5GGlPsR3z2BRkzg4HK3tHhE';
 
// 使用有效token测试saveUserInfo
function testSaveUserInfo(token) {
    console.log('=== 使用有效token测试saveUserInfo ===');
    
    const mutation = `
        mutation SaveUserInfo($input: UserInput!) {
            saveUserInfo(input: $input) {
                id
                name
                phone
                gender
                birthday
                wxOpenId
                unionId
            }
        }
    `;
 
    const variables = {
        input: {
            name: "测试用户JWT修复",
            phone: "13981970816",
            gender: "MALE",
            birthday: "2025-10-07"
        }
    };
 
    const postData = JSON.stringify({
        query: mutation,
        variables: variables
    });
 
    const options = {
        hostname: 'localhost',
        port: 8080,
        path: '/api/graphql',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData),
            'Authorization': `Bearer ${token}`
        }
    };
 
    console.log('发送GraphQL请求,携带有效token...');
    console.log('Token:', token.substring(0, 30) + '...');
 
    const req = http.request(options, (res) => {
        console.log('响应状态码:', res.statusCode);
 
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
 
        res.on('end', () => {
            console.log('响应内容:', data);
            try {
                const response = JSON.parse(data);
                if (response.errors) {
                    console.log('❌ GraphQL错误:', response.errors);
                    response.errors.forEach((error, index) => {
                        console.log(`错误 ${index + 1}:`, error.message);
                        if (error.extensions) {
                            console.log('错误详情:', error.extensions);
                        }
                    });
                } else if (response.data && response.data.saveUserInfo) {
                    console.log('✅ 保存用户信息成功!');
                    console.log('用户信息:', response.data.saveUserInfo);
                } else {
                    console.log('⚠️ 未知响应格式:', response);
                }
            } catch (e) {
                console.log('❌ 解析响应失败:', e.message);
                console.log('原始响应:', data);
            }
        });
    });
 
    req.on('error', (e) => {
        console.error('❌ 请求错误:', e.message);
    });
 
    req.write(postData);
    req.end();
}
 
// 检查token是否过期
function checkTokenExpiry(token) {
    try {
        const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
        const now = Math.floor(Date.now() / 1000);
        const exp = payload.exp;
        
        console.log('Token信息:');
        console.log('- 用户ID:', payload.sub);
        console.log('- 签发时间:', new Date(payload.iat * 1000).toLocaleString());
        console.log('- 过期时间:', new Date(exp * 1000).toLocaleString());
        console.log('- 当前时间:', new Date(now * 1000).toLocaleString());
        console.log('- 是否过期:', now > exp ? '是' : '否');
        console.log('- 剩余时间:', Math.max(0, exp - now), '秒');
        
        return now <= exp;
    } catch (e) {
        console.log('❌ Token解析失败:', e.message);
        return false;
    }
}
 
// 开始测试
console.log('=== JWT Token 验证 ===');
if (checkTokenExpiry(validToken)) {
    console.log('\n✅ Token有效,开始测试saveUserInfo...\n');
    testSaveUserInfo(validToken);
} else {
    console.log('\n❌ Token已过期,请重新获取token');
}