|
|
|
@ -21,27 +21,29 @@ app.use((req, res, next) => { |
|
|
|
app.use(bodyParser.json({ limit: '10mb' })); |
|
|
|
app.use(express.static(path.join(__dirname))); |
|
|
|
|
|
|
|
// 数据库配置
|
|
|
|
// 数据库配置 - 从环境变量获取,与docker-compose.yml保持一致
|
|
|
|
const dbConfig = { |
|
|
|
host: '1.95.162.61', |
|
|
|
user: 'root', |
|
|
|
password: 'schl@2025', // 请替换为实际的数据库密码
|
|
|
|
database: 'wechat_app', // 连接到wechat_app数据库
|
|
|
|
host: process.env.DB_HOST || '1.95.162.61', |
|
|
|
user: process.env.DB_USER || 'root', |
|
|
|
password: process.env.DB_PASSWORD || 'schl@2025', |
|
|
|
database: process.env.DB_NAME || 'wechat_app', |
|
|
|
waitForConnections: true, |
|
|
|
connectionLimit: 10, |
|
|
|
connectionLimit: 20, // 增加连接池大小,提高并发处理能力
|
|
|
|
queueLimit: 0, |
|
|
|
connectTimeout: 10000, // 增加连接超时时间(mysql2支持的选项)
|
|
|
|
timezone: '+08:00' // 设置为北京时间时区
|
|
|
|
}; |
|
|
|
|
|
|
|
// userlogin数据库配置
|
|
|
|
// userlogin数据库配置 - 从环境变量获取
|
|
|
|
const userLoginDbConfig = { |
|
|
|
host: '1.95.162.61', |
|
|
|
user: 'root', |
|
|
|
password: 'schl@2025', // 请替换为实际的数据库密码
|
|
|
|
database: 'userlogin', // 连接到userlogin数据库
|
|
|
|
host: process.env.DB_HOST || '1.95.162.61', |
|
|
|
user: process.env.DB_USER || 'root', |
|
|
|
password: process.env.DB_PASSWORD || 'schl@2025', |
|
|
|
database: process.env.USER_LOGIN_DB_NAME || 'userlogin', |
|
|
|
waitForConnections: true, |
|
|
|
connectionLimit: 10, |
|
|
|
connectionLimit: 20, // 增加连接池大小
|
|
|
|
queueLimit: 0, |
|
|
|
connectTimeout: 10000, // 增加连接超时时间(mysql2支持的选项)
|
|
|
|
timezone: '+08:00' // 设置为北京时间时区
|
|
|
|
}; |
|
|
|
|
|
|
|
@ -813,7 +815,10 @@ app.post('/api/supplies/create', async (req, res) => { |
|
|
|
imageUrls: uploadedImageUrls.length > 0 ? JSON.stringify(uploadedImageUrls) : '[]', |
|
|
|
created_at: new Date(), |
|
|
|
product_contact: productContact, // 添加联系人名称
|
|
|
|
contact_phone: contactPhone // 添加联系人电话
|
|
|
|
contact_phone: contactPhone, // 添加联系人电话
|
|
|
|
autoOfflineTime: req.body.autoOfflineTime, // 自动下架时间
|
|
|
|
autoOfflineDays: req.body.autoOfflineDays, // 自动下架天数
|
|
|
|
autoOfflineHours: req.body.autoOfflineHours // 自动下架小时数
|
|
|
|
}; |
|
|
|
|
|
|
|
// 插入商品数据
|
|
|
|
@ -826,21 +831,21 @@ app.post('/api/supplies/create', async (req, res) => { |
|
|
|
|
|
|
|
if (columns.length === 0) { |
|
|
|
// 没有quality字段,不包含quality字段的插入
|
|
|
|
insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, price, quantity, grossWeight, yolk, specification, producting, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; |
|
|
|
insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, price, quantity, grossWeight, yolk, specification, producting, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone, autoOfflineTime, autoOfflineDays, autoOfflineHours) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; |
|
|
|
insertParams = [ |
|
|
|
productId, productData.sellerId, productName, category || '', price.toString(), quantity, grossWeight, |
|
|
|
yolk, specification, producting, region, productData.status, productData.supplyStatus, productData.sourceType, |
|
|
|
productData.description, productData.rejectReason, productData.imageUrls, new Date(), new Date(), |
|
|
|
productContact, contactPhone // 添加联系人信息
|
|
|
|
productContact, contactPhone, req.body.autoOfflineTime, req.body.autoOfflineDays, req.body.autoOfflineHours // 添加联系人信息和自动下架时间、小时数
|
|
|
|
]; |
|
|
|
} else { |
|
|
|
// 有quality字段,包含quality字段的插入
|
|
|
|
insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, price, quantity, grossWeight, yolk, specification, producting, quality, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; |
|
|
|
insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, price, quantity, grossWeight, yolk, specification, producting, quality, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone, autoOfflineTime, autoOfflineDays, autoOfflineHours) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; |
|
|
|
insertParams = [ |
|
|
|
productId, productData.sellerId, productName, category || '', price.toString(), quantity, grossWeight, |
|
|
|
yolk, specification, producting, quality, region, productData.status, productData.supplyStatus, |
|
|
|
productData.sourceType, productData.description, productData.rejectReason, productData.imageUrls, |
|
|
|
new Date(), new Date(), productContact, contactPhone // 添加联系人信息
|
|
|
|
new Date(), new Date(), productContact, contactPhone, req.body.autoOfflineTime, req.body.autoOfflineDays, req.body.autoOfflineHours // 添加联系人信息和自动下架时间、小时数
|
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
@ -978,9 +983,9 @@ app.post('/api/supplies/:id/publish', async (req, res) => { |
|
|
|
return sendResponse(res, false, null, '货源不存在'); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新状态为已发布(直接上架,不需要审核)
|
|
|
|
// 更新状态为已发布(直接上架,不需要审核),同时更新updated_at和autoOfflineHours
|
|
|
|
await connection.query( |
|
|
|
'UPDATE products SET status = ? WHERE id = ?', |
|
|
|
'UPDATE products SET status = ?, updated_at = NOW() WHERE id = ?', |
|
|
|
['published', productId] |
|
|
|
); |
|
|
|
|
|
|
|
@ -1037,6 +1042,54 @@ app.post('/api/supplies/:id/delete', async (req, res) => { |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
// 更新货源状态API - /api/supplies/:id
|
|
|
|
console.log('正在注册更新货源状态API路由: /api/supplies/:id'); |
|
|
|
app.put('/api/supplies/:id', async (req, res) => { |
|
|
|
console.log('收到更新货源状态请求:', req.params.id, req.body); |
|
|
|
try { |
|
|
|
const connection = await pool.getConnection(); |
|
|
|
const productId = req.params.id; |
|
|
|
const { status } = req.body; |
|
|
|
|
|
|
|
// 验证状态参数
|
|
|
|
if (!status) { |
|
|
|
connection.release(); |
|
|
|
return sendResponse(res, false, null, '状态不能为空'); |
|
|
|
} |
|
|
|
|
|
|
|
// 开始事务
|
|
|
|
await connection.beginTransaction(); |
|
|
|
|
|
|
|
// 检查当前状态
|
|
|
|
const [currentProduct] = await connection.query( |
|
|
|
'SELECT status FROM products WHERE id = ?', |
|
|
|
[productId] |
|
|
|
); |
|
|
|
|
|
|
|
if (currentProduct.length === 0) { |
|
|
|
await connection.rollback(); |
|
|
|
connection.release(); |
|
|
|
return sendResponse(res, false, null, '货源不存在'); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新状态
|
|
|
|
await connection.query( |
|
|
|
'UPDATE products SET status = ? WHERE id = ?', |
|
|
|
[status, productId] |
|
|
|
); |
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
await connection.commit(); |
|
|
|
connection.release(); |
|
|
|
|
|
|
|
sendResponse(res, true, null, '货源状态更新成功'); |
|
|
|
} catch (error) { |
|
|
|
console.error('更新货源状态失败:', error.message); |
|
|
|
console.error('错误详情:', error); |
|
|
|
sendResponse(res, false, null, '更新货源状态失败'); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
// 编辑货源API - /api/supplies/:id/edit
|
|
|
|
console.log('正在注册编辑货源API路由: /api/supplies/:id/edit'); |
|
|
|
app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
|
@ -1112,19 +1165,34 @@ app.put('/api/supplies/:id/edit', async (req, res) => { |
|
|
|
console.log('图片处理完成,成功上传', uploadedImageUrls.length, '张图片'); |
|
|
|
} |
|
|
|
|
|
|
|
// 处理数量:由于数据库中quantity是int类型,只保存第一个数量值
|
|
|
|
let quantityValue = 0; |
|
|
|
if (quantity) { |
|
|
|
if (typeof quantity === 'string' && quantity.includes(',')) { |
|
|
|
// 逗号分隔的多个数量,只取第一个
|
|
|
|
quantityValue = parseInt(quantity.split(',')[0].trim()) || 0; |
|
|
|
} else { |
|
|
|
quantityValue = parseInt(quantity) || 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 更新货源信息
|
|
|
|
const updateQuery = ` |
|
|
|
UPDATE products |
|
|
|
SET productName = ?, price = ?, quantity = ?, grossWeight = ?, |
|
|
|
yolk = ?, specification = ?, producting = ?, supplyStatus = ?, description = ?, region = ?, |
|
|
|
product_contact = ?, contact_phone = ?, imageUrls = ? |
|
|
|
product_contact = ?, contact_phone = ?, imageUrls = ?, |
|
|
|
autoOfflineHours = ?, updated_at = ? |
|
|
|
WHERE id = ? |
|
|
|
`;
|
|
|
|
|
|
|
|
await connection.query(updateQuery, [ |
|
|
|
productName, price.toString(), parseInt(quantity), grossWeight, |
|
|
|
productName, price.toString(), quantityValue, grossWeight, |
|
|
|
yolk, specification, producting, supplyStatus, description, region, |
|
|
|
productContact, contactPhone, JSON.stringify(uploadedImageUrls), productId |
|
|
|
productContact, contactPhone, JSON.stringify(uploadedImageUrls), |
|
|
|
req.body.autoOfflineHours || 24, // 默认24小时
|
|
|
|
new Date(), // 更新updated_at字段
|
|
|
|
productId |
|
|
|
]); |
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
@ -1323,6 +1391,102 @@ async function ensureDatabaseSchema() { |
|
|
|
); |
|
|
|
console.log('rejectReason字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有autoOfflineTime字段
|
|
|
|
console.log('检查表products是否有autoOfflineTime字段...'); |
|
|
|
const [autoOfflineTimeColumns] = await connection.query( |
|
|
|
'SHOW COLUMNS FROM `products` LIKE ?', |
|
|
|
['autoOfflineTime'] |
|
|
|
); |
|
|
|
console.log('检查表字段结果:', autoOfflineTimeColumns.length > 0 ? '已存在' : '不存在'); |
|
|
|
|
|
|
|
if (autoOfflineTimeColumns.length === 0) { |
|
|
|
console.log('添加autoOfflineTime字段到products表...'); |
|
|
|
await connection.query( |
|
|
|
'ALTER TABLE `products` ADD COLUMN autoOfflineTime DATETIME COMMENT "自动下架时间"' |
|
|
|
); |
|
|
|
console.log('autoOfflineTime字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有autoOfflineDays字段
|
|
|
|
console.log('检查表products是否有autoOfflineDays字段...'); |
|
|
|
const [autoOfflineDaysColumns] = await connection.query( |
|
|
|
'SHOW COLUMNS FROM `products` LIKE ?', |
|
|
|
['autoOfflineDays'] |
|
|
|
); |
|
|
|
console.log('检查表字段结果:', autoOfflineDaysColumns.length > 0 ? '已存在' : '不存在'); |
|
|
|
|
|
|
|
if (autoOfflineDaysColumns.length === 0) { |
|
|
|
console.log('添加autoOfflineDays字段到products表...'); |
|
|
|
await connection.query( |
|
|
|
'ALTER TABLE `products` ADD COLUMN autoOfflineDays INT COMMENT "自动下架天数"' |
|
|
|
); |
|
|
|
console.log('autoOfflineDays字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有autoOfflineHours字段
|
|
|
|
console.log('检查表products是否有autoOfflineHours字段...'); |
|
|
|
const [autoOfflineHoursColumns] = await connection.query( |
|
|
|
'SHOW COLUMNS FROM `products` LIKE ?', |
|
|
|
['autoOfflineHours'] |
|
|
|
); |
|
|
|
console.log('检查表字段结果:', autoOfflineHoursColumns.length > 0 ? '已存在' : '不存在'); |
|
|
|
|
|
|
|
if (autoOfflineHoursColumns.length === 0) { |
|
|
|
console.log('添加autoOfflineHours字段到products表...'); |
|
|
|
await connection.query( |
|
|
|
'ALTER TABLE `products` ADD COLUMN autoOfflineHours FLOAT COMMENT "自动下架小时数"' |
|
|
|
); |
|
|
|
console.log('autoOfflineHours字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有supplyStatus字段
|
|
|
|
console.log('检查表products是否有supplyStatus字段...'); |
|
|
|
const [supplyStatusColumns] = await connection.query( |
|
|
|
'SHOW COLUMNS FROM `products` LIKE ?', |
|
|
|
['supplyStatus'] |
|
|
|
); |
|
|
|
console.log('检查表字段结果:', supplyStatusColumns.length > 0 ? '已存在' : '不存在'); |
|
|
|
|
|
|
|
if (supplyStatusColumns.length === 0) { |
|
|
|
console.log('添加supplyStatus字段到products表...'); |
|
|
|
await connection.query( |
|
|
|
'ALTER TABLE `products` ADD COLUMN supplyStatus VARCHAR(50) COMMENT "货源状态"' |
|
|
|
); |
|
|
|
console.log('supplyStatus字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有sourceType字段
|
|
|
|
console.log('检查表products是否有sourceType字段...'); |
|
|
|
const [sourceTypeColumns] = await connection.query( |
|
|
|
'SHOW COLUMNS FROM `products` LIKE ?', |
|
|
|
['sourceType'] |
|
|
|
); |
|
|
|
console.log('检查表字段结果:', sourceTypeColumns.length > 0 ? '已存在' : '不存在'); |
|
|
|
|
|
|
|
if (sourceTypeColumns.length === 0) { |
|
|
|
console.log('添加sourceType字段到products表...'); |
|
|
|
await connection.query( |
|
|
|
'ALTER TABLE `products` ADD COLUMN sourceType VARCHAR(50) COMMENT "货源类型"' |
|
|
|
); |
|
|
|
console.log('sourceType字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有category字段
|
|
|
|
console.log('检查表products是否有category字段...'); |
|
|
|
const [categoryColumns] = await connection.query( |
|
|
|
'SHOW COLUMNS FROM `products` LIKE ?', |
|
|
|
['category'] |
|
|
|
); |
|
|
|
console.log('检查表字段结果:', categoryColumns.length > 0 ? '已存在' : '不存在'); |
|
|
|
|
|
|
|
if (categoryColumns.length === 0) { |
|
|
|
console.log('添加category字段到products表...'); |
|
|
|
await connection.query( |
|
|
|
'ALTER TABLE `products` ADD COLUMN category VARCHAR(50) COMMENT "种类"' |
|
|
|
); |
|
|
|
console.log('category字段添加成功'); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查表是否有audit_time字段
|
|
|
|
console.log('检查表products是否有audit_time字段...'); |
|
|
|
|