86 lines
3.1 KiB
JavaScript
86 lines
3.1 KiB
JavaScript
|
|
// 测试前端认证修复效果的脚本
|
|||
|
|
import axios from 'axios';
|
|||
|
|
import fs from 'fs';
|
|||
|
|
import path from 'path';
|
|||
|
|
|
|||
|
|
// 配置
|
|||
|
|
const BASE_URL = 'http://localhost:5352/api';
|
|||
|
|
const USERNAME = 'admin';
|
|||
|
|
const PASSWORD = '123456';
|
|||
|
|
|
|||
|
|
// 测试函数
|
|||
|
|
async function runTest() {
|
|||
|
|
console.log('===== 开始测试智慧耳标页面认证问题 =====');
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 1. 尝试登录获取token
|
|||
|
|
console.log('1. 尝试登录...');
|
|||
|
|
const loginResponse = await axios.post(`${BASE_URL}/auth/login`, {
|
|||
|
|
username: USERNAME,
|
|||
|
|
password: PASSWORD
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (loginResponse.data && loginResponse.data.token) {
|
|||
|
|
const token = loginResponse.data.token;
|
|||
|
|
console.log('登录成功,获取到token:', token);
|
|||
|
|
|
|||
|
|
// 2. 使用获取的token尝试访问智能耳标API
|
|||
|
|
console.log('2. 使用token访问智能耳标API...');
|
|||
|
|
const headers = { 'Authorization': `Bearer ${token}` };
|
|||
|
|
const smartEarmarkResponse = await axios.get(`${BASE_URL}/smart-earmark`, { headers });
|
|||
|
|
|
|||
|
|
console.log('智能耳标API访问成功,返回状态:', smartEarmarkResponse.status);
|
|||
|
|
console.log('返回数据示例:', JSON.stringify(smartEarmarkResponse.data.slice(0, 1), null, 2));
|
|||
|
|
|
|||
|
|
console.log('\n===== 测试成功!智慧耳标页面认证问题已修复 =====');
|
|||
|
|
} else {
|
|||
|
|
console.error('登录失败,未获取到token:', loginResponse.data);
|
|||
|
|
|
|||
|
|
// 如果后端使用模拟数据,我们也创建一个模拟token来测试
|
|||
|
|
console.log('\n3. 尝试使用模拟token访问API(适用于前端模拟登录场景)...');
|
|||
|
|
const mockToken = 'mock-jwt-token-' + Date.now();
|
|||
|
|
const headers = { 'Authorization': `Bearer ${mockToken}` };
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const smartEarmarkResponse = await axios.get(`${BASE_URL}/smart-earmark`, {
|
|||
|
|
headers,
|
|||
|
|
timeout: 5000
|
|||
|
|
});
|
|||
|
|
console.log('使用模拟token访问成功,状态:', smartEarmarkResponse.status);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('使用模拟token访问失败:', error.code || error.message);
|
|||
|
|
if (error.response) {
|
|||
|
|
console.error('错误详情:', error.response.status, error.response.data);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 记录问题以便后续分析
|
|||
|
|
const errorInfo = {
|
|||
|
|
timestamp: new Date().toISOString(),
|
|||
|
|
error: error.message,
|
|||
|
|
response: error.response ? {
|
|||
|
|
status: error.response.status,
|
|||
|
|
data: error.response.data
|
|||
|
|
} : null
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
fs.writeFileSync(
|
|||
|
|
path.join(__dirname, 'auth_error.log'),
|
|||
|
|
JSON.stringify(errorInfo, null, 2) + '\n',
|
|||
|
|
{ flag: 'a' }
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('测试过程中发生错误:', error.message);
|
|||
|
|
if (error.response) {
|
|||
|
|
console.error('错误状态码:', error.response.status);
|
|||
|
|
console.error('错误详情:', error.response.data);
|
|||
|
|
} else if (error.request) {
|
|||
|
|
console.error('未收到响应:', error.request);
|
|||
|
|
console.error('请检查后端服务是否正常运行在端口5352');
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 运行测试
|
|||
|
|
runTest();
|