You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.6 KiB

3 months ago
// 毛重字段(grossWeight)处理逻辑完整修复脚本
// 此脚本用于统一所有API接口对grossWeight字段的处理逻辑
const fs = require('fs');
const path = require('path');
// 定义配置
const config = {
serverFilePath: path.join(__dirname, 'server-mysql.js'),
backupFilePath: path.join(__dirname, 'server-mysql.js.bak.final-fix-' + Date.now()),
logFilePath: path.join(__dirname, 'final-fix-gross-weight-log.txt')
};
// 日志函数
function log(message) {
const timestamp = new Date().toISOString();
const logMessage = '[' + timestamp + '] ' + message;
console.log(logMessage);
try {
fs.appendFileSync(config.logFilePath, logMessage + '\n');
} catch (e) {
console.error('写入日志文件失败:', e.message);
}
}
// 读取文件内容
function readFile(filePath) {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (error) {
log('读取文件失败: ' + error.message);
throw error;
}
}
// 写入文件内容
function writeFile(filePath, content) {
try {
fs.writeFileSync(filePath, content, 'utf8');
log('文件已成功写入: ' + filePath);
} catch (error) {
log('写入文件失败: ' + error.message);
throw error;
}
}
// 创建备份文件
function createBackup() {
try {
const content = readFile(config.serverFilePath);
writeFile(config.backupFilePath, content);
log('已创建备份文件: ' + config.backupFilePath);
} catch (error) {
log('创建备份文件失败: ' + error.message);
throw error;
}
}
// 主函数
function main() {
log('===== 开始执行毛重字段处理逻辑完整修复 =====');
try {
// 创建备份
createBackup();
// 读取文件内容
let content = readFile(config.serverFilePath);
// 修复1: 统一中间件中的毛重处理逻辑,确保所有空值都设为5
const searchPatterns = [
'product.grossWeight = 0;',
'product.grossWeight = 0; // 空值设置为0'
];
let fixesApplied = 0;
searchPatterns.forEach(pattern => {
if (content.includes(pattern)) {
const originalCount = (content.match(new RegExp(pattern, 'g')) || []).length;
content = content.replace(new RegExp(pattern, 'g'), 'product.grossWeight = 5; // 空值设置为5');
const fixedCount = (content.match(/product\.grossWeight = 5;/g) || []).length - originalCount;
fixesApplied += fixedCount;
log('修复中间件中的毛重默认值: 替换了' + fixedCount + '处');
}
});
if (fixesApplied > 0) {
log('修复1完成: 已统一所有中间件中的毛重默认值为5');
} else {
log('修复1跳过: 所有中间件中的毛重默认值已经是5');
}
// 修复2: 在商品上传接口添加毛重处理逻辑
const uploadApiSearch = 'app.post(\'/api/products/upload\', async (req, res) => {';
if (content.includes(uploadApiSearch)) {
// 查找上传接口的位置
const uploadApiStart = content.indexOf(uploadApiSearch);
const uploadApiEnd = content.indexOf('});', uploadApiStart) + 3;
const uploadApiContent = content.substring(uploadApiStart, uploadApiEnd);
// 检查是否已经包含毛重处理逻辑
if (uploadApiContent.includes('grossWeight') && uploadApiContent.includes('parseFloat')) {
log('修复2跳过: 商品上传接口已经包含毛重处理逻辑');
} else {
// 查找商品数据处理的位置(在try块内)
const tryBlockStart = uploadApiContent.indexOf('try {');
const tryBlockEnd = uploadApiContent.lastIndexOf('} catch');
if (tryBlockStart !== -1 && tryBlockEnd !== -1) {
// 在try块开始处添加毛重处理逻辑
const tryBlockContent = uploadApiContent.substring(tryBlockStart, tryBlockEnd);
const weightHandlingCode = `try {\n // 修复毛重字段处理逻辑\n if (req.body && req.body.productData) {\n let processedGrossWeight = 5; // 默认值为5\n if (req.body.productData.grossWeight !== null && req.body.productData.grossWeight !== undefined && req.body.productData.grossWeight !== \'\') {\n const numValue = parseFloat(req.body.productData.grossWeight);\n if (!isNaN(numValue) && isFinite(numValue)) {\n processedGrossWeight = numValue;\n }\n }\n req.body.productData.grossWeight = processedGrossWeight;\n console.log(\'修复后 - 毛重值处理: 原始值=\' + (req.body.productData.grossWeight || \'undefined\') + ', 处理后=\' + processedGrossWeight);\n }`;
// 替换原代码
const fixedUploadApiContent = uploadApiContent.replace(tryBlockContent, weightHandlingCode);
content = content.replace(uploadApiContent, fixedUploadApiContent);
log('修复2完成: 在商品上传接口添加了毛重处理逻辑');
} else {
log('修复2失败: 无法在商品上传接口中找到try-catch块');
}
}
} else {
log('修复2跳过: 未找到商品上传接口');
}
// 写入修复后的内容
writeFile(config.serverFilePath, content);
log('===== 毛重字段处理逻辑完整修复完成 =====');
log('修复内容总结:');
log('1. 统一了所有中间件中的毛重默认值为5');
log('2. 在商品上传接口中添加了毛重处理逻辑,将空值设为5,有效数字转换为float类型');
log('3. 创建了备份文件,以便需要时恢复');
} catch (error) {
log('修复过程中发生错误: ' + error.message);
log('===== 毛重字段处理逻辑完整修复失败 =====');
process.exit(1);
}
}
// 执行主函数
main();