111 lines
2.8 KiB
JavaScript
111 lines
2.8 KiB
JavaScript
// 测试KOL合作转换漏斗API的脚本
|
||
const { exec } = require('child_process');
|
||
const http = require('http');
|
||
const https = require('https');
|
||
const { URL } = require('url');
|
||
|
||
// 测试项目ID - 可以手动设置一个已知的项目ID
|
||
const TEST_PROJECT_ID = '1'; // 替换为实际的项目ID
|
||
|
||
// 简单的HTTP请求函数
|
||
function httpRequest(url, options = {}) {
|
||
return new Promise((resolve, reject) => {
|
||
const parsedUrl = new URL(url);
|
||
const protocol = parsedUrl.protocol === 'https:' ? https : http;
|
||
|
||
const req = protocol.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);
|
||
|
||
if (response.statusCode !== 200) {
|
||
throw new Error(`API请求失败: ${response.statusCode}`);
|
||
}
|
||
|
||
console.log('API响应:');
|
||
console.log(JSON.stringify(response.data, null, 2));
|
||
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error('测试API失败:', error.message);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 检查后端服务器是否正在运行
|
||
function checkServerRunning() {
|
||
return new Promise((resolve, reject) => {
|
||
const req = http.request('http://localhost:4000/health', { method: 'GET' }, (res) => {
|
||
if (res.statusCode === 200) {
|
||
resolve(true);
|
||
} else {
|
||
resolve(false);
|
||
}
|
||
});
|
||
|
||
req.on('error', () => {
|
||
resolve(false);
|
||
});
|
||
|
||
req.end();
|
||
});
|
||
}
|
||
|
||
// 主函数
|
||
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);
|
||
}
|
||
|
||
// 测试API
|
||
await testConversionFunnelAPI(TEST_PROJECT_ID);
|
||
|
||
console.log('测试完成!');
|
||
} catch (error) {
|
||
console.error('测试过程中出错:', error.message);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 运行主函数
|
||
main();
|