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