// 测试并监控日志 const { spawn } = require('child_process'); const fs = require('fs'); const path = require('path'); // 生成有效的JWT token function generateTestToken() { const crypto = require('crypto'); const header = Buffer.from(JSON.stringify({ alg: 'HS256', typ: 'JWT' })).toString('base64url'); const payload = Buffer.from(JSON.stringify({ id: 1, username: 'test', tenantId: 1, iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + 3600 })).toString('base64url'); const secret = 'your-secret-key'; const signature = crypto .createHmac('sha256', secret) .update(`${header}.${payload}`) .digest('base64url'); return `${header}.${payload}.${signature}`; } async function testAndMonitor() { console.log('=== 测试支付成功触发API并监控日志 ===\n'); const token = generateTestToken(); const apiUrl = 'http://localhost:8080/api/v1/payments/payment/trigger-success'; const testOrderId = 25; // 创建一个日志文件来记录 const logFile = path.join(__dirname, 'test-log.txt'); console.log(`日志将记录到: ${logFile}\n`); // 开始监控应用程序日志 const appPid = 16074; // 现有的应用程序进程ID console.log(`监控应用程序进程ID: ${appPid}`); // 测试API console.log(`测试API: ${apiUrl}`); console.log(`订单ID: ${testOrderId}`); console.log(`Token: ${token.substring(0, 30)}...\n`); try { const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ orderId: testOrderId }) }); console.log(`状态码: ${response.status}`); console.log(`状态文本: ${response.statusText}`); if (response.ok) { const result = await response.json(); console.log('成功响应:', JSON.stringify(result, null, 2)); console.log(`\n✅ API调用成功`); // 等待一会儿,让应用程序处理 console.log('\n等待3秒让应用程序处理...'); await new Promise(resolve => setTimeout(resolve, 3000)); // 检查数据库 console.log('\n检查数据库...'); const { exec } = require('child_process'); exec( 'psql -h 127.0.0.1 -U postgres -d postgres -c "SELECT id, tenant_id, task_id, order_id, print_status, scheduled_at, created_at FROM feie_print_task_mt WHERE order_id = 25 ORDER BY created_at DESC"', (error, stdout, stderr) => { if (error) { console.error('查询数据库失败:', error.message); } else { console.log('订单ID 25的打印任务:'); console.log(stdout); if (stdout.includes('(0 rows)')) { console.log('❌ 数据库中没有找到打印任务'); console.log('\n可能的原因:'); console.log('1. 飞鹅打印模块导入失败'); console.log('2. PrintTriggerService处理失败'); console.log('3. 数据库插入失败'); console.log('4. 事务回滚'); } else { console.log('✅ 数据库中找到打印任务'); } } } ); } else { const errorText = await response.text(); console.log('错误响应:', errorText); console.log(`\n❌ API调用失败`); } } catch (error) { console.error('请求失败:', error.message); } // 检查应用程序的stderr输出 console.log('\n=== 检查应用程序日志 ==='); console.log('尝试读取应用程序的stderr...'); // 尝试通过/proc文件系统读取进程的输出 const stderrFile = `/proc/${appPid}/fd/2`; if (fs.existsSync(stderrFile)) { try { const stderrContent = fs.readFileSync(stderrFile, 'utf8'); console.log('应用程序stderr输出:'); console.log(stderrContent.substring(-500)); // 最后500个字符 } catch (err) { console.log('无法读取stderr:', err.message); } } else { console.log('stderr文件不存在'); } // 检查系统日志 console.log('\n=== 检查系统日志 ==='); exec('dmesg | tail -20', (error, stdout, stderr) => { if (!error) { console.log('系统日志最后20行:'); console.log(stdout); } }); } // 运行测试 testAndMonitor().catch(error => { console.error('测试脚本执行失败:', error); });