|
|
@ -1349,11 +1349,14 @@ app.put('/api/supplies/:id', async (req, res) => { |
|
|
// 编辑货源API - /api/supplies/:id/edit
|
|
|
// 编辑货源API - /api/supplies/:id/edit
|
|
|
console.log('正在注册编辑货源API路由: /api/supplies/:id/edit'); |
|
|
console.log('正在注册编辑货源API路由: /api/supplies/:id/edit'); |
|
|
app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
console.log('收到编辑货源请求:', req.params.id, req.body); |
|
|
console.log('收到编辑货源请求:', req.params.id); |
|
|
|
|
|
console.log('请求体中的category:', req.body.category); |
|
|
|
|
|
console.log('请求体中的sourceType:', req.body.sourceType); |
|
|
|
|
|
console.log('请求体中的freshness:', req.body.freshness); |
|
|
try { |
|
|
try { |
|
|
const connection = await pool.getConnection(); |
|
|
const connection = await pool.getConnection(); |
|
|
const productId = req.params.id; |
|
|
const productId = req.params.id; |
|
|
const { productName, price, costprice, quantity, grossWeight, yolk, specification, supplyStatus, description, region, contactId, producting, imageUrls, autoOfflineTime } = req.body; |
|
|
const { productName, price, costprice, quantity, grossWeight, yolk, specification, supplyStatus, description, region, contactId, producting, imageUrls, autoOfflineTime, category, sourceType, freshness, autoOfflineHours } = req.body; |
|
|
|
|
|
|
|
|
// 开始事务
|
|
|
// 开始事务
|
|
|
await connection.beginTransaction(); |
|
|
await connection.beginTransaction(); |
|
|
@ -1397,7 +1400,7 @@ app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
|
|
|
|
|
|
// 处理媒体文件上传(图片和视频)
|
|
|
// 处理媒体文件上传(图片和视频)
|
|
|
let uploadedImageUrls = []; |
|
|
let uploadedImageUrls = []; |
|
|
if (Array.isArray(imageUrls) && imageUrls.length > 0) { |
|
|
if (imageUrls && Array.isArray(imageUrls) && imageUrls.length > 0) { |
|
|
console.log('开始处理编辑媒体文件上传,共', imageUrls.length, '个文件'); |
|
|
console.log('开始处理编辑媒体文件上传,共', imageUrls.length, '个文件'); |
|
|
|
|
|
|
|
|
for (const mediaUrl of imageUrls) { |
|
|
for (const mediaUrl of imageUrls) { |
|
|
@ -1439,14 +1442,6 @@ app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
console.log('媒体文件处理完成,成功上传', uploadedImageUrls.length, '个文件'); |
|
|
console.log('媒体文件处理完成,成功上传', uploadedImageUrls.length, '个文件'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果有新上传的图片,更新imageUrls字段
|
|
|
|
|
|
if (uploadedImageUrls.length > 0) { |
|
|
|
|
|
await connection.query( |
|
|
|
|
|
'UPDATE products SET imageUrls = ? WHERE id = ?', |
|
|
|
|
|
[JSON.stringify(uploadedImageUrls), productId] |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理数量:保存所有数量值,与规格保持一致
|
|
|
// 处理数量:保存所有数量值,与规格保持一致
|
|
|
let quantityValue = quantity; // 直接保存前端提交的数量字符串
|
|
|
let quantityValue = quantity; // 直接保存前端提交的数量字符串
|
|
|
// 如果是数字,转换为字符串
|
|
|
// 如果是数字,转换为字符串
|
|
|
@ -1457,26 +1452,66 @@ app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
quantityValue = quantity; |
|
|
quantityValue = quantity; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 只有当有新上传的图片时,才更新imageUrls,否则保留原有值
|
|
|
|
|
|
let imageUrlsToUpdate = null; |
|
|
|
|
|
if (uploadedImageUrls.length > 0) { |
|
|
|
|
|
imageUrlsToUpdate = JSON.stringify(uploadedImageUrls); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果没有新上传的图片,查询原有图片URL
|
|
|
|
|
|
const [existingProduct] = await connection.query( |
|
|
|
|
|
'SELECT imageUrls FROM products WHERE id = ?', |
|
|
|
|
|
[productId] |
|
|
|
|
|
); |
|
|
|
|
|
imageUrlsToUpdate = existingProduct[0].imageUrls; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 打印用于调试的更新参数
|
|
|
|
|
|
console.log('更新参数调试:'); |
|
|
|
|
|
console.log('productName:', productName); |
|
|
|
|
|
console.log('price:', (price || '').toString()); |
|
|
|
|
|
console.log('costprice:', costprice || ''); |
|
|
|
|
|
console.log('quantityValue:', quantityValue); |
|
|
|
|
|
console.log('grossWeight:', grossWeight); |
|
|
|
|
|
console.log('yolk:', yolk); |
|
|
|
|
|
console.log('specification:', specification); |
|
|
|
|
|
console.log('producting:', producting); |
|
|
|
|
|
console.log('supplyStatus:', supplyStatus); |
|
|
|
|
|
console.log('description:', description); |
|
|
|
|
|
console.log('region:', region); |
|
|
|
|
|
console.log('category:', category); |
|
|
|
|
|
console.log('sourceType:', sourceType); |
|
|
|
|
|
console.log('freshness:', freshness); |
|
|
|
|
|
console.log('productContact:', productContact); |
|
|
|
|
|
console.log('contactPhone:', contactPhone); |
|
|
|
|
|
console.log('imageUrlsToUpdate:', imageUrlsToUpdate); |
|
|
|
|
|
console.log('autoOfflineTime:', autoOfflineTime || null); |
|
|
|
|
|
console.log('autoOfflineHours:', autoOfflineHours || 24); |
|
|
|
|
|
console.log('productId:', productId); |
|
|
|
|
|
|
|
|
// 更新货源信息
|
|
|
// 更新货源信息
|
|
|
const updateQuery = ` |
|
|
const updateQuery = ` |
|
|
UPDATE products |
|
|
UPDATE products |
|
|
SET productName = ?, price = ?, costprice = ?, quantity = ?, grossWeight = ?, |
|
|
SET productName = ?, price = ?, costprice = ?, quantity = ?, grossWeight = ?, |
|
|
yolk = ?, specification = ?, producting = ?, supplyStatus = ?, description = ?, region = ?, |
|
|
yolk = ?, specification = ?, producting = ?, supplyStatus = ?, description = ?, region = ?, |
|
|
|
|
|
category = ?, sourceType = ?, freshness = ?, |
|
|
product_contact = ?, contact_phone = ?, imageUrls = ?, |
|
|
product_contact = ?, contact_phone = ?, imageUrls = ?, |
|
|
autoOfflineTime = ?, autoOfflineHours = ?, updated_at = ? |
|
|
autoOfflineTime = ?, autoOfflineHours = ?, updated_at = ? |
|
|
WHERE id = ? |
|
|
WHERE id = ? |
|
|
`;
|
|
|
`;
|
|
|
|
|
|
|
|
|
await connection.query(updateQuery, [ |
|
|
const [result] = await connection.query(updateQuery, [ |
|
|
productName, (price || '').toString(), costprice || '', quantityValue, grossWeight, |
|
|
productName, (price || '').toString(), costprice || '', quantityValue, grossWeight, |
|
|
yolk, specification, producting, supplyStatus, description, region, |
|
|
yolk, specification, producting, supplyStatus, description, region, |
|
|
productContact, contactPhone, JSON.stringify(uploadedImageUrls), |
|
|
category, sourceType, freshness, |
|
|
|
|
|
productContact, contactPhone, imageUrlsToUpdate, |
|
|
autoOfflineTime || null, // 自动下架时间
|
|
|
autoOfflineTime || null, // 自动下架时间
|
|
|
req.body.autoOfflineHours || 24, // 默认24小时
|
|
|
autoOfflineHours || 24, // 默认24小时
|
|
|
new Date(), // 更新updated_at字段
|
|
|
new Date(), // 更新updated_at字段
|
|
|
productId |
|
|
productId |
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
console.log('更新结果:', result); |
|
|
|
|
|
|
|
|
// 提交事务
|
|
|
// 提交事务
|
|
|
await connection.commit(); |
|
|
await connection.commit(); |
|
|
connection.release(); |
|
|
connection.release(); |
|
|
@ -1495,10 +1530,13 @@ app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
grossWeight, |
|
|
grossWeight, |
|
|
yolk, |
|
|
yolk, |
|
|
specification, |
|
|
specification, |
|
|
|
|
|
category, |
|
|
|
|
|
sourceType, |
|
|
|
|
|
freshness, |
|
|
|
|
|
producting, |
|
|
supplyStatus, |
|
|
supplyStatus, |
|
|
description, |
|
|
description, |
|
|
region, |
|
|
region, |
|
|
producting, |
|
|
|
|
|
imageUrls: uploadedImageUrls.length > 0 ? uploadedImageUrls : imageUrls |
|
|
imageUrls: uploadedImageUrls.length > 0 ? uploadedImageUrls : imageUrls |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|