Codex Assistant
昨天 c8dffd157cd8b62023b26e62a0b92c152d959423
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const axios = require('axios');
 
// 配置
const BASE_URL = 'http://localhost:8080/api';
 
// JWT token解码函数(不验证签名,仅用于调试)
function decodeJwtToken(token) {
    try {
        // JWT token格式:header.payload.signature
        const parts = token.split('.');
        if (parts.length !== 3) {
            throw new Error('Invalid JWT token format');
        }
        
        // 解码header
        const header = JSON.parse(Buffer.from(parts[0], 'base64url').toString());
        
        // 解码payload
        const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString());
        
        return {
            header,
            payload,
            signature: parts[2]
        };
    } catch (error) {
        console.error('JWT token解码失败:', error.message);
        return null;
    }
}
 
// 测试JWT token内容
async function debugJwtToken(token) {
    console.log('=== JWT Token 调试 ===');
    
    if (!token || token === 'PASTE_YOUR_VALID_TOKEN_HERE') {
        console.log('❌ 请提供有效的JWT token');
        return;
    }
    
    console.log('Token长度:', token.length);
    console.log('Token前50字符:', token.substring(0, 50) + '...');
    
    // 解码JWT token
    const decoded = decodeJwtToken(token);
    if (decoded) {
        console.log('\n📋 JWT Token 内容:');
        console.log('Header:', JSON.stringify(decoded.header, null, 2));
        console.log('Payload:', JSON.stringify(decoded.payload, null, 2));
        
        // 检查关键字段
        if (decoded.payload.userId) {
            console.log(`\n🔍 用户ID: ${decoded.payload.userId}`);
            console.log(`   类型: ${typeof decoded.payload.userId}`);
            console.log(`   是否为负数: ${decoded.payload.userId < 0}`);
        } else {
            console.log('\n⚠️  Token中没有找到userId字段');
        }
        
        if (decoded.payload.sub) {
            console.log(`\n🔍 Subject: ${decoded.payload.sub}`);
        }
        
        if (decoded.payload.exp) {
            const expDate = new Date(decoded.payload.exp * 1000);
            const now = new Date();
            console.log(`\n⏰ Token过期时间: ${expDate.toLocaleString()}`);
            console.log(`   当前时间: ${now.toLocaleString()}`);
            console.log(`   是否已过期: ${now > expDate}`);
        }
    }
    
    // 测试token验证
    console.log('\n=== Token 验证测试 ===');
    await testTokenValidation(token);
}
 
// 测试token验证
async function testTokenValidation(token) {
    try {
        // 1. 测试简单的GraphQL查询
        console.log('1. 测试简单查询:');
        const simpleQuery = `
            query {
                __typename
            }
        `;
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: simpleQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
        
        if (response.status === 200) {
            console.log('✅ Token验证通过(基础查询成功)');
        } else {
            console.log('❌ Token验证失败,状态码:', response.status);
        }
        
    } catch (error) {
        console.log('❌ Token验证失败:', error.response?.status, error.response?.data || error.message);
    }
    
    try {
        // 2. 测试需要认证的查询
        console.log('\n2. 测试认证查询:');
        const authQuery = `
            query GetCurrentUser {
                currentUser {
                    id
                    name
                    phone
                }
            }
        `;
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: authQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
        
        if (response.data.errors) {
            console.log('❌ 认证查询失败:', response.data.errors);
        } else if (response.data.data?.currentUser) {
            const user = response.data.data.currentUser;
            console.log('✅ 认证查询成功:');
            console.log(`   用户ID: ${user.id}`);
            console.log(`   姓名: ${user.name}`);
            console.log(`   手机号: ${user.phone}`);
            
            // 检查用户ID是否匹配
            if (user.id === 152) {
                console.log('✅ 用户ID匹配(152)');
            } else {
                console.log(`⚠️  用户ID不匹配,期望152,实际${user.id}`);
            }
        } else {
            console.log('⚠️  认证查询返回空结果');
        }
        
    } catch (error) {
        console.log('❌ 认证查询失败:', error.response?.status, error.response?.data || error.message);
    }
    
    try {
        // 3. 测试评委查询
        console.log('\n3. 测试评委查询:');
        const judgeQuery = `
            query GetCurrentJudge {
                currentJudgeInfo {
                    judgeId
                    judgeName
                    title
                    company
                }
            }
        `;
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: judgeQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
        
        if (response.data.errors) {
            console.log('❌ 评委查询失败:', response.data.errors);
        } else if (response.data.data?.currentJudgeInfo) {
            const judge = response.data.data.currentJudgeInfo;
            console.log('✅ 评委查询成功:');
            console.log(`   评委ID: ${judge.judgeId}`);
            console.log(`   评委名: ${judge.judgeName}`);
            
            // 检查评委ID是否匹配
            if (judge.judgeId === 72) {
                console.log('✅ 评委ID匹配(72)');
            } else {
                console.log(`⚠️  评委ID不匹配,期望72,实际${judge.judgeId}`);
            }
        } else {
            console.log('⚠️  评委查询返回空结果(可能不是评委)');
        }
        
    } catch (error) {
        console.log('❌ 评委查询失败:', error.response?.status, error.response?.data || error.message);
    }
}
 
// 获取新的微信登录token并调试
async function getAndDebugNewToken() {
    console.log('=== 获取新的微信登录token ===');
    
    try {
        const wxLoginMutation = `
            mutation WxLogin($input: WxLoginRequest!) {
                wxLogin(input: $input) {
                    token
                    userInfo {
                        userId
                        name
                        phone
                        userType
                    }
                    success
                    message
                    hasJudge
                }
            }
        `;
        
        // 需要一个新的微信code
        const wxCode = 'NEED_NEW_WX_CODE_HERE'; // 请替换为新的微信code
        
        if (wxCode === 'NEED_NEW_WX_CODE_HERE') {
            console.log('❌ 请先替换脚本中的微信code');
            return;
        }
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: wxLoginMutation,
            variables: {
                input: {
                    code: wxCode,
                    wxOpenid: "ogxxA1-KrSVTdqI9T1uaB1BQwPGU", // 使用已知的openid
                    loginIp: "127.0.0.1",
                    deviceInfo: "test-device",
                    phoneAuthorized: false
                }
            }
        });
        
        if (response.data.errors) {
            console.log('❌ 微信登录失败:', response.data.errors);
            return;
        }
        
        const loginData = response.data.data.wxLogin;
        console.log('✅ 微信登录成功:');
        console.log('- 用户ID:', loginData.userInfo?.userId);
        console.log('- 用户名:', loginData.userInfo?.name);
        console.log('- 用户类型:', loginData.userInfo?.userType);
        console.log('- 是否有评委权限:', loginData.hasJudge);
        
        if (loginData.token) {
            console.log('\n开始调试新获取的token...');
            await debugJwtToken(loginData.token);
        } else {
            console.log('❌ 未获取到token');
        }
        
    } catch (error) {
        console.error('❌ 微信登录失败:', error.response?.data || error.message);
    }
}
 
// 主函数
async function main() {
    console.log('🔍 开始JWT Token调试...\n');
    
    // 选择调试方式
    const useExistingToken = true; // 设置为true使用已有token,false获取新token
    
    if (useExistingToken) {
        // 使用已有的token进行调试
        const existingToken = 'PASTE_YOUR_VALID_TOKEN_HERE'; // 请替换为你的token
        await debugJwtToken(existingToken);
    } else {
        // 获取新token并调试
        await getAndDebugNewToken();
    }
}
 
// 运行主函数
if (require.main === module) {
    main();
}
 
module.exports = { debugJwtToken, decodeJwtToken };