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.
85 lines
2.7 KiB
85 lines
2.7 KiB
// 简单测试服务器 - 不连接数据库,专注于API接口测试和毛重字段处理
|
|
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const path = require('path');
|
|
|
|
// 创建Express应用
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
// 中间件
|
|
app.use(bodyParser.json());
|
|
|
|
// 请求日志中间件
|
|
app.use((req, res, next) => {
|
|
const now = new Date();
|
|
console.log(`[${now.toISOString()}] 收到请求: ${req.method} ${req.url}`);
|
|
next();
|
|
});
|
|
|
|
// 简单测试接口
|
|
app.get('/api/test-connection', (req, res) => {
|
|
res.json({
|
|
success: true,
|
|
message: '服务器连接测试成功',
|
|
timestamp: new Date().toISOString(),
|
|
serverInfo: { port: PORT }
|
|
});
|
|
});
|
|
|
|
// 商品发布接口(简化版,专注于毛重处理)
|
|
app.post('/api/product/publish', (req, res) => {
|
|
try {
|
|
const { openid, product } = req.body;
|
|
console.log('收到商品发布请求:', { openid, product });
|
|
|
|
// 验证参数
|
|
if (!openid || !product) {
|
|
return res.status(400).json({ success: false, message: '缺少必要参数' });
|
|
}
|
|
|
|
// 重点:毛重字段处理逻辑
|
|
let grossWeightValue = product.grossWeight;
|
|
console.log('原始毛重值:', grossWeightValue, '类型:', typeof grossWeightValue);
|
|
|
|
// 处理各种情况的毛重值
|
|
if (grossWeightValue === '' || grossWeightValue === null || grossWeightValue === undefined || (typeof grossWeightValue === 'object' && grossWeightValue === null)) {
|
|
grossWeightValue = null;
|
|
console.log('毛重值为空或null,设置为null');
|
|
} else {
|
|
// 转换为数字
|
|
const numValue = Number(grossWeightValue);
|
|
if (!isNaN(numValue) && isFinite(numValue)) {
|
|
grossWeightValue = numValue;
|
|
console.log('毛重值成功转换为数字:', grossWeightValue);
|
|
} else {
|
|
grossWeightValue = null;
|
|
console.log('毛重值不是有效数字,设置为null');
|
|
}
|
|
}
|
|
|
|
// 返回处理结果
|
|
return res.json({
|
|
success: true,
|
|
message: '商品发布处理成功(模拟)',
|
|
processedData: {
|
|
productName: product.productName,
|
|
price: product.price,
|
|
quantity: product.quantity,
|
|
grossWeight: grossWeightValue, // 返回处理后的毛重值
|
|
grossWeightType: typeof grossWeightValue
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('发布商品失败:', error);
|
|
res.status(500).json({ success: false, message: '服务器错误', error: error.message });
|
|
}
|
|
});
|
|
|
|
// 启动服务器
|
|
app.listen(PORT, () => {
|
|
console.log(`修复版服务器运行在 http://localhost:${PORT}`);
|
|
console.log('测试接口: http://localhost:3000/api/test-connection');
|
|
console.log('商品发布接口: POST http://localhost:3000/api/product/publish');
|
|
});
|