72 lines
2.3 KiB
JavaScript
72 lines
2.3 KiB
JavaScript
import { Client } from 'ssh2';
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('=== 修复重复订单ID ===\n');
|
|
|
|
console.log('1. 获取完整订单数据:');
|
|
conn.exec('curl -s http://localhost:4000/api/orders', (err, stream) => {
|
|
if (err) { console.log('ERR:', err); conn.end(); return; }
|
|
let output = '';
|
|
stream.on('data', (d) => output += d.toString());
|
|
stream.on('close', () => {
|
|
try {
|
|
const result = JSON.parse(output);
|
|
const orders = result.data || [];
|
|
|
|
console.log(`总订单数: ${orders.length}`);
|
|
|
|
const ids = orders.map(o => o.id);
|
|
const uniqueIds = [...new Set(ids)];
|
|
|
|
if (uniqueIds.length !== ids.length) {
|
|
console.log('\n⚠️ 发现重复订单ID!');
|
|
const duplicates = ids.filter((id, index) => ids.indexOf(id) !== index);
|
|
console.log('重复的ID:', [...new Set(duplicates)]);
|
|
|
|
console.log('\n重新编号订单...');
|
|
orders.forEach((order, idx) => {
|
|
order.id = (idx + 1).toString();
|
|
});
|
|
|
|
console.log('新订单ID列表:', orders.map(o => o.id).join(', '));
|
|
|
|
const updatedData = JSON.stringify(orders, null, 2);
|
|
conn.exec(`echo '${updatedData}' > /www/huishou/server/data/orders.json`, (err, stream2) => {
|
|
if (err) { console.log('ERR:', err); conn.end(); return; }
|
|
stream2.on('close', () => {
|
|
console.log('\n数据文件已更新!');
|
|
console.log('重启容器...');
|
|
conn.exec('docker restart huishou-backend', (err, stream3) => {
|
|
if (err) { console.log('ERR:', err); conn.end(); return; }
|
|
stream3.on('close', () => {
|
|
console.log('容器已重启!');
|
|
conn.end();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
} else {
|
|
console.log('\n✅ 没有重复订单ID');
|
|
conn.end();
|
|
}
|
|
} catch (e) {
|
|
console.log('解析错误:', e.message);
|
|
console.log('输出:', output);
|
|
conn.end();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.log('连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect({
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
}); |