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);
|
});
|