110 lines
3.2 KiB
JavaScript
110 lines
3.2 KiB
JavaScript
// 测试KOL合作转换漏斗API的脚本,使用模拟的项目ID
|
||
const http = require('http');
|
||
|
||
// 模拟的项目ID列表
|
||
const MOCK_PROJECT_IDS = [
|
||
'1', // 简单数字ID
|
||
'test-project-id', // 测试项目ID
|
||
'550e8400-e29b-41d4-a716-446655440000', // UUID格式
|
||
];
|
||
|
||
// 简单的HTTP请求函数
|
||
function httpRequest(url, options = {}) {
|
||
return new Promise((resolve, reject) => {
|
||
const req = http.request(url, options, (res) => {
|
||
let data = '';
|
||
|
||
res.on('data', (chunk) => {
|
||
data += chunk;
|
||
});
|
||
|
||
res.on('end', () => {
|
||
try {
|
||
const jsonData = JSON.parse(data);
|
||
resolve({ statusCode: res.statusCode, data: jsonData });
|
||
} catch (error) {
|
||
resolve({ statusCode: res.statusCode, data });
|
||
}
|
||
});
|
||
});
|
||
|
||
req.on('error', (error) => {
|
||
reject(error);
|
||
});
|
||
|
||
if (options.body) {
|
||
req.write(JSON.stringify(options.body));
|
||
}
|
||
|
||
req.end();
|
||
});
|
||
}
|
||
|
||
// 测试KOL合作转换漏斗API
|
||
async function testConversionFunnelAPI(projectId) {
|
||
console.log(`测试KOL合作转换漏斗API,项目ID: ${projectId}...`);
|
||
|
||
try {
|
||
const url = `http://localhost:4000/api/analytics/project/${projectId}/conversion-funnel`;
|
||
console.log(`请求URL: ${url}`);
|
||
|
||
const response = await httpRequest(url);
|
||
|
||
console.log(`状态码: ${response.statusCode}`);
|
||
console.log('API响应:');
|
||
console.log(JSON.stringify(response.data, null, 2));
|
||
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error('测试API失败:', error.message);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// 检查后端服务器是否正在运行
|
||
async function checkServerRunning() {
|
||
try {
|
||
const response = await httpRequest('http://localhost:4000/health', { method: 'GET' });
|
||
return response.statusCode === 200;
|
||
} catch (error) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 主函数
|
||
async function main() {
|
||
try {
|
||
// 检查服务器是否运行
|
||
const isServerRunning = await checkServerRunning();
|
||
|
||
if (!isServerRunning) {
|
||
console.log('后端服务器未运行,请先启动服务器');
|
||
console.log('运行命令: cd /Users/liam/code/promote/backend && npm run dev');
|
||
process.exit(1);
|
||
}
|
||
|
||
console.log('后端服务器正在运行,开始测试漏斗接口...\n');
|
||
|
||
// 测试所有模拟的项目ID
|
||
for (const projectId of MOCK_PROJECT_IDS) {
|
||
console.log(`\n===== 测试项目ID: ${projectId} =====\n`);
|
||
await testConversionFunnelAPI(projectId);
|
||
}
|
||
|
||
console.log('\n测试完成!');
|
||
console.log('\n提示: 如果所有测试都返回404错误,说明服务器无法找到项目。');
|
||
console.log('这可能是因为:');
|
||
console.log('1. 数据库中没有这些项目ID');
|
||
console.log('2. 无法连接到数据库');
|
||
console.log('3. 漏斗接口没有实现模拟数据功能');
|
||
console.log('\n建议:');
|
||
console.log('1. 在前端代码中使用硬编码的项目ID: "1"');
|
||
console.log('2. 修改漏斗接口,在找不到项目时返回模拟数据');
|
||
} catch (error) {
|
||
console.error('测试过程中出错:', error.message);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 运行主函数
|
||
main();
|