|
|
const axios = require('axios');
|
|
|
|
|
|
async function testAnalyticsAPI() {
|
|
|
try {
|
|
|
// 1. 登录获取token
|
|
|
console.log('1️⃣ 登录为admin用户...');
|
|
|
const loginRes = await axios.post('http://localhost:5000/api/auth/login', {
|
|
|
username: 'admin',
|
|
|
password: 'admin123'
|
|
|
});
|
|
|
|
|
|
if (!loginRes.data.success) {
|
|
|
console.error('❌ 登录失败:', loginRes.data.message);
|
|
|
process.exit(1);
|
|
|
}
|
|
|
|
|
|
const token = loginRes.data.data.token;
|
|
|
console.log('✅ 登录成功, Token:', token.substring(0, 20) + '...');
|
|
|
|
|
|
// 2. 测试 sales-trend API
|
|
|
console.log('\n2️⃣ 测试营业额趋势API...');
|
|
|
const today = new Date().toISOString().split('T')[0];
|
|
|
const start = new Date();
|
|
|
start.setDate(start.getDate() - 7);
|
|
|
const startDate = start.toISOString().split('T')[0];
|
|
|
|
|
|
console.log(`查询范围: ${startDate} 到 ${today}`);
|
|
|
|
|
|
const trendRes = await axios.get('http://localhost:5000/api/analytics/sales-trend', {
|
|
|
params: {
|
|
|
start_date: startDate,
|
|
|
end_date: today
|
|
|
},
|
|
|
headers: {
|
|
|
Authorization: `Bearer ${token}`
|
|
|
}
|
|
|
});
|
|
|
|
|
|
if (trendRes.data.success) {
|
|
|
console.log('✅ API调用成功!');
|
|
|
console.log('完整响应:', JSON.stringify(trendRes.data, null, 2));
|
|
|
|
|
|
const dataArray = Array.isArray(trendRes.data.data) ? trendRes.data.data :
|
|
|
Array.isArray(trendRes.data) ? trendRes.data : [];
|
|
|
|
|
|
console.log('返回数据条数:', dataArray.length);
|
|
|
if (dataArray.length > 0) {
|
|
|
console.log('\n数据样例:');
|
|
|
console.table(dataArray.slice(0, 5));
|
|
|
}
|
|
|
} else {
|
|
|
console.error('❌ API返回失败:', trendRes.data.message);
|
|
|
}
|
|
|
|
|
|
// 3. 测试 dashboard API
|
|
|
console.log('\n3️⃣ 测试仪表盘API...');
|
|
|
const dashboardRes = await axios.get('http://localhost:5000/api/analytics/dashboard', {
|
|
|
params: {
|
|
|
start_date: today,
|
|
|
end_date: today
|
|
|
},
|
|
|
headers: {
|
|
|
Authorization: `Bearer ${token}`
|
|
|
}
|
|
|
});
|
|
|
|
|
|
if (dashboardRes.data.success) {
|
|
|
console.log('✅ 仪表盘API成功!');
|
|
|
console.log('订单统计:', dashboardRes.data.data.orders);
|
|
|
}
|
|
|
|
|
|
console.log('\n🎉 所有测试通过!');
|
|
|
process.exit(0);
|
|
|
|
|
|
} catch (error) {
|
|
|
console.error('\n❌ 测试失败:');
|
|
|
if (error.response) {
|
|
|
console.error('状态码:', error.response.status);
|
|
|
console.error('错误信息:', error.response.data);
|
|
|
} else {
|
|
|
console.error('错误:', error.message);
|
|
|
}
|
|
|
process.exit(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
testAnalyticsAPI();
|