const axios = require('axios');
|
|
// 配置
|
const BASE_URL = 'http://localhost:8080/api';
|
|
// 获取有效token的函数
|
async function getValidToken() {
|
console.log('请提供一个新的微信登录code来获取token...');
|
console.log('你可以从微信开发者工具的网络面板中获取最新的code');
|
|
// 这里需要用户提供新的code
|
const code = 'YOUR_WECHAT_CODE_HERE'; // 用户需要替换这个值
|
|
if (code === 'YOUR_WECHAT_CODE_HERE') {
|
throw new Error('请先替换脚本中的微信code');
|
}
|
|
try {
|
const response = await axios.post(`${BASE_URL}/auth/wechat-login`, {
|
code: code
|
});
|
|
if (response.data && response.data.token) {
|
console.log('✅ 微信登录成功');
|
console.log('Token:', response.data.token);
|
return response.data.token;
|
} else {
|
throw new Error('登录响应中没有token');
|
}
|
} catch (error) {
|
console.error('❌ 微信登录失败:', error.response?.data || error.message);
|
throw error;
|
}
|
}
|
|
// 调试用户信息的GraphQL查询
|
const DEBUG_USER_INFO_QUERY = `
|
query DebugUserInfo {
|
# 获取当前用户基本信息
|
currentUser {
|
id
|
name
|
phone
|
}
|
|
# 获取当前评委信息
|
currentJudgeInfo {
|
judgeId
|
judgeName
|
title
|
company
|
}
|
|
# 获取评审统计(这个查询会检查用户是否是评委)
|
reviewStatistics {
|
unReviewedCount
|
reviewedCount
|
studentUnReviewedCount
|
}
|
}
|
`;
|
|
// 测试用户权限的函数
|
async function testUserPermissions(token) {
|
console.log('\n=== 测试用户权限和评委身份 ===');
|
|
try {
|
const response = await axios.post(`${BASE_URL}/graphql`, {
|
query: DEBUG_USER_INFO_QUERY
|
}, {
|
headers: {
|
'Authorization': `Bearer ${token}`,
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('HTTP状态码:', response.status);
|
|
if (response.data.errors) {
|
console.log('❌ GraphQL错误:');
|
response.data.errors.forEach((error, index) => {
|
console.log(` ${index + 1}. ${error.message}`);
|
if (error.path) {
|
console.log(` 路径: ${error.path.join(' -> ')}`);
|
}
|
});
|
}
|
|
if (response.data.data) {
|
console.log('✅ 成功获取数据:');
|
|
// 检查当前用户信息
|
if (response.data.data.currentUser) {
|
console.log('📋 当前用户信息:');
|
console.log(' 用户ID:', response.data.data.currentUser.id);
|
console.log(' 用户名:', response.data.data.currentUser.name);
|
console.log(' 手机号:', response.data.data.currentUser.phone);
|
} else {
|
console.log('⚠️ 无法获取当前用户信息');
|
}
|
|
// 检查评委信息
|
if (response.data.data.currentJudgeInfo) {
|
console.log('👨⚖️ 当前评委信息:');
|
console.log(' 评委ID:', response.data.data.currentJudgeInfo.judgeId);
|
console.log(' 评委名:', response.data.data.currentJudgeInfo.judgeName);
|
console.log(' 职位:', response.data.data.currentJudgeInfo.title);
|
console.log(' 公司:', response.data.data.currentJudgeInfo.company);
|
} else {
|
console.log('❌ 当前用户不是评委或无法获取评委信息');
|
}
|
|
// 检查评审统计
|
if (response.data.data.reviewStatistics) {
|
console.log('📊 评审统计:');
|
console.log(' 未评审数量:', response.data.data.reviewStatistics.unReviewedCount);
|
console.log(' 已评审数量:', response.data.data.reviewStatistics.reviewedCount);
|
console.log(' 学员未评审数量:', response.data.data.reviewStatistics.studentUnReviewedCount);
|
} else {
|
console.log('❌ 无法获取评审统计(可能不是评委)');
|
}
|
}
|
|
} catch (error) {
|
console.error('❌ 请求失败:', error.response?.data || error.message);
|
if (error.response?.status) {
|
console.error('HTTP状态码:', error.response.status);
|
}
|
}
|
}
|
|
// 测试简单的评审项目查询
|
async function testSimpleReviewQuery(token) {
|
console.log('\n=== 测试简单的评审项目查询 ===');
|
|
const SIMPLE_REVIEW_QUERY = `
|
query TestReviewQuery {
|
unReviewedProjects(searchKeyword: "", page: 1, pageSize: 5) {
|
totalCount
|
totalPages
|
currentPage
|
items {
|
id
|
title
|
}
|
}
|
}
|
`;
|
|
try {
|
const response = await axios.post(`${BASE_URL}/graphql`, {
|
query: SIMPLE_REVIEW_QUERY
|
}, {
|
headers: {
|
'Authorization': `Bearer ${token}`,
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('HTTP状态码:', response.status);
|
|
if (response.data.errors) {
|
console.log('❌ GraphQL错误:');
|
response.data.errors.forEach((error, index) => {
|
console.log(` ${index + 1}. ${error.message}`);
|
});
|
}
|
|
if (response.data.data) {
|
console.log('✅ 查询成功:');
|
console.log('未评审项目数量:', response.data.data.unReviewedProjects?.totalCount || 0);
|
}
|
|
} catch (error) {
|
console.error('❌ 查询失败:', error.response?.data || error.message);
|
}
|
}
|
|
// 主函数
|
async function main() {
|
console.log('🔍 开始调试用户权限和评委身份问题...\n');
|
|
try {
|
// 获取token
|
const token = await getValidToken();
|
|
// 测试用户权限
|
await testUserPermissions(token);
|
|
// 测试评审查询
|
await testSimpleReviewQuery(token);
|
|
} catch (error) {
|
console.error('❌ 调试过程中发生错误:', error.message);
|
}
|
}
|
|
// 运行主函数
|
if (require.main === module) {
|
main();
|
}
|
|
module.exports = { getValidToken, testUserPermissions, testSimpleReviewQuery };
|