Files
services/scripts/fix-image-urls.js
T

66 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-05-24 10:34:02 +08:00
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;
2026-05-24 10:49:45 +08:00
// 替换所有 localhost:3000 和 localhost:3006
2026-05-24 10:34:02 +08:00
let newImages = oldImages
2026-05-24 14:32:35 +08:00
.replace(/http:\/\/localhost:3000/g, 'https://donghy.top')
.replace(/http:\/\/localhost:3006/g, 'https://donghy.top');
2026-05-24 10:34:02 +08:00
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
2026-05-24 14:32:35 +08:00
.replace(/http:\/\/localhost:3000/g, 'https://donghy.top')
.replace(/http:\/\/localhost:3006/g, 'https://donghy.top');
2026-05-24 10:34:02 +08:00
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();