Files
nxxmdata/admin-system/frontend/test-users-frontend.js
2025-09-01 02:42:03 +08:00

67 lines
2.1 KiB
JavaScript

/**
* 测试前端用户管理功能
*/
const axios = require('axios');
// 模拟localStorage
const mockLocalStorage = {
getItem: (key) => {
const storage = {
'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsImVtYWlsIjoiYWRtaW5Abnh4bWRhdGEuY29tIiwiaWF0IjoxNzU2MTAyNzU2LCJleHAiOjE3NTYxODkxNTZ9.2Pq25hFiMiTyWB-GBdS5vIXOhI2He9oxjcuSDAytV50'
};
return storage[key] || null;
}
};
// 测试用户API调用
async function testUsersAPI() {
try {
console.log('测试前端用户API调用...');
console.log('=' .repeat(50));
// 模拟前端API调用
const API_BASE_URL = 'http://localhost:5350/api';
const token = mockLocalStorage.getItem('token');
console.log('Token存在:', !!token);
console.log('Token长度:', token ? token.length : 0);
if (!token) {
console.log('❌ 没有找到认证token');
return;
}
// 调用用户API
const response = await axios.get(`${API_BASE_URL}/users`, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
console.log('API响应状态:', response.status);
console.log('API响应成功:', response.data.success);
console.log('用户数据数量:', response.data.data ? response.data.data.length : 0);
if (response.data.success && response.data.data) {
console.log('✅ 前端可以正常获取用户数据');
console.log('用户列表:');
response.data.data.forEach((user, index) => {
console.log(` ${index + 1}. ${user.username} (${user.email}) - 角色: ${user.role}`);
});
} else {
console.log('❌ API返回数据格式异常');
console.log('响应数据:', JSON.stringify(response.data, null, 2));
}
} catch (error) {
console.log('❌ 前端API调用失败:', error.message);
if (error.response) {
console.log('错误状态码:', error.response.status);
console.log('错误响应:', error.response.data);
}
}
}
// 运行测试
testUsersAPI().catch(console.error);