Codex Assistant
1 天以前 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
const axios = require('axios');
const jwt = require('jsonwebtoken');
 
const BASE_URL = 'http://localhost:8080/api';
 
async function testWxLoginRest() {
    console.log('=== 测试微信登录 (REST API) ===');
    
    try {
        // 使用用户提供的微信code
        const wxCode = '0f3cd4ll2X7Eqg4242ml2zvTju4cd4l1';
        
        console.log('使用微信code:', wxCode);
        console.log('请求URL:', `${BASE_URL}/auth/wx-login`);
        
        const requestData = {
            code: wxCode,
            wxOpenid: "ogxxA1-KrSVTdqI9T1uaB1BQwPGU", // 使用已知的openid
            loginIp: "127.0.0.1",
            deviceInfo: "test-device",
            phoneAuthorized: false
        };
        
        console.log('请求数据:', JSON.stringify(requestData, null, 2));
        
        const response = await axios.post(`${BASE_URL}/auth/wx-login`, requestData, {
            headers: {
                'Content-Type': 'application/json'
            }
        });
        
        console.log('\n📋 微信登录响应:');
        console.log('状态码:', response.status);
        console.log('响应数据:', JSON.stringify(response.data, null, 2));
        
        if (response.data && response.data.token) {
            const token = response.data.token;
            console.log('\n✅ 成功获取到token:', token.substring(0, 50) + '...');
            
            // 解码JWT token查看内容
            try {
                const decoded = jwt.decode(token, { complete: true });
                console.log('\n🔍 JWT Token内容:');
                console.log('Header:', JSON.stringify(decoded.header, null, 2));
                console.log('Payload:', JSON.stringify(decoded.payload, null, 2));
                
                const userId = decoded.payload.userId || decoded.payload.sub;
                console.log('\n👤 用户信息:');
                console.log('用户ID:', userId);
                console.log('用户类型:', decoded.payload.userType);
                console.log('角色:', decoded.payload.roles);
                
                // 测试使用token访问需要权限的接口
                console.log('\n🔐 测试权限验证...');
                await testWithToken(token);
                
            } catch (jwtError) {
                console.log('❌ JWT解码失败:', jwtError.message);
            }
            
        } else {
            console.log('❌ 登录失败,未获取到token');
        }
        
    } catch (error) {
        console.error('\n❌ 测试过程中发生错误:');
        console.error('状态码:', error.response?.status);
        console.error('错误信息:', error.response?.data || error.message);
        if (error.response?.data) {
            console.error('详细错误:', JSON.stringify(error.response.data, null, 2));
        }
        console.error('错误堆栈:', error.stack);
    }
}
 
async function testWithToken(token) {
    try {
        // 测试获取用户信息
        console.log('1. 测试获取用户信息...');
        const userProfileQuery = `
            query {
                userProfile {
                    id
                    name
                    phone
                    userType
                    roles
                }
            }
        `;
 
        const userResponse = await axios.post(`${BASE_URL}/graphql`, {
            query: userProfileQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
 
        console.log('用户信息响应:', JSON.stringify(userResponse.data, null, 2));
        
        // 测试获取评审统计
        console.log('\n2. 测试获取评审统计...');
        const reviewStatsQuery = `
            query {
                reviewStatistics {
                    unReviewedCount
                    reviewedCount
                    studentUnReviewedCount
                }
            }
        `;
 
        const statsResponse = await axios.post(`${BASE_URL}/graphql`, {
            query: reviewStatsQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
 
        console.log('评审统计响应:', JSON.stringify(statsResponse.data, null, 2));
        
        // 测试获取未评审项目
        console.log('\n3. 测试获取未评审项目...');
        const unReviewedQuery = `
            query {
                unReviewedProjects(page: 1, pageSize: 10, searchKeyword: "") {
                    total
                    hasMore
                    items {
                        id
                        projectName
                        activityName
                        studentName
                    }
                }
            }
        `;
 
        const projectsResponse = await axios.post(`${BASE_URL}/graphql`, {
            query: unReviewedQuery
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
 
        console.log('未评审项目响应:', JSON.stringify(projectsResponse.data, null, 2));
        
    } catch (error) {
        console.error('权限测试失败:', error.response?.status, error.response?.data || error.message);
    }
}
 
// 运行测试
testWxLoginRest().then(() => {
    console.log('\n=== 测试完成 ===');
}).catch(error => {
    console.error('测试执行失败:', error);
});