Codex Assistant
23 小时以前 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const axios = require('axios');
 
// 配置
const BASE_URL = 'http://localhost:8080/api';
 
// JWT token解码函数(不验证签名,仅用于调试)
function decodeJwtToken(token) {
    try {
        const parts = token.split('.');
        if (parts.length !== 3) {
            throw new Error('Invalid JWT token format');
        }
        
        const header = JSON.parse(Buffer.from(parts[0], 'base64url').toString());
        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;
    }
}
 
// 测试微信登录
async function testWxLogin() {
    console.log('=== 测试微信登录 ===');
    
    try {
        const wxLoginMutation = `
            mutation WxLogin($input: WxLoginRequest!) {
                wxLogin(input: $input) {
                    token
                    userInfo {
                        userId
                        name
                        phone
                        userType
                    }
                    success
                    message
                    hasJudge
                }
            }
        `;
        
        // 使用用户提供的微信code
        const wxCode = '0b3ycd0w32tGL53puK1w3ho1Hv2ycd0R'; // 用户提供的code
        
        console.log('使用微信code:', wxCode);
        
        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
                }
            }
        });
        
        console.log('\n📋 微信登录响应:');
        console.log('状态码:', response.status);
        
        if (response.data.errors) {
            console.log('❌ 微信登录失败:', response.data.errors);
            return null;
        }
        
        const loginData = response.data.data.wxLogin;
        console.log('\n✅ 微信登录成功:');
        console.log('- Success:', loginData.success);
        console.log('- Message:', loginData.message);
        console.log('- HasJudge:', loginData.hasJudge);
        
        if (loginData.userInfo) {
            console.log('\n👤 用户信息:');
            console.log('- 用户ID:', loginData.userInfo.userId);
            console.log('- 姓名:', loginData.userInfo.name);
            console.log('- 手机号:', loginData.userInfo.phone);
            console.log('- 用户类型:', loginData.userInfo.userType);
            
            // 验证用户ID是否为152
            if (loginData.userInfo.userId === 152) {
                console.log('✅ 用户ID匹配(152)');
            } else {
                console.log(`⚠️  用户ID不匹配,期望152,实际${loginData.userInfo.userId}`);
            }
        }
        
        if (loginData.token) {
            console.log('\n🔑 JWT Token信息:');
            console.log('- Token长度:', loginData.token.length);
            console.log('- Token前50字符:', loginData.token.substring(0, 50) + '...');
            
            // 解码JWT token
            const decoded = decodeJwtToken(loginData.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🔍 Token中的用户ID: ${decoded.payload.userId}`);
                    console.log(`   类型: ${typeof decoded.payload.userId}`);
                    console.log(`   是否为负数: ${decoded.payload.userId < 0}`);
                    
                    if (decoded.payload.userId === 152) {
                        console.log('✅ Token中的用户ID匹配(152)');
                    } else {
                        console.log(`⚠️  Token中的用户ID不匹配,期望152,实际${decoded.payload.userId}`);
                    }
                }
                
                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进行后续测试
            await testWithToken(loginData.token);
            
            return loginData.token;
        } else {
            console.log('❌ 未获取到token');
            return null;
        }
        
    } catch (error) {
        console.error('❌ 微信登录失败:', error.response?.data || error.message);
        return null;
    }
}
 
// 使用token进行测试
async function testWithToken(token) {
    console.log('\n=== 使用Token进行测试 ===');
    
    // 1. 测试获取当前用户信息
    console.log('\n1. 测试获取当前用户信息:');
    try {
        const userQuery = `
            query GetCurrentUser {
                currentUser {
                    id
                    name
                    phone
                }
            }
        `;
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: userQuery
        }, {
            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}`);
            
            if (user.id === 152) {
                console.log('✅ 用户ID验证通过(152)');
            } else {
                console.log(`⚠️  用户ID不匹配,期望152,实际${user.id}`);
            }
        } else {
            console.log('⚠️  无法获取当前用户信息');
        }
    } catch (error) {
        console.log('❌ 获取当前用户失败:', error.response?.data || error.message);
    }
    
    // 2. 测试获取当前评委信息
    console.log('\n2. 测试获取当前评委信息:');
    try {
        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}`);
            console.log(`   职位: ${judge.title}`);
            console.log(`   公司: ${judge.company}`);
            
            if (judge.judgeId === 72) {
                console.log('✅ 评委ID验证通过(72)');
            } else {
                console.log(`⚠️  评委ID不匹配,期望72,实际${judge.judgeId}`);
            }
        } else {
            console.log('⚠️  无法获取当前评委信息(可能不是评委)');
        }
    } catch (error) {
        console.log('❌ 获取当前评委失败:', error.response?.data || error.message);
    }
    
    // 3. 测试评审统计查询
    console.log('\n3. 测试评审统计查询:');
    try {
        const statsQuery = `
            query GetReviewStats {
                reviewStatistics {
                    unReviewedCount
                    reviewedCount
                    studentUnReviewedCount
                }
            }
        `;
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: statsQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
        
        if (response.data.errors) {
            console.log('❌ 获取评审统计失败:', response.data.errors);
            response.data.errors.forEach(error => {
                console.log(`   错误: ${error.message}`);
                if (error.extensions) {
                    console.log(`   扩展信息:`, error.extensions);
                }
            });
        } else if (response.data.data?.reviewStatistics) {
            const stats = response.data.data.reviewStatistics;
            console.log('✅ 评审统计:');
            console.log(`   未评审: ${stats.unReviewedCount}`);
            console.log(`   已评审: ${stats.reviewedCount}`);
            console.log(`   学员未评审: ${stats.studentUnReviewedCount}`);
        }
    } catch (error) {
        console.log('❌ 获取评审统计失败:', error.response?.data || error.message);
    }
    
    // 4. 测试评审项目查询(这是原来400错误的查询)
    console.log('\n4. 测试评审项目查询(原400错误查询):');
    try {
        const projectsQuery = `
            query GetUnreviewedProjects($searchKeyword: String!, $page: Int!, $pageSize: Int!) {
                unReviewedProjects(searchKeyword: $searchKeyword, page: $page, pageSize: $pageSize) {
                    totalCount
                    currentPage
                    totalPages
                    projects {
                        id
                        projectName
                        teamName
                        category
                        submissionTime
                    }
                }
            }
        `;
        
        const response = await axios.post(`${BASE_URL}/graphql`, {
            query: projectsQuery,
            variables: {
                searchKeyword: "",
                page: 1,
                pageSize: 10
            }
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
        
        if (response.data.errors) {
            console.log('❌ 获取评审项目失败:', response.data.errors);
            response.data.errors.forEach(error => {
                console.log(`   错误: ${error.message}`);
            });
        } else if (response.data.data?.unReviewedProjects) {
            const projects = response.data.data.unReviewedProjects;
            console.log('✅ 评审项目查询成功:');
            console.log(`   总数: ${projects.totalCount}`);
            console.log(`   当前页: ${projects.currentPage}`);
            console.log(`   总页数: ${projects.totalPages}`);
            console.log(`   项目数量: ${projects.projects?.length || 0}`);
            
            if (projects.projects && projects.projects.length > 0) {
                console.log('   前几个项目:');
                projects.projects.slice(0, 3).forEach((project, index) => {
                    console.log(`     ${index + 1}. ${project.projectName} (${project.teamName})`);
                });
            }
        }
    } catch (error) {
        console.log('❌ 获取评审项目失败:', error.response?.data || error.message);
    }
}
 
// 主函数
async function main() {
    console.log('🔍 开始测试微信登录和用户权限...\n');
    
    const token = await testWxLogin();
    
    if (token) {
        console.log('\n🎉 测试完成!');
        console.log('如果上面的测试都成功,说明用户权限正常。');
        console.log('如果评审查询仍然失败,可能是后端的其他逻辑问题。');
    } else {
        console.log('\n❌ 微信登录失败,无法进行后续测试');
    }
}
 
// 运行主函数
if (require.main === module) {
    main();
}
 
module.exports = { testWxLogin, testWithToken };