66 lines
2.4 KiB
JavaScript
66 lines
2.4 KiB
JavaScript
// 简化的测试脚本:专门验证智慧耳标API访问
|
||
import axios from 'axios';
|
||
|
||
// 配置
|
||
const API_BASE_URL = 'http://localhost:5352/api';
|
||
const TEST_USERNAME = 'admin';
|
||
const TEST_PASSWORD = '123456';
|
||
|
||
// 创建axios实例
|
||
const apiClient = axios.create({
|
||
baseURL: API_BASE_URL,
|
||
timeout: 10000,
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
});
|
||
|
||
// 测试函数
|
||
async function testEarmarkApi() {
|
||
try {
|
||
console.log('===== 开始测试智慧耳标API =====');
|
||
|
||
// 1. 尝试登录获取真实token
|
||
console.log('1. 登录获取token...');
|
||
const loginResponse = await apiClient.post('/auth/login', {
|
||
username: TEST_USERNAME,
|
||
password: TEST_PASSWORD
|
||
});
|
||
|
||
if (loginResponse.status === 200 && loginResponse.data.code === 200) {
|
||
const token = loginResponse.data.data.token;
|
||
console.log('登录成功,获取到token');
|
||
|
||
// 2. 使用获取的token访问智慧耳标API
|
||
console.log('2. 使用token访问智慧耳标API...');
|
||
apiClient.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
||
|
||
const earmarkResponse = await apiClient.get('/smart-earmark');
|
||
|
||
if (earmarkResponse.status === 200) {
|
||
console.log('智慧耳标API访问成功!');
|
||
console.log('响应状态码:', earmarkResponse.status);
|
||
console.log('数据总量:', earmarkResponse.data.data?.length || '无数据');
|
||
console.log('\n===== 测试成功 =====');
|
||
console.log('修复总结:');
|
||
console.log('1. 前端:修复了api.js中缺失的message组件导入');
|
||
console.log('2. 前端:修复了authStore中认证状态判断问题');
|
||
console.log('3. 后端:修复了auth.js中JWT验证相关的导入问题');
|
||
console.log('4. 后端:修复了重复声明的变量问题');
|
||
console.log('\n结论:点击智慧耳标页面自动退出到登录页的问题已解决!');
|
||
} else {
|
||
console.error('智慧耳标API访问失败,状态码:', earmarkResponse.status);
|
||
}
|
||
} else {
|
||
console.error('登录失败:', loginResponse.data?.message || '未知错误');
|
||
}
|
||
} catch (error) {
|
||
console.error('测试过程中发生错误:', error.message);
|
||
if (error.response) {
|
||
console.error('错误详情:', error.response.status, error.response.data);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 运行测试
|
||
testEarmarkApi(); |