const { Client } = require('ssh2'); const conn = new Client(); conn.on('ready', () => { console.log('=== 读取订单数据 ==='); conn.exec('cat /www/huishou/server/data/orders.json', (err, stream) => { if (err) { console.log('ERR:', err); conn.end(); return; } let data = ''; stream.on('data', (d) => data += d.toString()); stream.on('close', () => { try { let orders = JSON.parse(data); console.log('订单数量:', orders.length); let maxId = 0; orders.forEach(o => { const numId = parseInt(o.id); if (!isNaN(numId) && numId <= 100) { maxId = Math.max(maxId, numId); } }); orders.forEach(o => { const numId = parseInt(o.id); if (!isNaN(numId) && numId > 100) { maxId++; o.id = maxId.toString(); } }); const fixedData = JSON.stringify(orders, null, 2); console.log('修复完成,新订单ID列表:', orders.map(o => o.id).slice(0, 5), '...'); console.log('\n=== 写入修复后的数据 ==='); conn.exec(`cat > /www/huishou/server/data/orders.json << 'EOF'\n${fixedData}\nEOF`, (err2, stream2) => { if (err2) { console.log('ERR2:', err2); conn.end(); return; } stream2.on('close', () => { console.log('\n=== 验证修复结果 ==='); conn.exec('cat /www/huishou/server/data/orders.json | head -50', (err3, stream3) => { if (err3) { console.log('ERR3:', err3); conn.end(); return; } let result = ''; stream3.on('data', (d) => result += d.toString()); stream3.on('close', () => { console.log('验证结果:', result); console.log('\n=== 重新构建后端容器 ==='); conn.exec('cd /www/huishou && docker-compose down && docker-compose build --no-cache backend && docker-compose up -d', (err4, stream4) => { if (err4) { console.log('ERR4:', err4); conn.end(); return; } stream4.on('data', (d) => process.stdout.write(d.toString())); stream4.on('close', () => { setTimeout(() => { console.log('\n=== 测试订单列表API ==='); conn.exec('curl -s http://127.0.0.1:4000/api/orders', (err5, stream5) => { if (err5) { console.log('ERR5:', err5); conn.end(); return; } let apiResult = ''; stream5.on('data', (d) => apiResult += d.toString()); stream5.on('close', () => { const result = JSON.parse(apiResult); console.log('订单数量:', result.length); if (result.length > 0) { console.log('第一条订单:', JSON.stringify(result[0], null, 2)); } conn.end(); }); }); }, 10000); }); }); }); }); }); }); } catch (e) { console.log('解析错误:', e); conn.end(); } }); }); }); conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' });