66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
const config = require('../config/database');
|
|
|
|
async function fixImageUrls() {
|
|
try {
|
|
const connection = await mysql.createConnection(config);
|
|
console.log('✅ 数据库连接成功\n');
|
|
|
|
// 查找包含旧地址的记录
|
|
console.log('🔍 检查数据库中的图片URL...');
|
|
const [goods] = await connection.execute('SELECT id, images FROM goods WHERE images LIKE "%localhost%"');
|
|
|
|
console.log(`找到 ${goods.length} 条包含 localhost 的记录\n`);
|
|
|
|
// 更新图片URL
|
|
for (const item of goods) {
|
|
if (item.images) {
|
|
const oldImages = item.images;
|
|
// 替换所有 localhost:3000 和 localhost:3005
|
|
let newImages = oldImages
|
|
.replace(/http:\/\/localhost:3000/g, 'http://110.42.255.239:3005')
|
|
.replace(/http:\/\/localhost:3005/g, 'http://110.42.255.239:3005');
|
|
|
|
await connection.execute(
|
|
'UPDATE goods SET images = ? WHERE id = ?',
|
|
[newImages, item.id]
|
|
);
|
|
|
|
console.log(`商品ID ${item.id}:`);
|
|
console.log(` 旧: ${oldImages}`);
|
|
console.log(` 新: ${newImages}`);
|
|
console.log('---');
|
|
}
|
|
}
|
|
|
|
// 检查其他表(如有需要)
|
|
const [pointsGoods] = await connection.execute('SELECT id, images FROM points_goods WHERE images LIKE "%localhost%"');
|
|
if (pointsGoods.length > 0) {
|
|
console.log(`\n找到 ${pointsGoods.length} 条积分商品记录...`);
|
|
for (const item of pointsGoods) {
|
|
if (item.images) {
|
|
const oldImages = item.images;
|
|
let newImages = oldImages
|
|
.replace(/http:\/\/localhost:3000/g, 'http://110.42.255.239:3005')
|
|
.replace(/http:\/\/localhost:3005/g, 'http://110.42.255.239:3005');
|
|
|
|
await connection.execute(
|
|
'UPDATE points_goods SET images = ? WHERE id = ?',
|
|
[newImages, item.id]
|
|
);
|
|
|
|
console.log(`积分商品ID ${item.id}: 已更新`);
|
|
}
|
|
}
|
|
}
|
|
|
|
await connection.end();
|
|
console.log('\n✅ 所有图片URL更新完成!');
|
|
} catch (error) {
|
|
console.error('❌ 更新失败:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
fixImageUrls();
|